shader_graph_editor_plugin.cpp 93 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945
  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-2016 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. #include "scene/gui/check_box.h"
  31. #include "scene/gui/menu_button.h"
  32. #include "scene/gui/panel.h"
  33. #include "spatial_editor_plugin.h"
  34. #include "os/keyboard.h"
  35. #include "canvas_item_editor_plugin.h"
  36. void GraphColorRampEdit::_input_event(const InputEvent& p_event) {
  37. if (p_event.type==InputEvent::KEY && p_event.key.pressed && p_event.key.scancode==KEY_DELETE && grabbed!=-1) {
  38. points.remove(grabbed);
  39. grabbed=-1;
  40. update();
  41. emit_signal("ramp_changed");
  42. accept_event();
  43. }
  44. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && p_event.mouse_button.pressed) {
  45. update();
  46. int x = p_event.mouse_button.x;
  47. int total_w = get_size().width-get_size().height-3;
  48. if (x>total_w+3) {
  49. if (grabbed==-1)
  50. return;
  51. Size2 ms = Size2(350, picker->get_combined_minimum_size().height+10);
  52. picker->set_color(points[grabbed].color);
  53. popup->set_pos(get_global_pos()-Size2(0,ms.height));
  54. popup->set_size(ms);
  55. popup->popup();
  56. return;
  57. }
  58. float ofs = CLAMP(x/float(total_w),0,1);
  59. grabbed=-1;
  60. grabbing=true;
  61. int pos=-1;
  62. for(int i=0;i<points.size();i++) {
  63. if (ABS(x-points[i].offset*total_w)<4) {
  64. grabbed=i;
  65. }
  66. if (points[i].offset<ofs)
  67. pos=i;
  68. }
  69. grabbed_at=ofs;
  70. //grab or select
  71. if (grabbed!=-1) {
  72. return;
  73. }
  74. //insert
  75. Point p;
  76. p.offset=ofs;
  77. Point prev;
  78. Point next;
  79. if (pos==-1) {
  80. prev.color=Color(0,0,0);
  81. prev.offset=0;
  82. if (points.size()) {
  83. next=points[0];
  84. } else {
  85. next.color=Color(1,1,1);
  86. next.offset=1.0;
  87. }
  88. } else {
  89. if (pos==points.size()-1) {
  90. next.color=Color(1,1,1);
  91. next.offset=1.0;
  92. } else {
  93. next=points[pos+1];
  94. }
  95. prev=points[pos];
  96. }
  97. p.color=prev.color.linear_interpolate(next.color,(p.offset-prev.offset)/(next.offset-prev.offset));
  98. points.push_back(p);
  99. points.sort();
  100. for(int i=0;i<points.size();i++) {
  101. if (points[i].offset==ofs) {
  102. grabbed=i;
  103. break;
  104. }
  105. }
  106. emit_signal("ramp_changed");
  107. }
  108. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && !p_event.mouse_button.pressed) {
  109. if (grabbing) {
  110. grabbing=false;
  111. emit_signal("ramp_changed");
  112. }
  113. update();
  114. }
  115. if (p_event.type==InputEvent::MOUSE_MOTION && grabbing) {
  116. int total_w = get_size().width-get_size().height-3;
  117. int x = p_event.mouse_motion.x;
  118. float newofs = CLAMP(x/float(total_w),0,1);
  119. bool valid=true;
  120. for(int i=0;i<points.size();i++) {
  121. if (points[i].offset==newofs && i!=grabbed) {
  122. valid=false;
  123. }
  124. }
  125. if (!valid)
  126. return;
  127. points[grabbed].offset=newofs;
  128. points.sort();
  129. for(int i=0;i<points.size();i++) {
  130. if (points[i].offset==newofs) {
  131. grabbed=i;
  132. break;
  133. }
  134. }
  135. emit_signal("ramp_changed");
  136. update();
  137. }
  138. }
  139. void GraphColorRampEdit::_notification(int p_what){
  140. if (p_what==NOTIFICATION_ENTER_TREE) {
  141. if (!picker->is_connected("color_changed",this,"_color_changed")) {
  142. picker->connect("color_changed",this,"_color_changed");
  143. }
  144. }
  145. if (p_what==NOTIFICATION_DRAW) {
  146. Point prev;
  147. prev.offset=0;
  148. prev.color=Color(0,0,0);
  149. int h = get_size().y;
  150. int total_w = get_size().width-get_size().height-3;
  151. for(int i=-1;i<points.size();i++) {
  152. Point next;
  153. if (i+1==points.size()) {
  154. next.color=Color(1,1,1);
  155. next.offset=1;
  156. } else {
  157. next=points[i+1];
  158. }
  159. if (prev.offset==next.offset) {
  160. prev=next;
  161. continue;
  162. }
  163. Vector<Vector2> points;
  164. Vector<Color> colors;
  165. points.push_back(Vector2(prev.offset*total_w,h));
  166. points.push_back(Vector2(prev.offset*total_w,0));
  167. points.push_back(Vector2(next.offset*total_w,0));
  168. points.push_back(Vector2(next.offset*total_w,h));
  169. colors.push_back(prev.color);
  170. colors.push_back(prev.color);
  171. colors.push_back(next.color);
  172. colors.push_back(next.color);
  173. draw_primitive(points,colors,Vector<Point2>());
  174. prev=next;
  175. }
  176. for(int i=0;i<points.size();i++) {
  177. Color col=i==grabbed?Color(1,0.0,0.0,0.9):Color(1,1,1,0.8);
  178. draw_line(Vector2(points[i].offset*total_w,0),Vector2(points[i].offset*total_w,h-1),Color(0,0,0,0.7));
  179. draw_line(Vector2(points[i].offset*total_w-1,h/2),Vector2(points[i].offset*total_w-1,h-1),col);
  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/2),col);
  182. draw_line(Vector2(points[i].offset*total_w-1,h-1),Vector2(points[i].offset*total_w+1,h-1),col);
  183. }
  184. if (grabbed!=-1) {
  185. draw_rect(Rect2(total_w+3,0,h,h),points[grabbed].color);
  186. }
  187. if (has_focus()) {
  188. draw_line(Vector2(-1,-1),Vector2(total_w+1,-1),Color(1,1,1,0.6));
  189. draw_line(Vector2(total_w+1,-1),Vector2(total_w+1,h+1),Color(1,1,1,0.6));
  190. draw_line(Vector2(total_w+1,h+1),Vector2(-1,h+1),Color(1,1,1,0.6));
  191. draw_line(Vector2(-1,-1),Vector2(-1,h+1),Color(1,1,1,0.6));
  192. }
  193. }
  194. }
  195. Size2 GraphColorRampEdit::get_minimum_size() const {
  196. return Vector2(0,16);
  197. }
  198. void GraphColorRampEdit::_color_changed(const Color& p_color) {
  199. if (grabbed==-1)
  200. return;
  201. points[grabbed].color=p_color;
  202. update();
  203. emit_signal("ramp_changed");
  204. }
  205. void GraphColorRampEdit::set_ramp(const Vector<float>& p_offsets,const Vector<Color>& p_colors) {
  206. ERR_FAIL_COND(p_offsets.size()!=p_colors.size());
  207. points.clear();
  208. for(int i=0;i<p_offsets.size();i++) {
  209. Point p;
  210. p.offset=p_offsets[i];
  211. p.color=p_colors[i];
  212. points.push_back(p);
  213. }
  214. points.sort();
  215. update();
  216. }
  217. Vector<float> GraphColorRampEdit::get_offsets() const{
  218. Vector<float> ret;
  219. for(int i=0;i<points.size();i++)
  220. ret.push_back(points[i].offset);
  221. return ret;
  222. }
  223. Vector<Color> GraphColorRampEdit::get_colors() const{
  224. Vector<Color> ret;
  225. for(int i=0;i<points.size();i++)
  226. ret.push_back(points[i].color);
  227. return ret;
  228. }
  229. void GraphColorRampEdit::_bind_methods(){
  230. ObjectTypeDB::bind_method(_MD("_input_event"),&GraphColorRampEdit::_input_event);
  231. ObjectTypeDB::bind_method(_MD("_color_changed"),&GraphColorRampEdit::_color_changed);
  232. ADD_SIGNAL(MethodInfo("ramp_changed"));
  233. }
  234. GraphColorRampEdit::GraphColorRampEdit(){
  235. grabbed=-1;
  236. grabbing=false;
  237. set_focus_mode(FOCUS_ALL);
  238. popup = memnew( PopupPanel );
  239. picker = memnew( ColorPicker );
  240. popup->add_child(picker);
  241. popup->set_child_rect(picker);
  242. add_child(popup);
  243. }
  244. ////////////
  245. void GraphCurveMapEdit::_input_event(const InputEvent& p_event) {
  246. if (p_event.type==InputEvent::KEY && p_event.key.pressed && p_event.key.scancode==KEY_DELETE && grabbed!=-1) {
  247. points.remove(grabbed);
  248. grabbed=-1;
  249. update();
  250. emit_signal("curve_changed");
  251. accept_event();
  252. }
  253. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && p_event.mouse_button.pressed) {
  254. update();
  255. Point2 p = Vector2(p_event.mouse_button.x,p_event.mouse_button.y)/get_size();
  256. p.y=1.0-p.y;
  257. grabbed=-1;
  258. grabbing=true;
  259. for(int i=0;i<points.size();i++) {
  260. Vector2 ps = p*get_size();
  261. Vector2 pt = Vector2(points[i].offset,points[i].height)*get_size();
  262. if (ps.distance_to(pt)<4) {
  263. grabbed=i;
  264. }
  265. }
  266. //grab or select
  267. if (grabbed!=-1) {
  268. return;
  269. }
  270. //insert
  271. Point np;
  272. np.offset=p.x;
  273. np.height=p.y;
  274. points.push_back(np);
  275. points.sort();
  276. for(int i=0;i<points.size();i++) {
  277. if (points[i].offset==p.x && points[i].height==p.y) {
  278. grabbed=i;
  279. break;
  280. }
  281. }
  282. emit_signal("curve_changed");
  283. }
  284. if (p_event.type==InputEvent::MOUSE_BUTTON && p_event.mouse_button.button_index==1 && !p_event.mouse_button.pressed) {
  285. if (grabbing) {
  286. grabbing=false;
  287. emit_signal("curve_changed");
  288. }
  289. update();
  290. }
  291. if (p_event.type==InputEvent::MOUSE_MOTION && grabbing && grabbed != -1) {
  292. Point2 p = Vector2(p_event.mouse_button.x,p_event.mouse_button.y)/get_size();
  293. p.y=1.0-p.y;
  294. p.x = CLAMP(p.x,0.0,1.0);
  295. p.y = CLAMP(p.y,0.0,1.0);
  296. bool valid=true;
  297. for(int i=0;i<points.size();i++) {
  298. if (points[i].offset==p.x && points[i].height==p.y && i!=grabbed) {
  299. valid=false;
  300. }
  301. }
  302. if (!valid)
  303. return;
  304. points[grabbed].offset=p.x;
  305. points[grabbed].height=p.y;
  306. points.sort();
  307. for(int i=0;i<points.size();i++) {
  308. if (points[i].offset==p.x && points[i].height==p.y) {
  309. grabbed=i;
  310. break;
  311. }
  312. }
  313. emit_signal("curve_changed");
  314. update();
  315. }
  316. }
  317. void GraphCurveMapEdit::_plot_curve(const Vector2& p_a,const Vector2& p_b,const Vector2& p_c,const Vector2& p_d) {
  318. float geometry[4][4];
  319. float tmp1[4][4];
  320. float tmp2[4][4];
  321. float deltas[4][4];
  322. double x, dx, dx2, dx3;
  323. double y, dy, dy2, dy3;
  324. double d, d2, d3;
  325. int lastx, lasty;
  326. int newx, newy;
  327. int ntimes;
  328. int i,j;
  329. int xmax=get_size().x;
  330. int ymax=get_size().y;
  331. /* construct the geometry matrix from the segment */
  332. for (i = 0; i < 4; i++) {
  333. geometry[i][2] = 0;
  334. geometry[i][3] = 0;
  335. }
  336. geometry[0][0] = (p_a[0] * xmax);
  337. geometry[1][0] = (p_b[0] * xmax);
  338. geometry[2][0] = (p_c[0] * xmax);
  339. geometry[3][0] = (p_d[0] * xmax);
  340. geometry[0][1] = (p_a[1] * ymax);
  341. geometry[1][1] = (p_b[1] * ymax);
  342. geometry[2][1] = (p_c[1] * ymax);
  343. geometry[3][1] = (p_d[1] * ymax);
  344. /* subdivide the curve ntimes (1000) times */
  345. ntimes = 4 * xmax;
  346. /* ntimes can be adjusted to give a finer or coarser curve */
  347. d = 1.0 / ntimes;
  348. d2 = d * d;
  349. d3 = d * d * d;
  350. /* construct a temporary matrix for determining the forward differencing deltas */
  351. tmp2[0][0] = 0; tmp2[0][1] = 0; tmp2[0][2] = 0; tmp2[0][3] = 1;
  352. tmp2[1][0] = d3; tmp2[1][1] = d2; tmp2[1][2] = d; tmp2[1][3] = 0;
  353. tmp2[2][0] = 6*d3; tmp2[2][1] = 2*d2; tmp2[2][2] = 0; tmp2[2][3] = 0;
  354. tmp2[3][0] = 6*d3; tmp2[3][1] = 0; tmp2[3][2] = 0; tmp2[3][3] = 0;
  355. /* compose the basis and geometry matrices */
  356. static const float CR_basis[4][4] =
  357. {
  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. ObjectTypeDB::bind_method(_MD("_input_event"),&GraphCurveMapEdit::_input_event);
  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. DVector<float> new_offsets;
  773. DVector<Color> new_colors;
  774. {
  775. new_offsets.resize(offsets.size());
  776. new_colors.resize(colors.size());
  777. DVector<float>::Write ow=new_offsets.write();
  778. DVector<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. DVector<float> old_offsets=graph->color_ramp_node_get_offsets(type,p_id);
  785. DVector<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. DVector<Vector2> new_points;
  803. {
  804. new_points.resize(points.size());
  805. DVector<Vector2>::Write ow=new_points.write();
  806. for(int i=0;i<new_points.size();i++) {
  807. ow[i]=points[i];
  808. }
  809. }
  810. DVector<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_RGB);
  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. DVector<real_t> offsets = graph->color_ramp_node_get_offsets(type,p_id);
  1717. DVector<Color> colors = graph->color_ramp_node_get_colors(type,p_id);
  1718. int oc = offsets.size();
  1719. if (oc) {
  1720. DVector<real_t>::Read rofs = offsets.read();
  1721. DVector<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. DVector<Vector2> points = graph->curve_map_node_get_points(type,p_id);
  1756. int oc = points.size();
  1757. if (oc) {
  1758. DVector<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. TextureFrame *tex = memnew( TextureFrame );
  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_ignore_mouse(false);
  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("Diffuse");
  1956. colors.push_back("Specular");
  1957. colors.push_back("Emmision");
  1958. Array reals;
  1959. reals.push_back("Alpha");
  1960. reals.push_back("DiffuseAlpha");
  1961. reals.push_back("NormalMapDepth");
  1962. reals.push_back("SpecExp");
  1963. reals.push_back("Glow");
  1964. reals.push_back("ShadeParam");
  1965. reals.push_back("SpecularExp");
  1966. reals.push_back("LightAlpha");
  1967. reals.push_back("PointSize");
  1968. reals.push_back("Discard");
  1969. int idx=0;
  1970. for (List<ShaderGraph::SlotInfo>::Element *E=si.front();E;E=E->next()) {
  1971. ShaderGraph::SlotInfo& s=E->get();
  1972. if (s.dir==ShaderGraph::SLOT_OUT) {
  1973. Variant::Type v;
  1974. if (colors.find(s.name)>=0)
  1975. v=Variant::COLOR;
  1976. else if (reals.find(s.name)>=0)
  1977. v=Variant::REAL;
  1978. else
  1979. v=Variant::VECTOR3;
  1980. gn->add_child(make_label(s.name, v));
  1981. gn->set_slot(idx,true,s.type,typecol[s.type],false,0,Color());
  1982. idx++;
  1983. }
  1984. }
  1985. } break; // output (case Shader type dependent)
  1986. case ShaderGraph::NODE_COMMENT: {
  1987. gn->set_title("Comment");
  1988. TextEdit *te = memnew(TextEdit);
  1989. te->set_custom_minimum_size(Size2(100,100));
  1990. gn->add_child(te);
  1991. te->set_text(graph->comment_node_get_text(type,p_id));
  1992. te->connect("text_changed",this,"_comment_edited",varray(p_id,te));
  1993. } break; // comment
  1994. }
  1995. gn->connect("dragged",this,"_node_moved",varray(p_id));
  1996. gn->connect("close_request",this,"_node_removed",varray(p_id),CONNECT_DEFERRED);
  1997. graph_edit->add_child(gn);
  1998. node_map[p_id]=gn;
  1999. gn->set_offset(graph->node_get_pos(type,p_id));
  2000. }
  2001. void ShaderGraphView::_update_graph() {
  2002. if (block_update)
  2003. return;
  2004. for (Map<int,GraphNode*>::Element *E=node_map.front();E;E=E->next()) {
  2005. memdelete(E->get());
  2006. }
  2007. node_map.clear();
  2008. if (!graph.is_valid())
  2009. return;
  2010. List<int> nl;
  2011. graph->get_node_list(type,&nl);
  2012. for(List<int>::Element *E=nl.front();E;E=E->next()) {
  2013. _create_node(E->get());
  2014. }
  2015. graph_edit->clear_connections();
  2016. List<ShaderGraph::Connection> connections;
  2017. graph->get_node_connections(type,&connections);
  2018. for(List<ShaderGraph::Connection>::Element *E=connections.front();E;E=E->next()) {
  2019. ERR_CONTINUE(!node_map.has(E->get().src_id) || !node_map.has(E->get().dst_id));
  2020. 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);
  2021. }
  2022. }
  2023. void ShaderGraphView::_sg_updated() {
  2024. if (!graph.is_valid())
  2025. return;
  2026. switch(graph->get_graph_error(type)) {
  2027. case ShaderGraph::GRAPH_OK: status->set_text(""); break;
  2028. case ShaderGraph::GRAPH_ERROR_CYCLIC: status->set_text(TTR("Error: Cyclic Connection Link")); break;
  2029. case ShaderGraph::GRAPH_ERROR_MISSING_CONNECTIONS: status->set_text(TTR("Error: Missing Input Connections")); break;
  2030. }
  2031. }
  2032. Variant ShaderGraphView::get_drag_data_fw(const Point2 &p_point, Control *p_from)
  2033. {
  2034. TextureFrame* frame = p_from->cast_to<TextureFrame>();
  2035. if (!frame)
  2036. return Variant();
  2037. if (!frame->get_texture().is_valid())
  2038. return Variant();
  2039. RES res = frame->get_texture();
  2040. return EditorNode::get_singleton()->drag_resource(res,p_from);
  2041. return Variant();
  2042. }
  2043. bool ShaderGraphView::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const
  2044. {
  2045. if (p_data.get_type() != Variant::DICTIONARY)
  2046. return false;
  2047. Dictionary d = p_data;
  2048. if (d.has("type")){
  2049. if (d["type"] == "resource" && d.has("resource")) {
  2050. Variant val = d["resource"];
  2051. if (val.get_type()==Variant::OBJECT) {
  2052. RES res = val;
  2053. if (res.is_valid() && res->cast_to<Texture>())
  2054. return true;
  2055. }
  2056. }
  2057. else if (d["type"] == "files" && d.has("files")) {
  2058. Vector<String> files = d["files"];
  2059. if (files.size() != 1)
  2060. return false;
  2061. return (ResourceLoader::get_resource_type(files[0]) == "ImageTexture");
  2062. }
  2063. }
  2064. return false;
  2065. }
  2066. void ShaderGraphView::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from)
  2067. {
  2068. if (!can_drop_data_fw(p_point, p_data, p_from))
  2069. return;
  2070. TextureFrame *frame = p_from->cast_to<TextureFrame>();
  2071. if (!frame)
  2072. return;
  2073. Dictionary d = p_data;
  2074. Ref<Texture> tex;
  2075. if (d.has("type")) {
  2076. if (d["type"] == "resource" && d.has("resource")){
  2077. Variant val = d["resource"];
  2078. if (val.get_type()==Variant::OBJECT) {
  2079. RES res = val;
  2080. if (res.is_valid())
  2081. tex = Ref<Texture>(res->cast_to<Texture>());
  2082. }
  2083. }
  2084. else if (d["type"] == "files" && d.has("files")) {
  2085. Vector<String> files = d["files"];
  2086. RES res = ResourceLoader::load(files[0]);
  2087. if (res.is_valid())
  2088. tex = Ref<Texture>(res->cast_to<Texture>());
  2089. }
  2090. }
  2091. if (!tex.is_valid()) return;
  2092. GraphNode *gn = frame->get_parent()->cast_to<GraphNode>();
  2093. if (!gn) return;
  2094. int id = -1;
  2095. for(Map<int,GraphNode*>::Element *E = node_map.front();E;E=E->next())
  2096. if (E->get() == gn) {
  2097. id = E->key();
  2098. break;
  2099. }
  2100. print_line(String::num(double(id)));
  2101. if (id < 0) return;
  2102. if (graph->node_get_type(type,id)==ShaderGraph::NODE_TEXTURE_INPUT) {
  2103. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  2104. ur->create_action(TTR("Change Texture Uniform"));
  2105. ur->add_do_method(graph.ptr(),"texture_input_node_set_value",type,id,tex);
  2106. ur->add_undo_method(graph.ptr(),"texture_input_node_set_value",type,id,graph->texture_input_node_get_value(type,id));
  2107. ur->add_do_method(this,"_update_graph");
  2108. ur->add_undo_method(this,"_update_graph");
  2109. ur->commit_action();
  2110. }
  2111. }
  2112. void ShaderGraphView::set_graph(Ref<ShaderGraph> p_graph){
  2113. if (graph.is_valid()) {
  2114. graph->disconnect("updated",this,"_sg_updated");
  2115. }
  2116. graph=p_graph;
  2117. if (graph.is_valid()) {
  2118. graph->connect("updated",this,"_sg_updated");
  2119. }
  2120. _update_graph();
  2121. _sg_updated();
  2122. }
  2123. void ShaderGraphView::_notification(int p_what) {
  2124. if (p_what==NOTIFICATION_ENTER_TREE) {
  2125. ped_popup->connect("variant_changed",this,"_variant_edited");
  2126. }
  2127. }
  2128. void ShaderGraphView::add_node(int p_type, const Vector2 &location) {
  2129. if (p_type==ShaderGraph::NODE_INPUT && graph->node_count(type, p_type)>0)
  2130. return;
  2131. List<int> existing;
  2132. graph->get_node_list(type,&existing);
  2133. existing.sort();
  2134. int newid=1;
  2135. for(List<int>::Element *E=existing.front();E;E=E->next()) {
  2136. if (!E->next() || (E->get()+1!=E->next()->get())){
  2137. newid=E->get()+1;
  2138. break;
  2139. }
  2140. }
  2141. Vector2 init_ofs = location;
  2142. while(true) {
  2143. bool valid=true;
  2144. for(List<int>::Element *E=existing.front();E;E=E->next()) {
  2145. Vector2 pos = graph->node_get_pos(type,E->get());
  2146. if (init_ofs==pos) {
  2147. init_ofs+=Vector2(20,20);
  2148. valid=false;
  2149. break;
  2150. }
  2151. }
  2152. if (valid)
  2153. break;
  2154. }
  2155. UndoRedo *ur=EditorNode::get_singleton()->get_undo_redo();
  2156. ur->create_action(TTR("Add Shader Graph Node"));
  2157. ur->add_do_method(graph.ptr(),"node_add",type,p_type,newid);
  2158. ur->add_do_method(graph.ptr(),"node_set_pos",type,newid,init_ofs);
  2159. ur->add_undo_method(graph.ptr(),"node_remove",type,newid);
  2160. ur->add_do_method(this,"_update_graph");
  2161. ur->add_undo_method(this,"_update_graph");
  2162. ur->commit_action();
  2163. }
  2164. void ShaderGraphView::_bind_methods() {
  2165. ObjectTypeDB::bind_method("_update_graph",&ShaderGraphView::_update_graph);
  2166. ObjectTypeDB::bind_method("_begin_node_move", &ShaderGraphView::_begin_node_move);
  2167. ObjectTypeDB::bind_method("_node_moved",&ShaderGraphView::_node_moved);
  2168. ObjectTypeDB::bind_method("_end_node_move", &ShaderGraphView::_end_node_move);
  2169. ObjectTypeDB::bind_method("_move_node",&ShaderGraphView::_move_node);
  2170. ObjectTypeDB::bind_method("_node_removed",&ShaderGraphView::_node_removed);
  2171. ObjectTypeDB::bind_method("_connection_request",&ShaderGraphView::_connection_request);
  2172. ObjectTypeDB::bind_method("_disconnection_request",&ShaderGraphView::_disconnection_request);
  2173. ObjectTypeDB::bind_method("_duplicate_nodes_request", &ShaderGraphView::_duplicate_nodes_request);
  2174. ObjectTypeDB::bind_method("_duplicate_nodes", &ShaderGraphView::_duplicate_nodes);
  2175. ObjectTypeDB::bind_method("_delete_nodes_request", &ShaderGraphView::_delete_nodes_request);
  2176. ObjectTypeDB::bind_method("_default_changed",&ShaderGraphView::_default_changed);
  2177. ObjectTypeDB::bind_method("_scalar_const_changed",&ShaderGraphView::_scalar_const_changed);
  2178. ObjectTypeDB::bind_method("_vec_const_changed",&ShaderGraphView::_vec_const_changed);
  2179. ObjectTypeDB::bind_method("_rgb_const_changed",&ShaderGraphView::_rgb_const_changed);
  2180. ObjectTypeDB::bind_method("_xform_const_changed",&ShaderGraphView::_xform_const_changed);
  2181. ObjectTypeDB::bind_method("_scalar_op_changed",&ShaderGraphView::_scalar_op_changed);
  2182. ObjectTypeDB::bind_method("_vec_op_changed",&ShaderGraphView::_vec_op_changed);
  2183. ObjectTypeDB::bind_method("_vec_scalar_op_changed",&ShaderGraphView::_vec_scalar_op_changed);
  2184. ObjectTypeDB::bind_method("_rgb_op_changed",&ShaderGraphView::_rgb_op_changed);
  2185. ObjectTypeDB::bind_method("_xform_inv_rev_changed",&ShaderGraphView::_xform_inv_rev_changed);
  2186. ObjectTypeDB::bind_method("_scalar_func_changed",&ShaderGraphView::_scalar_func_changed);
  2187. ObjectTypeDB::bind_method("_vec_func_changed",&ShaderGraphView::_vec_func_changed);
  2188. ObjectTypeDB::bind_method("_scalar_input_changed",&ShaderGraphView::_scalar_input_changed);
  2189. ObjectTypeDB::bind_method("_vec_input_changed",&ShaderGraphView::_vec_input_changed);
  2190. ObjectTypeDB::bind_method("_xform_input_changed",&ShaderGraphView::_xform_input_changed);
  2191. ObjectTypeDB::bind_method("_rgb_input_changed",&ShaderGraphView::_rgb_input_changed);
  2192. ObjectTypeDB::bind_method("_tex_input_change",&ShaderGraphView::_tex_input_change);
  2193. ObjectTypeDB::bind_method("_cube_input_change",&ShaderGraphView::_cube_input_change);
  2194. ObjectTypeDB::bind_method("_input_name_changed",&ShaderGraphView::_input_name_changed);
  2195. ObjectTypeDB::bind_method("_tex_edited",&ShaderGraphView::_tex_edited);
  2196. ObjectTypeDB::bind_method("_variant_edited",&ShaderGraphView::_variant_edited);
  2197. ObjectTypeDB::bind_method("_cube_edited",&ShaderGraphView::_cube_edited);
  2198. ObjectTypeDB::bind_method("_comment_edited",&ShaderGraphView::_comment_edited);
  2199. ObjectTypeDB::bind_method("_color_ramp_changed",&ShaderGraphView::_color_ramp_changed);
  2200. ObjectTypeDB::bind_method("_curve_changed",&ShaderGraphView::_curve_changed);
  2201. ObjectTypeDB::bind_method(_MD("get_drag_data_fw"), &ShaderGraphView::get_drag_data_fw);
  2202. ObjectTypeDB::bind_method(_MD("can_drop_data_fw"), &ShaderGraphView::can_drop_data_fw);
  2203. ObjectTypeDB::bind_method(_MD("drop_data_fw"), &ShaderGraphView::drop_data_fw);
  2204. ObjectTypeDB::bind_method("_sg_updated",&ShaderGraphView::_sg_updated);
  2205. }
  2206. ShaderGraphView::ShaderGraphView(ShaderGraph::ShaderType p_type) {
  2207. type=p_type;
  2208. graph_edit = memnew( GraphEdit );
  2209. block_update=false;
  2210. ped_popup = memnew( CustomPropertyEditor );
  2211. graph_edit->add_child(ped_popup);
  2212. status = memnew( Label );
  2213. graph_edit->get_top_layer()->add_child(status);
  2214. graph_edit->connect("_begin_node_move", this, "_begin_node_move");
  2215. graph_edit->connect("_end_node_move", this, "_end_node_move");
  2216. status->set_pos(Vector2(5,5));
  2217. status->add_color_override("font_color_shadow",Color(0,0,0));
  2218. status->add_color_override("font_color",Color(1,0.4,0.3));
  2219. status->add_constant_override("shadow_as_outline",1);
  2220. status->add_constant_override("shadow_offset_x",2);
  2221. status->add_constant_override("shadow_offset_y",2);
  2222. status->set_text("");
  2223. }
  2224. //////////////edit//////////////
  2225. void ShaderGraphEditor::edit(Ref<ShaderGraph> p_shader) {
  2226. for(int i=0;i<ShaderGraph::SHADER_TYPE_MAX;i++) {
  2227. graph_edits[i]->set_graph(p_shader);
  2228. }
  2229. }
  2230. void ShaderGraphEditor::_add_node(int p_type) {
  2231. ShaderGraph::ShaderType shader_type=ShaderGraph::ShaderType(tabs->get_current_tab());
  2232. graph_edits[shader_type]->add_node(p_type, next_location);
  2233. }
  2234. void ShaderGraphEditor::_popup_requested(const Vector2 &p_position)
  2235. {
  2236. Vector2 scroll_ofs=graph_edits[tabs->get_current_tab()]->get_graph_edit()->get_scroll_ofs();
  2237. next_location = get_local_mouse_pos() + scroll_ofs;
  2238. popup->set_global_pos(p_position);
  2239. popup->set_size( Size2( 200, 0) );
  2240. popup->popup();
  2241. popup->call_deferred("grab_click_focus");
  2242. popup->set_invalidate_click_until_motion();
  2243. }
  2244. void ShaderGraphEditor::_notification(int p_what) {
  2245. if (p_what==NOTIFICATION_ENTER_TREE) {
  2246. for(int i=0;i<ShaderGraph::NODE_TYPE_MAX;i++) {
  2247. if (i==ShaderGraph::NODE_OUTPUT)
  2248. continue;
  2249. if (!_2d && i==ShaderGraph::NODE_DEFAULT_TEXTURE)
  2250. continue;
  2251. String nn = node_names[i];
  2252. String ic = nn.get_slice(":",0);
  2253. String v = nn.get_slice(":",1);
  2254. bool addsep=false;
  2255. if (nn.ends_with(":")) {
  2256. addsep=true;
  2257. }
  2258. popup->add_icon_item(get_icon(ic,"EditorIcons"),v,i);
  2259. if (addsep)
  2260. popup->add_separator();
  2261. }
  2262. popup->connect("item_pressed",this,"_add_node");
  2263. }
  2264. }
  2265. void ShaderGraphEditor::_bind_methods() {
  2266. ObjectTypeDB::bind_method("_add_node",&ShaderGraphEditor::_add_node);
  2267. ObjectTypeDB::bind_method("_popup_requested",&ShaderGraphEditor::_popup_requested);
  2268. }
  2269. const char* ShaderGraphEditor::node_names[ShaderGraph::NODE_TYPE_MAX]={
  2270. ("GraphInput:Input"), // all inputs (shader type dependent)
  2271. ("GraphScalar:Scalar Constant"), //scalar constant
  2272. ("GraphVector:Vector Constant"), //vec3 constant
  2273. ("GraphRgb:RGB Constant"), //rgb constant (shows a color picker instead)
  2274. ("GraphXform:XForm Constant"), // 4x4 matrix constant
  2275. ("GraphTime:Time:"), // time in seconds
  2276. ("GraphTexscreen:Screen Sample"), // screen texture sampler (takes uv) (only usable in fragment shader)
  2277. ("GraphScalarOp:Scalar Operator"), // scalar vs scalar op (mul", add", div", etc)
  2278. ("GraphVecOp:Vector Operator"), // vec3 vs vec3 op (mul",ad",div",crossprod",etc)
  2279. ("GraphVecScalarOp:Scalar+Vector Operator"), // vec3 vs scalar op (mul", add", div", etc)
  2280. ("GraphRgbOp:RGB Operator:"), // vec3 vs vec3 rgb op (with scalar amount)", like brighten", darken", burn", dodge", multiply", etc.
  2281. ("GraphXformMult:XForm Multiply"), // mat4 x mat4
  2282. ("GraphXformVecMult:XForm+Vector Multiply"), // mat4 x vec3 mult (with no-translation option)
  2283. ("GraphXformVecImult:Form+Vector InvMultiply:"), // mat4 x vec3 inverse mult (with no-translation option)
  2284. ("GraphXformScalarFunc:Scalar Function"), // scalar function (sin", cos", etc)
  2285. ("GraphXformVecFunc:Vector Function"), // vector function (normalize", negate", reciprocal", rgb2hsv", hsv2rgb", etc", etc)
  2286. ("GraphVecLength:Vector Length"), // vec3 length
  2287. ("GraphVecDp:Dot Product:"), // vec3 . vec3 (dot product -> scalar output)
  2288. ("GraphVecToScalars:Vector -> Scalars"), // 1 vec3 input", 3 scalar outputs
  2289. ("GraphScalarsToVec:Scalars -> Vector"), // 3 scalar input", 1 vec3 output
  2290. ("GraphXformToVecs:XForm -> Vectors"), // 3 vec input", 1 xform output
  2291. ("GraphVecsToXform:Vectors -> XForm:"), // 3 vec input", 1 xform output
  2292. ("GraphScalarInterp:Scalar Interpolate"), // scalar interpolation (with optional curve)
  2293. ("GraphVecInterp:Vector Interpolate:"), // vec3 interpolation (with optional curve)
  2294. ("GraphColorRamp:Color Ramp"), // vec3 interpolation (with optional curve)
  2295. ("GraphCurveMap:Curve Remap:"), // vec3 interpolation (with optional curve)
  2296. ("GraphScalarUniform:Scalar Uniform"), // scalar uniform (assignable in material)
  2297. ("GraphVectorUniform:Vector Uniform"), // vec3 uniform (assignable in material)
  2298. ("GraphRgbUniform:RGB Uniform"), // color uniform (assignable in material)
  2299. ("GraphXformUniform:XForm Uniform"), // mat4 uniform (assignable in material)
  2300. ("GraphTextureUniform:Texture Uniform"), // texture input (assignable in material)
  2301. ("GraphCubeUniform:CubeMap Uniform:"), // cubemap input (assignable in material)
  2302. ("GraphDefaultTexture:CanvasItem Texture:"), // cubemap input (assignable in material)
  2303. ("Output"), // output (shader type dependent)
  2304. ("GraphComment:Comment"), // comment
  2305. };
  2306. ShaderGraphEditor::ShaderGraphEditor(bool p_2d) {
  2307. _2d=p_2d;
  2308. popup = memnew( PopupMenu );
  2309. add_child(popup);
  2310. tabs = memnew(TabContainer);
  2311. tabs->set_v_size_flags(SIZE_EXPAND_FILL);
  2312. add_child(tabs);
  2313. const char* sname[ShaderGraph::SHADER_TYPE_MAX]={
  2314. "Vertex",
  2315. "Fragment",
  2316. "Light"
  2317. };
  2318. for(int i=0;i<ShaderGraph::SHADER_TYPE_MAX;i++) {
  2319. graph_edits[i]= memnew( ShaderGraphView(ShaderGraph::ShaderType(i)) );
  2320. add_child(graph_edits[i]);
  2321. graph_edits[i]->get_graph_edit()->set_name(sname[i]);
  2322. tabs->add_child(graph_edits[i]->get_graph_edit());
  2323. graph_edits[i]->get_graph_edit()->connect("connection_request",graph_edits[i],"_connection_request");
  2324. graph_edits[i]->get_graph_edit()->connect("disconnection_request",graph_edits[i],"_disconnection_request");
  2325. graph_edits[i]->get_graph_edit()->connect("duplicate_nodes_request", graph_edits[i], "_duplicate_nodes_request");
  2326. graph_edits[i]->get_graph_edit()->connect("popup_request",this,"_popup_requested");
  2327. graph_edits[i]->get_graph_edit()->connect("delete_nodes_request",graph_edits[i],"_delete_nodes_request");
  2328. graph_edits[i]->get_graph_edit()->set_right_disconnects(true);
  2329. }
  2330. tabs->set_current_tab(1);
  2331. set_custom_minimum_size(Size2(100,300));
  2332. }
  2333. void ShaderGraphEditorPlugin::edit(Object *p_object) {
  2334. shader_editor->edit(p_object->cast_to<ShaderGraph>());
  2335. }
  2336. bool ShaderGraphEditorPlugin::handles(Object *p_object) const {
  2337. ShaderGraph *shader=p_object->cast_to<ShaderGraph>();
  2338. if (!shader)
  2339. return false;
  2340. if (_2d)
  2341. return shader->get_mode()==Shader::MODE_CANVAS_ITEM;
  2342. else
  2343. return shader->get_mode()==Shader::MODE_MATERIAL;
  2344. }
  2345. void ShaderGraphEditorPlugin::make_visible(bool p_visible) {
  2346. if (p_visible) {
  2347. shader_editor->show();
  2348. } else {
  2349. shader_editor->hide();
  2350. }
  2351. }
  2352. ShaderGraphEditorPlugin::ShaderGraphEditorPlugin(EditorNode *p_node, bool p_2d) {
  2353. _2d=p_2d;
  2354. editor=p_node;
  2355. shader_editor = memnew( ShaderGraphEditor(p_2d) );
  2356. shader_editor->hide();
  2357. if (p_2d)
  2358. CanvasItemEditor::get_singleton()->get_bottom_split()->add_child(shader_editor);
  2359. else
  2360. SpatialEditor::get_singleton()->get_shader_split()->add_child(shader_editor);
  2361. // editor->get_viewport()->add_child(shader_editor);
  2362. // shader_editor->set_area_as_parent_rect();
  2363. // shader_editor->hide();
  2364. }
  2365. ShaderGraphEditorPlugin::~ShaderGraphEditorPlugin()
  2366. {
  2367. }