visual_shader_nodes.cpp 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421
  1. /**************************************************************************/
  2. /* visual_shader_nodes.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "visual_shader_nodes.h"
  31. ////////////// Scalar
  32. String VisualShaderNodeScalarConstant::get_caption() const {
  33. return "Scalar";
  34. }
  35. int VisualShaderNodeScalarConstant::get_input_port_count() const {
  36. return 0;
  37. }
  38. VisualShaderNodeScalarConstant::PortType VisualShaderNodeScalarConstant::get_input_port_type(int p_port) const {
  39. return PORT_TYPE_SCALAR;
  40. }
  41. String VisualShaderNodeScalarConstant::get_input_port_name(int p_port) const {
  42. return String();
  43. }
  44. int VisualShaderNodeScalarConstant::get_output_port_count() const {
  45. return 1;
  46. }
  47. VisualShaderNodeScalarConstant::PortType VisualShaderNodeScalarConstant::get_output_port_type(int p_port) const {
  48. return PORT_TYPE_SCALAR;
  49. }
  50. String VisualShaderNodeScalarConstant::get_output_port_name(int p_port) const {
  51. return ""; //no output port means the editor will be used as port
  52. }
  53. String VisualShaderNodeScalarConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  54. return "\t" + p_output_vars[0] + " = " + vformat("%.6f", constant) + ";\n";
  55. }
  56. void VisualShaderNodeScalarConstant::set_constant(float p_value) {
  57. constant = p_value;
  58. emit_changed();
  59. }
  60. float VisualShaderNodeScalarConstant::get_constant() const {
  61. return constant;
  62. }
  63. Vector<StringName> VisualShaderNodeScalarConstant::get_editable_properties() const {
  64. Vector<StringName> props;
  65. props.push_back("constant");
  66. return props;
  67. }
  68. void VisualShaderNodeScalarConstant::_bind_methods() {
  69. ClassDB::bind_method(D_METHOD("set_constant", "value"), &VisualShaderNodeScalarConstant::set_constant);
  70. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeScalarConstant::get_constant);
  71. ADD_PROPERTY(PropertyInfo(Variant::REAL, "constant"), "set_constant", "get_constant");
  72. }
  73. VisualShaderNodeScalarConstant::VisualShaderNodeScalarConstant() {
  74. constant = 0;
  75. }
  76. ////////////// Boolean
  77. String VisualShaderNodeBooleanConstant::get_caption() const {
  78. return "Boolean";
  79. }
  80. int VisualShaderNodeBooleanConstant::get_input_port_count() const {
  81. return 0;
  82. }
  83. VisualShaderNodeBooleanConstant::PortType VisualShaderNodeBooleanConstant::get_input_port_type(int p_port) const {
  84. return PORT_TYPE_BOOLEAN;
  85. }
  86. String VisualShaderNodeBooleanConstant::get_input_port_name(int p_port) const {
  87. return String();
  88. }
  89. int VisualShaderNodeBooleanConstant::get_output_port_count() const {
  90. return 1;
  91. }
  92. VisualShaderNodeBooleanConstant::PortType VisualShaderNodeBooleanConstant::get_output_port_type(int p_port) const {
  93. return PORT_TYPE_BOOLEAN;
  94. }
  95. String VisualShaderNodeBooleanConstant::get_output_port_name(int p_port) const {
  96. return ""; //no output port means the editor will be used as port
  97. }
  98. String VisualShaderNodeBooleanConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  99. return "\t" + p_output_vars[0] + " = " + (constant ? "true" : "false") + ";\n";
  100. }
  101. void VisualShaderNodeBooleanConstant::set_constant(bool p_value) {
  102. constant = p_value;
  103. emit_changed();
  104. }
  105. bool VisualShaderNodeBooleanConstant::get_constant() const {
  106. return constant;
  107. }
  108. Vector<StringName> VisualShaderNodeBooleanConstant::get_editable_properties() const {
  109. Vector<StringName> props;
  110. props.push_back("constant");
  111. return props;
  112. }
  113. void VisualShaderNodeBooleanConstant::_bind_methods() {
  114. ClassDB::bind_method(D_METHOD("set_constant", "value"), &VisualShaderNodeBooleanConstant::set_constant);
  115. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeBooleanConstant::get_constant);
  116. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "constant"), "set_constant", "get_constant");
  117. }
  118. VisualShaderNodeBooleanConstant::VisualShaderNodeBooleanConstant() {
  119. constant = false;
  120. }
  121. ////////////// Color
  122. String VisualShaderNodeColorConstant::get_caption() const {
  123. return "Color";
  124. }
  125. int VisualShaderNodeColorConstant::get_input_port_count() const {
  126. return 0;
  127. }
  128. VisualShaderNodeColorConstant::PortType VisualShaderNodeColorConstant::get_input_port_type(int p_port) const {
  129. return PORT_TYPE_VECTOR;
  130. }
  131. String VisualShaderNodeColorConstant::get_input_port_name(int p_port) const {
  132. return String();
  133. }
  134. int VisualShaderNodeColorConstant::get_output_port_count() const {
  135. return 2;
  136. }
  137. VisualShaderNodeColorConstant::PortType VisualShaderNodeColorConstant::get_output_port_type(int p_port) const {
  138. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  139. }
  140. String VisualShaderNodeColorConstant::get_output_port_name(int p_port) const {
  141. return p_port == 0 ? "" : "alpha"; //no output port means the editor will be used as port
  142. }
  143. String VisualShaderNodeColorConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  144. String code;
  145. code += "\t" + p_output_vars[0] + " = " + vformat("vec3(%.6f, %.6f, %.6f)", constant.r, constant.g, constant.b) + ";\n";
  146. code += "\t" + p_output_vars[1] + " = " + vformat("%.6f", constant.a) + ";\n";
  147. return code;
  148. }
  149. void VisualShaderNodeColorConstant::set_constant(Color p_value) {
  150. constant = p_value;
  151. emit_changed();
  152. }
  153. Color VisualShaderNodeColorConstant::get_constant() const {
  154. return constant;
  155. }
  156. Vector<StringName> VisualShaderNodeColorConstant::get_editable_properties() const {
  157. Vector<StringName> props;
  158. props.push_back("constant");
  159. return props;
  160. }
  161. void VisualShaderNodeColorConstant::_bind_methods() {
  162. ClassDB::bind_method(D_METHOD("set_constant", "value"), &VisualShaderNodeColorConstant::set_constant);
  163. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeColorConstant::get_constant);
  164. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "constant"), "set_constant", "get_constant");
  165. }
  166. VisualShaderNodeColorConstant::VisualShaderNodeColorConstant() {
  167. constant = Color(1, 1, 1, 1);
  168. }
  169. ////////////// Vector
  170. String VisualShaderNodeVec3Constant::get_caption() const {
  171. return "Vector";
  172. }
  173. int VisualShaderNodeVec3Constant::get_input_port_count() const {
  174. return 0;
  175. }
  176. VisualShaderNodeVec3Constant::PortType VisualShaderNodeVec3Constant::get_input_port_type(int p_port) const {
  177. return PORT_TYPE_VECTOR;
  178. }
  179. String VisualShaderNodeVec3Constant::get_input_port_name(int p_port) const {
  180. return String();
  181. }
  182. int VisualShaderNodeVec3Constant::get_output_port_count() const {
  183. return 1;
  184. }
  185. VisualShaderNodeVec3Constant::PortType VisualShaderNodeVec3Constant::get_output_port_type(int p_port) const {
  186. return PORT_TYPE_VECTOR;
  187. }
  188. String VisualShaderNodeVec3Constant::get_output_port_name(int p_port) const {
  189. return ""; //no output port means the editor will be used as port
  190. }
  191. String VisualShaderNodeVec3Constant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  192. return "\t" + p_output_vars[0] + " = " + vformat("vec3(%.6f, %.6f, %.6f)", constant.x, constant.y, constant.z) + ";\n";
  193. }
  194. void VisualShaderNodeVec3Constant::set_constant(Vector3 p_value) {
  195. constant = p_value;
  196. emit_changed();
  197. }
  198. Vector3 VisualShaderNodeVec3Constant::get_constant() const {
  199. return constant;
  200. }
  201. Vector<StringName> VisualShaderNodeVec3Constant::get_editable_properties() const {
  202. Vector<StringName> props;
  203. props.push_back("constant");
  204. return props;
  205. }
  206. void VisualShaderNodeVec3Constant::_bind_methods() {
  207. ClassDB::bind_method(D_METHOD("set_constant", "value"), &VisualShaderNodeVec3Constant::set_constant);
  208. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeVec3Constant::get_constant);
  209. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "constant"), "set_constant", "get_constant");
  210. }
  211. VisualShaderNodeVec3Constant::VisualShaderNodeVec3Constant() {
  212. }
  213. ////////////// Transform
  214. String VisualShaderNodeTransformConstant::get_caption() const {
  215. return "Transform";
  216. }
  217. int VisualShaderNodeTransformConstant::get_input_port_count() const {
  218. return 0;
  219. }
  220. VisualShaderNodeTransformConstant::PortType VisualShaderNodeTransformConstant::get_input_port_type(int p_port) const {
  221. return PORT_TYPE_VECTOR;
  222. }
  223. String VisualShaderNodeTransformConstant::get_input_port_name(int p_port) const {
  224. return String();
  225. }
  226. int VisualShaderNodeTransformConstant::get_output_port_count() const {
  227. return 1;
  228. }
  229. VisualShaderNodeTransformConstant::PortType VisualShaderNodeTransformConstant::get_output_port_type(int p_port) const {
  230. return PORT_TYPE_TRANSFORM;
  231. }
  232. String VisualShaderNodeTransformConstant::get_output_port_name(int p_port) const {
  233. return ""; //no output port means the editor will be used as port
  234. }
  235. String VisualShaderNodeTransformConstant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  236. Transform t = constant;
  237. t.basis.transpose();
  238. String code = "\t" + p_output_vars[0] + " = mat4(";
  239. code += vformat("vec4(%.6f, %.6f, %.6f, 0.0), ", t.basis[0].x, t.basis[0].y, t.basis[0].z);
  240. code += vformat("vec4(%.6f, %.6f, %.6f, 0.0), ", t.basis[1].x, t.basis[1].y, t.basis[1].z);
  241. code += vformat("vec4(%.6f, %.6f, %.6f, 0.0), ", t.basis[2].x, t.basis[2].y, t.basis[2].z);
  242. code += vformat("vec4(%.6f, %.6f, %.6f, 1.0));\n", t.origin.x, t.origin.y, t.origin.z);
  243. return code;
  244. }
  245. void VisualShaderNodeTransformConstant::set_constant(Transform p_value) {
  246. constant = p_value;
  247. emit_changed();
  248. }
  249. Transform VisualShaderNodeTransformConstant::get_constant() const {
  250. return constant;
  251. }
  252. Vector<StringName> VisualShaderNodeTransformConstant::get_editable_properties() const {
  253. Vector<StringName> props;
  254. props.push_back("constant");
  255. return props;
  256. }
  257. void VisualShaderNodeTransformConstant::_bind_methods() {
  258. ClassDB::bind_method(D_METHOD("set_constant", "value"), &VisualShaderNodeTransformConstant::set_constant);
  259. ClassDB::bind_method(D_METHOD("get_constant"), &VisualShaderNodeTransformConstant::get_constant);
  260. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "constant"), "set_constant", "get_constant");
  261. }
  262. VisualShaderNodeTransformConstant::VisualShaderNodeTransformConstant() {
  263. }
  264. ////////////// Texture
  265. String VisualShaderNodeTexture::get_caption() const {
  266. return "Texture";
  267. }
  268. int VisualShaderNodeTexture::get_input_port_count() const {
  269. return 3;
  270. }
  271. VisualShaderNodeTexture::PortType VisualShaderNodeTexture::get_input_port_type(int p_port) const {
  272. switch (p_port) {
  273. case 0:
  274. return PORT_TYPE_VECTOR;
  275. case 1:
  276. return PORT_TYPE_SCALAR;
  277. case 2:
  278. return PORT_TYPE_SAMPLER;
  279. default:
  280. return PORT_TYPE_SCALAR;
  281. }
  282. }
  283. String VisualShaderNodeTexture::get_input_port_name(int p_port) const {
  284. switch (p_port) {
  285. case 0:
  286. return "uv";
  287. case 1:
  288. return "lod";
  289. case 2:
  290. return "sampler2D";
  291. default:
  292. return "";
  293. }
  294. }
  295. int VisualShaderNodeTexture::get_output_port_count() const {
  296. return 2;
  297. }
  298. VisualShaderNodeTexture::PortType VisualShaderNodeTexture::get_output_port_type(int p_port) const {
  299. if (p_port == 0 && source == SOURCE_DEPTH) {
  300. return PORT_TYPE_SCALAR;
  301. }
  302. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  303. }
  304. String VisualShaderNodeTexture::get_output_port_name(int p_port) const {
  305. if (p_port == 0 && source == SOURCE_DEPTH) {
  306. return "depth";
  307. }
  308. return p_port == 0 ? "rgb" : "alpha";
  309. }
  310. String VisualShaderNodeTexture::get_input_port_default_hint(int p_port) const {
  311. if (p_port == 0) {
  312. return "UV.xy";
  313. }
  314. return "";
  315. }
  316. static String make_unique_id(VisualShader::Type p_type, int p_id, const String &p_name) {
  317. static const char *typepf[VisualShader::TYPE_MAX] = { "vtx", "frg", "lgt" };
  318. return p_name + "_" + String(typepf[p_type]) + "_" + itos(p_id);
  319. }
  320. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeTexture::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  321. VisualShader::DefaultTextureParam dtp;
  322. dtp.name = make_unique_id(p_type, p_id, "tex");
  323. dtp.param = texture;
  324. Vector<VisualShader::DefaultTextureParam> ret;
  325. ret.push_back(dtp);
  326. return ret;
  327. }
  328. String VisualShaderNodeTexture::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  329. if (source == SOURCE_TEXTURE) {
  330. String u = "uniform sampler2D " + make_unique_id(p_type, p_id, "tex");
  331. switch (texture_type) {
  332. case TYPE_DATA:
  333. break;
  334. case TYPE_COLOR:
  335. u += " : hint_albedo";
  336. break;
  337. case TYPE_NORMALMAP:
  338. u += " : hint_normal";
  339. break;
  340. }
  341. return u + ";\n";
  342. }
  343. return String();
  344. }
  345. String VisualShaderNodeTexture::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  346. if (source == SOURCE_TEXTURE) {
  347. String id = make_unique_id(p_type, p_id, "tex");
  348. String code;
  349. if (p_input_vars[0] == String()) { // Use UV by default.
  350. if (p_input_vars[1] == String()) {
  351. code += "\tvec4 " + id + "_read = texture(" + id + ", UV.xy);\n";
  352. } else {
  353. code += "\tvec4 " + id + "_read = textureLod(" + id + ", UV.xy, " + p_input_vars[1] + ");\n";
  354. }
  355. } else if (p_input_vars[1] == String()) {
  356. //no lod
  357. code += "\tvec4 " + id + "_read = texture(" + id + ", " + p_input_vars[0] + ".xy);\n";
  358. } else {
  359. code += "\tvec4 " + id + "_read = textureLod(" + id + ", " + p_input_vars[0] + ".xy, " + p_input_vars[1] + ");\n";
  360. }
  361. code += "\t" + p_output_vars[0] + " = " + id + "_read.rgb;\n";
  362. code += "\t" + p_output_vars[1] + " = " + id + "_read.a;\n";
  363. return code;
  364. }
  365. if (source == SOURCE_PORT) {
  366. String id = p_input_vars[2];
  367. String code;
  368. code += "\t{\n";
  369. if (id == String()) {
  370. code += "\t\tvec4 " + id + "_tex_read = vec4(0.0);\n";
  371. } else {
  372. if (p_input_vars[0] == String()) { // Use UV by default.
  373. if (p_input_vars[1] == String()) {
  374. code += "\t\tvec4 " + id + "_tex_read = texture(" + id + ", UV.xy);\n";
  375. } else {
  376. code += "\t\tvec4 " + id + "_tex_read = textureLod(" + id + ", UV.xy, " + p_input_vars[1] + ");\n";
  377. }
  378. } else if (p_input_vars[1] == String()) {
  379. //no lod
  380. code += "\t\tvec4 " + id + "_tex_read = texture(" + id + ", " + p_input_vars[0] + ".xy);\n";
  381. } else {
  382. code += "\t\tvec4 " + id + "_tex_read = textureLod(" + id + ", " + p_input_vars[0] + ".xy, " + p_input_vars[1] + ");\n";
  383. }
  384. code += "\t\t" + p_output_vars[0] + " = " + id + "_tex_read.rgb;\n";
  385. code += "\t\t" + p_output_vars[1] + " = " + id + "_tex_read.a;\n";
  386. }
  387. code += "\t}\n";
  388. return code;
  389. }
  390. if (source == SOURCE_SCREEN && (p_mode == Shader::MODE_SPATIAL || p_mode == Shader::MODE_CANVAS_ITEM) && p_type == VisualShader::TYPE_FRAGMENT) {
  391. String code = "\t{\n";
  392. if (p_input_vars[0] == String() || p_for_preview) { // Use UV by default.
  393. if (p_input_vars[1] == String()) {
  394. code += "\t\tvec4 _tex_read = textureLod(SCREEN_TEXTURE, UV.xy, 0.0 );\n";
  395. } else {
  396. code += "\t\tvec4 _tex_read = textureLod(SCREEN_TEXTURE, UV.xy, " + p_input_vars[1] + ");\n";
  397. }
  398. } else if (p_input_vars[1] == String()) {
  399. //no lod
  400. code += "\t\tvec4 _tex_read = textureLod(SCREEN_TEXTURE, " + p_input_vars[0] + ".xy, 0.0);\n";
  401. } else {
  402. code += "\t\tvec4 _tex_read = textureLod(SCREEN_TEXTURE, " + p_input_vars[0] + ".xy, " + p_input_vars[1] + ");\n";
  403. }
  404. code += "\t\t" + p_output_vars[0] + " = _tex_read.rgb;\n";
  405. code += "\t\t" + p_output_vars[1] + " = _tex_read.a;\n";
  406. code += "\t}\n";
  407. return code;
  408. }
  409. if (source == SOURCE_2D_TEXTURE && p_mode == Shader::MODE_CANVAS_ITEM && p_type == VisualShader::TYPE_FRAGMENT) {
  410. String code = "\t{\n";
  411. if (p_input_vars[0] == String()) { // Use UV by default.
  412. if (p_input_vars[1] == String()) {
  413. code += "\t\tvec4 _tex_read = texture(TEXTURE , UV.xy);\n";
  414. } else {
  415. code += "\t\tvec4 _tex_read = textureLod(TEXTURE, UV.xy, " + p_input_vars[1] + ");\n";
  416. }
  417. } else if (p_input_vars[1] == String()) {
  418. //no lod
  419. code += "\t\tvec4 _tex_read = texture(TEXTURE, " + p_input_vars[0] + ".xy);\n";
  420. } else {
  421. code += "\t\tvec4 _tex_read = textureLod(TEXTURE, " + p_input_vars[0] + ".xy, " + p_input_vars[1] + ");\n";
  422. }
  423. code += "\t\t" + p_output_vars[0] + " = _tex_read.rgb;\n";
  424. code += "\t\t" + p_output_vars[1] + " = _tex_read.a;\n";
  425. code += "\t}\n";
  426. return code;
  427. }
  428. if (source == SOURCE_2D_NORMAL && p_mode == Shader::MODE_CANVAS_ITEM && p_type == VisualShader::TYPE_FRAGMENT) {
  429. String code = "\t{\n";
  430. if (p_input_vars[0] == String()) { // Use UV by default.
  431. if (p_input_vars[1] == String()) {
  432. code += "\t\tvec4 _tex_read = texture(NORMAL_TEXTURE, UV.xy);\n";
  433. } else {
  434. code += "\t\tvec4 _tex_read = textureLod(NORMAL_TEXTURE, UV.xy, " + p_input_vars[1] + ");\n";
  435. }
  436. } else if (p_input_vars[1] == String()) {
  437. //no lod
  438. code += "\t\tvec4 _tex_read = texture(NORMAL_TEXTURE, " + p_input_vars[0] + ".xy);\n";
  439. } else {
  440. code += "\t\tvec4 _tex_read = textureLod(NORMAL_TEXTURE, " + p_input_vars[0] + ".xy, " + p_input_vars[1] + ");\n";
  441. }
  442. code += "\t\t" + p_output_vars[0] + " = _tex_read.rgb;\n";
  443. code += "\t\t" + p_output_vars[1] + " = _tex_read.a;\n";
  444. code += "\t}\n";
  445. return code;
  446. }
  447. if (p_for_preview) // DEPTH_TEXTURE is not supported in preview(canvas_item) shader
  448. {
  449. if (source == SOURCE_DEPTH) {
  450. String code;
  451. code += "\t" + p_output_vars[0] + " = 0.0;\n";
  452. code += "\t" + p_output_vars[1] + " = 1.0;\n";
  453. return code;
  454. }
  455. }
  456. if (source == SOURCE_DEPTH && p_mode == Shader::MODE_SPATIAL && p_type == VisualShader::TYPE_FRAGMENT) {
  457. String code = "\t{\n";
  458. if (p_input_vars[0] == String()) { // Use UV by default.
  459. if (p_input_vars[1] == String()) {
  460. code += "\t\tfloat _depth = texture(DEPTH_TEXTURE, UV.xy).r;\n";
  461. } else {
  462. code += "\t\tfloat _depth = textureLod(DEPTH_TEXTURE, UV.xy, " + p_input_vars[1] + ").r;\n";
  463. }
  464. } else if (p_input_vars[1] == String()) {
  465. //no lod
  466. code += "\t\tfloat _depth = texture(DEPTH_TEXTURE, " + p_input_vars[0] + ".xy).r;\n";
  467. } else {
  468. code += "\t\tfloat _depth = textureLod(DEPTH_TEXTURE, " + p_input_vars[0] + ".xy, " + p_input_vars[1] + ").r;\n";
  469. }
  470. code += "\t\t" + p_output_vars[0] + " = _depth;\n";
  471. code += "\t\t" + p_output_vars[1] + " = 1.0;\n";
  472. code += "\t}\n";
  473. return code;
  474. } else if (source == SOURCE_DEPTH) {
  475. String code;
  476. code += "\t" + p_output_vars[0] + " = 0.0;\n";
  477. code += "\t" + p_output_vars[1] + " = 1.0;\n";
  478. return code;
  479. }
  480. //none
  481. String code;
  482. code += "\t" + p_output_vars[0] + " = vec3(0.0);\n";
  483. code += "\t" + p_output_vars[1] + " = 1.0;\n";
  484. return code;
  485. }
  486. void VisualShaderNodeTexture::set_source(Source p_source) {
  487. source = p_source;
  488. switch (source) {
  489. case SOURCE_TEXTURE:
  490. simple_decl = true;
  491. break;
  492. case SOURCE_SCREEN:
  493. simple_decl = false;
  494. break;
  495. case SOURCE_2D_TEXTURE:
  496. simple_decl = false;
  497. break;
  498. case SOURCE_2D_NORMAL:
  499. simple_decl = false;
  500. break;
  501. case SOURCE_DEPTH:
  502. simple_decl = false;
  503. break;
  504. case SOURCE_PORT:
  505. simple_decl = false;
  506. break;
  507. }
  508. emit_changed();
  509. emit_signal("editor_refresh_request");
  510. }
  511. VisualShaderNodeTexture::Source VisualShaderNodeTexture::get_source() const {
  512. return source;
  513. }
  514. void VisualShaderNodeTexture::set_texture(Ref<Texture> p_value) {
  515. texture = p_value;
  516. emit_changed();
  517. }
  518. Ref<Texture> VisualShaderNodeTexture::get_texture() const {
  519. return texture;
  520. }
  521. void VisualShaderNodeTexture::set_texture_type(TextureType p_type) {
  522. texture_type = p_type;
  523. emit_changed();
  524. }
  525. VisualShaderNodeTexture::TextureType VisualShaderNodeTexture::get_texture_type() const {
  526. return texture_type;
  527. }
  528. Vector<StringName> VisualShaderNodeTexture::get_editable_properties() const {
  529. Vector<StringName> props;
  530. props.push_back("source");
  531. if (source == SOURCE_TEXTURE) {
  532. props.push_back("texture");
  533. props.push_back("texture_type");
  534. }
  535. return props;
  536. }
  537. String VisualShaderNodeTexture::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  538. if (is_input_port_connected(2) && source != SOURCE_PORT) {
  539. return TTR("The sampler port is connected but not used. Consider changing the source to 'SamplerPort'.");
  540. }
  541. if (source == SOURCE_TEXTURE) {
  542. return String(); // all good
  543. }
  544. if (source == SOURCE_PORT) {
  545. return String(); // all good
  546. }
  547. if (source == SOURCE_SCREEN && (p_mode == Shader::MODE_SPATIAL || p_mode == Shader::MODE_CANVAS_ITEM) && p_type == VisualShader::TYPE_FRAGMENT) {
  548. return String(); // all good
  549. }
  550. if (source == SOURCE_2D_TEXTURE && p_mode == Shader::MODE_CANVAS_ITEM && p_type == VisualShader::TYPE_FRAGMENT) {
  551. return String(); // all good
  552. }
  553. if (source == SOURCE_2D_NORMAL && p_mode == Shader::MODE_CANVAS_ITEM) {
  554. return String(); // all good
  555. }
  556. if (source == SOURCE_DEPTH && p_mode == Shader::MODE_SPATIAL && p_type == VisualShader::TYPE_FRAGMENT) {
  557. if (get_output_port_for_preview() == 0) { // DEPTH_TEXTURE is not supported in preview(canvas_item) shader
  558. return TTR("Invalid source for preview.");
  559. }
  560. return String(); // all good
  561. }
  562. return TTR("Invalid source for shader.");
  563. }
  564. void VisualShaderNodeTexture::_bind_methods() {
  565. ClassDB::bind_method(D_METHOD("set_source", "value"), &VisualShaderNodeTexture::set_source);
  566. ClassDB::bind_method(D_METHOD("get_source"), &VisualShaderNodeTexture::get_source);
  567. ClassDB::bind_method(D_METHOD("set_texture", "value"), &VisualShaderNodeTexture::set_texture);
  568. ClassDB::bind_method(D_METHOD("get_texture"), &VisualShaderNodeTexture::get_texture);
  569. ClassDB::bind_method(D_METHOD("set_texture_type", "value"), &VisualShaderNodeTexture::set_texture_type);
  570. ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeTexture::get_texture_type);
  571. ADD_PROPERTY(PropertyInfo(Variant::INT, "source", PROPERTY_HINT_ENUM, "Texture,Screen,Texture2D,NormalMap2D,Depth,SamplerPort"), "set_source", "get_source");
  572. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "texture", PROPERTY_HINT_RESOURCE_TYPE, "Texture"), "set_texture", "get_texture");
  573. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normalmap"), "set_texture_type", "get_texture_type");
  574. BIND_ENUM_CONSTANT(SOURCE_TEXTURE);
  575. BIND_ENUM_CONSTANT(SOURCE_SCREEN);
  576. BIND_ENUM_CONSTANT(SOURCE_2D_TEXTURE);
  577. BIND_ENUM_CONSTANT(SOURCE_2D_NORMAL);
  578. BIND_ENUM_CONSTANT(SOURCE_DEPTH);
  579. BIND_ENUM_CONSTANT(SOURCE_PORT);
  580. BIND_ENUM_CONSTANT(TYPE_DATA);
  581. BIND_ENUM_CONSTANT(TYPE_COLOR);
  582. BIND_ENUM_CONSTANT(TYPE_NORMALMAP);
  583. }
  584. VisualShaderNodeTexture::VisualShaderNodeTexture() {
  585. texture_type = TYPE_DATA;
  586. source = SOURCE_TEXTURE;
  587. }
  588. ////////////// CubeMap
  589. String VisualShaderNodeCubeMap::get_caption() const {
  590. return "CubeMap";
  591. }
  592. int VisualShaderNodeCubeMap::get_input_port_count() const {
  593. return 3;
  594. }
  595. VisualShaderNodeCubeMap::PortType VisualShaderNodeCubeMap::get_input_port_type(int p_port) const {
  596. switch (p_port) {
  597. case 0:
  598. return PORT_TYPE_VECTOR;
  599. case 1:
  600. return PORT_TYPE_SCALAR;
  601. case 2:
  602. return PORT_TYPE_SAMPLER;
  603. default:
  604. return PORT_TYPE_SCALAR;
  605. }
  606. }
  607. String VisualShaderNodeCubeMap::get_input_port_name(int p_port) const {
  608. switch (p_port) {
  609. case 0:
  610. return "uv";
  611. case 1:
  612. return "lod";
  613. case 2:
  614. return "samplerCube";
  615. default:
  616. return "";
  617. }
  618. }
  619. int VisualShaderNodeCubeMap::get_output_port_count() const {
  620. return 2;
  621. }
  622. VisualShaderNodeCubeMap::PortType VisualShaderNodeCubeMap::get_output_port_type(int p_port) const {
  623. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  624. }
  625. String VisualShaderNodeCubeMap::get_output_port_name(int p_port) const {
  626. return p_port == 0 ? "rgb" : "alpha";
  627. }
  628. Vector<VisualShader::DefaultTextureParam> VisualShaderNodeCubeMap::get_default_texture_parameters(VisualShader::Type p_type, int p_id) const {
  629. VisualShader::DefaultTextureParam dtp;
  630. dtp.name = make_unique_id(p_type, p_id, "cube");
  631. dtp.param = cube_map;
  632. Vector<VisualShader::DefaultTextureParam> ret;
  633. ret.push_back(dtp);
  634. return ret;
  635. }
  636. String VisualShaderNodeCubeMap::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  637. if (source == SOURCE_TEXTURE) {
  638. String u = "uniform samplerCube " + make_unique_id(p_type, p_id, "cube");
  639. switch (texture_type) {
  640. case TYPE_DATA:
  641. break;
  642. case TYPE_COLOR:
  643. u += " : hint_albedo";
  644. break;
  645. case TYPE_NORMALMAP:
  646. u += " : hint_normal";
  647. break;
  648. }
  649. return u + ";\n";
  650. }
  651. return String();
  652. }
  653. String VisualShaderNodeCubeMap::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  654. String code;
  655. String id;
  656. if (source == SOURCE_TEXTURE) {
  657. id = make_unique_id(p_type, p_id, "cube");
  658. } else if (source == SOURCE_PORT) {
  659. id = p_input_vars[2];
  660. } else {
  661. return String();
  662. }
  663. code += "\t{\n";
  664. if (id == String()) {
  665. code += "\t\tvec4 " + id + "_read = vec4(0.0);\n";
  666. code += "\t\t" + p_output_vars[0] + " = " + id + "_read.rgb;\n";
  667. code += "\t\t" + p_output_vars[1] + " = " + id + "_read.a;\n";
  668. code += "\t}\n";
  669. return code;
  670. }
  671. if (p_input_vars[0] == String()) { // Use UV by default.
  672. if (p_input_vars[1] == String()) {
  673. code += "\t\tvec4 " + id + "_read = texture(" + id + " , vec3(UV, 0.0));\n";
  674. } else {
  675. code += "\t\tvec4 " + id + "_read = textureLod(" + id + " , vec3(UV, 0.0)" + " , " + p_input_vars[1] + " );\n";
  676. }
  677. } else if (p_input_vars[1] == String()) {
  678. //no lod
  679. code += "\t\tvec4 " + id + "_read = texture(" + id + ", " + p_input_vars[0] + ");\n";
  680. } else {
  681. code += "\t\tvec4 " + id + "_read = textureLod(" + id + ", " + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  682. }
  683. code += "\t\t" + p_output_vars[0] + " = " + id + "_read.rgb;\n";
  684. code += "\t\t" + p_output_vars[1] + " = " + id + "_read.a;\n";
  685. code += "\t}\n";
  686. return code;
  687. }
  688. String VisualShaderNodeCubeMap::get_input_port_default_hint(int p_port) const {
  689. if (p_port == 0) {
  690. return "vec3(UV, 0.0)";
  691. }
  692. return "";
  693. }
  694. void VisualShaderNodeCubeMap::set_source(Source p_source) {
  695. source = p_source;
  696. emit_changed();
  697. emit_signal("editor_refresh_request");
  698. }
  699. VisualShaderNodeCubeMap::Source VisualShaderNodeCubeMap::get_source() const {
  700. return source;
  701. }
  702. void VisualShaderNodeCubeMap::set_cube_map(Ref<CubeMap> p_value) {
  703. cube_map = p_value;
  704. emit_changed();
  705. }
  706. Ref<CubeMap> VisualShaderNodeCubeMap::get_cube_map() const {
  707. return cube_map;
  708. }
  709. void VisualShaderNodeCubeMap::set_texture_type(TextureType p_type) {
  710. texture_type = p_type;
  711. emit_changed();
  712. }
  713. VisualShaderNodeCubeMap::TextureType VisualShaderNodeCubeMap::get_texture_type() const {
  714. return texture_type;
  715. }
  716. Vector<StringName> VisualShaderNodeCubeMap::get_editable_properties() const {
  717. Vector<StringName> props;
  718. props.push_back("source");
  719. if (source == SOURCE_TEXTURE) {
  720. props.push_back("cube_map");
  721. props.push_back("texture_type");
  722. }
  723. return props;
  724. }
  725. String VisualShaderNodeCubeMap::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  726. if (is_input_port_connected(2) && source != SOURCE_PORT) {
  727. return TTR("The sampler port is connected but not used. Consider changing the source to 'SamplerPort'.");
  728. }
  729. return String();
  730. }
  731. void VisualShaderNodeCubeMap::_bind_methods() {
  732. ClassDB::bind_method(D_METHOD("set_source", "value"), &VisualShaderNodeCubeMap::set_source);
  733. ClassDB::bind_method(D_METHOD("get_source"), &VisualShaderNodeCubeMap::get_source);
  734. ClassDB::bind_method(D_METHOD("set_cube_map", "value"), &VisualShaderNodeCubeMap::set_cube_map);
  735. ClassDB::bind_method(D_METHOD("get_cube_map"), &VisualShaderNodeCubeMap::get_cube_map);
  736. ClassDB::bind_method(D_METHOD("set_texture_type", "value"), &VisualShaderNodeCubeMap::set_texture_type);
  737. ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeCubeMap::get_texture_type);
  738. ADD_PROPERTY(PropertyInfo(Variant::INT, "source", PROPERTY_HINT_ENUM, "Texture,SamplerPort"), "set_source", "get_source");
  739. ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "cube_map", PROPERTY_HINT_RESOURCE_TYPE, "CubeMap"), "set_cube_map", "get_cube_map");
  740. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normalmap"), "set_texture_type", "get_texture_type");
  741. BIND_ENUM_CONSTANT(SOURCE_TEXTURE);
  742. BIND_ENUM_CONSTANT(SOURCE_PORT);
  743. BIND_ENUM_CONSTANT(TYPE_DATA);
  744. BIND_ENUM_CONSTANT(TYPE_COLOR);
  745. BIND_ENUM_CONSTANT(TYPE_NORMALMAP);
  746. }
  747. VisualShaderNodeCubeMap::VisualShaderNodeCubeMap() {
  748. texture_type = TYPE_DATA;
  749. source = SOURCE_TEXTURE;
  750. simple_decl = false;
  751. }
  752. ////////////// Scalar Op
  753. String VisualShaderNodeScalarOp::get_caption() const {
  754. return "ScalarOp";
  755. }
  756. int VisualShaderNodeScalarOp::get_input_port_count() const {
  757. return 2;
  758. }
  759. VisualShaderNodeScalarOp::PortType VisualShaderNodeScalarOp::get_input_port_type(int p_port) const {
  760. return PORT_TYPE_SCALAR;
  761. }
  762. String VisualShaderNodeScalarOp::get_input_port_name(int p_port) const {
  763. return p_port == 0 ? "a" : "b";
  764. }
  765. int VisualShaderNodeScalarOp::get_output_port_count() const {
  766. return 1;
  767. }
  768. VisualShaderNodeScalarOp::PortType VisualShaderNodeScalarOp::get_output_port_type(int p_port) const {
  769. return PORT_TYPE_SCALAR;
  770. }
  771. String VisualShaderNodeScalarOp::get_output_port_name(int p_port) const {
  772. return "op"; //no output port means the editor will be used as port
  773. }
  774. String VisualShaderNodeScalarOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  775. String code = "\t" + p_output_vars[0] + " = ";
  776. switch (op) {
  777. case OP_ADD:
  778. code += p_input_vars[0] + " + " + p_input_vars[1] + ";\n";
  779. break;
  780. case OP_SUB:
  781. code += p_input_vars[0] + " - " + p_input_vars[1] + ";\n";
  782. break;
  783. case OP_MUL:
  784. code += p_input_vars[0] + " * " + p_input_vars[1] + ";\n";
  785. break;
  786. case OP_DIV:
  787. code += p_input_vars[0] + " / " + p_input_vars[1] + ";\n";
  788. break;
  789. case OP_MOD:
  790. code += "mod(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  791. break;
  792. case OP_POW:
  793. code += "pow(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  794. break;
  795. case OP_MAX:
  796. code += "max(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  797. break;
  798. case OP_MIN:
  799. code += "min(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  800. break;
  801. case OP_ATAN2:
  802. code += "atan(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  803. break;
  804. case OP_STEP:
  805. code += "step(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  806. break;
  807. }
  808. return code;
  809. }
  810. void VisualShaderNodeScalarOp::set_operator(Operator p_op) {
  811. op = p_op;
  812. emit_changed();
  813. }
  814. VisualShaderNodeScalarOp::Operator VisualShaderNodeScalarOp::get_operator() const {
  815. return op;
  816. }
  817. Vector<StringName> VisualShaderNodeScalarOp::get_editable_properties() const {
  818. Vector<StringName> props;
  819. props.push_back("operator");
  820. return props;
  821. }
  822. void VisualShaderNodeScalarOp::_bind_methods() {
  823. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeScalarOp::set_operator);
  824. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeScalarOp::get_operator);
  825. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Add,Sub,Multiply,Divide,Remainder,Power,Max,Min,Atan2,Step"), "set_operator", "get_operator");
  826. BIND_ENUM_CONSTANT(OP_ADD);
  827. BIND_ENUM_CONSTANT(OP_SUB);
  828. BIND_ENUM_CONSTANT(OP_MUL);
  829. BIND_ENUM_CONSTANT(OP_DIV);
  830. BIND_ENUM_CONSTANT(OP_MOD);
  831. BIND_ENUM_CONSTANT(OP_POW);
  832. BIND_ENUM_CONSTANT(OP_MAX);
  833. BIND_ENUM_CONSTANT(OP_MIN);
  834. BIND_ENUM_CONSTANT(OP_ATAN2);
  835. BIND_ENUM_CONSTANT(OP_STEP);
  836. }
  837. VisualShaderNodeScalarOp::VisualShaderNodeScalarOp() {
  838. op = OP_ADD;
  839. set_input_port_default_value(0, 0.0);
  840. set_input_port_default_value(1, 0.0);
  841. }
  842. ////////////// Vector Op
  843. String VisualShaderNodeVectorOp::get_caption() const {
  844. return "VectorOp";
  845. }
  846. int VisualShaderNodeVectorOp::get_input_port_count() const {
  847. return 2;
  848. }
  849. VisualShaderNodeVectorOp::PortType VisualShaderNodeVectorOp::get_input_port_type(int p_port) const {
  850. return PORT_TYPE_VECTOR;
  851. }
  852. String VisualShaderNodeVectorOp::get_input_port_name(int p_port) const {
  853. return p_port == 0 ? "a" : "b";
  854. }
  855. int VisualShaderNodeVectorOp::get_output_port_count() const {
  856. return 1;
  857. }
  858. VisualShaderNodeVectorOp::PortType VisualShaderNodeVectorOp::get_output_port_type(int p_port) const {
  859. return PORT_TYPE_VECTOR;
  860. }
  861. String VisualShaderNodeVectorOp::get_output_port_name(int p_port) const {
  862. return "op"; //no output port means the editor will be used as port
  863. }
  864. String VisualShaderNodeVectorOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  865. String code = "\t" + p_output_vars[0] + " = ";
  866. switch (op) {
  867. case OP_ADD:
  868. code += p_input_vars[0] + " + " + p_input_vars[1] + ";\n";
  869. break;
  870. case OP_SUB:
  871. code += p_input_vars[0] + " - " + p_input_vars[1] + ";\n";
  872. break;
  873. case OP_MUL:
  874. code += p_input_vars[0] + " * " + p_input_vars[1] + ";\n";
  875. break;
  876. case OP_DIV:
  877. code += p_input_vars[0] + " / " + p_input_vars[1] + ";\n";
  878. break;
  879. case OP_MOD:
  880. code += "mod(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  881. break;
  882. case OP_POW:
  883. code += "pow(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  884. break;
  885. case OP_MAX:
  886. code += "max(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  887. break;
  888. case OP_MIN:
  889. code += "min(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  890. break;
  891. case OP_CROSS:
  892. code += "cross(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  893. break;
  894. case OP_ATAN2:
  895. code += "atan(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  896. break;
  897. case OP_REFLECT:
  898. code += "reflect(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  899. break;
  900. case OP_STEP:
  901. code += "step(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  902. break;
  903. }
  904. return code;
  905. }
  906. void VisualShaderNodeVectorOp::set_operator(Operator p_op) {
  907. op = p_op;
  908. emit_changed();
  909. }
  910. VisualShaderNodeVectorOp::Operator VisualShaderNodeVectorOp::get_operator() const {
  911. return op;
  912. }
  913. Vector<StringName> VisualShaderNodeVectorOp::get_editable_properties() const {
  914. Vector<StringName> props;
  915. props.push_back("operator");
  916. return props;
  917. }
  918. void VisualShaderNodeVectorOp::_bind_methods() {
  919. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeVectorOp::set_operator);
  920. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeVectorOp::get_operator);
  921. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Add,Sub,Multiply,Divide,Remainder,Power,Max,Min,Cross,Atan2,Reflect,Step"), "set_operator", "get_operator");
  922. BIND_ENUM_CONSTANT(OP_ADD);
  923. BIND_ENUM_CONSTANT(OP_SUB);
  924. BIND_ENUM_CONSTANT(OP_MUL);
  925. BIND_ENUM_CONSTANT(OP_DIV);
  926. BIND_ENUM_CONSTANT(OP_MOD);
  927. BIND_ENUM_CONSTANT(OP_POW);
  928. BIND_ENUM_CONSTANT(OP_MAX);
  929. BIND_ENUM_CONSTANT(OP_MIN);
  930. BIND_ENUM_CONSTANT(OP_CROSS);
  931. BIND_ENUM_CONSTANT(OP_ATAN2);
  932. BIND_ENUM_CONSTANT(OP_REFLECT);
  933. BIND_ENUM_CONSTANT(OP_STEP);
  934. }
  935. VisualShaderNodeVectorOp::VisualShaderNodeVectorOp() {
  936. op = OP_ADD;
  937. set_input_port_default_value(0, Vector3());
  938. set_input_port_default_value(1, Vector3());
  939. }
  940. ////////////// Color Op
  941. String VisualShaderNodeColorOp::get_caption() const {
  942. return "ColorOp";
  943. }
  944. int VisualShaderNodeColorOp::get_input_port_count() const {
  945. return 2;
  946. }
  947. VisualShaderNodeColorOp::PortType VisualShaderNodeColorOp::get_input_port_type(int p_port) const {
  948. return PORT_TYPE_VECTOR;
  949. }
  950. String VisualShaderNodeColorOp::get_input_port_name(int p_port) const {
  951. return p_port == 0 ? "a" : "b";
  952. }
  953. int VisualShaderNodeColorOp::get_output_port_count() const {
  954. return 1;
  955. }
  956. VisualShaderNodeColorOp::PortType VisualShaderNodeColorOp::get_output_port_type(int p_port) const {
  957. return PORT_TYPE_VECTOR;
  958. }
  959. String VisualShaderNodeColorOp::get_output_port_name(int p_port) const {
  960. return "op"; //no output port means the editor will be used as port
  961. }
  962. String VisualShaderNodeColorOp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  963. String code;
  964. static const char *axisn[3] = { "x", "y", "z" };
  965. switch (op) {
  966. case OP_SCREEN: {
  967. code += "\t" + p_output_vars[0] + " = vec3(1.0) - (vec3(1.0) - " + p_input_vars[0] + ") * (vec3(1.0) - " + p_input_vars[1] + ");\n";
  968. } break;
  969. case OP_DIFFERENCE: {
  970. code += "\t" + p_output_vars[0] + " = abs(" + p_input_vars[0] + " - " + p_input_vars[1] + ");\n";
  971. } break;
  972. case OP_DARKEN: {
  973. code += "\t" + p_output_vars[0] + " = min(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  974. } break;
  975. case OP_LIGHTEN: {
  976. code += "\t" + p_output_vars[0] + " = max(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  977. } break;
  978. case OP_OVERLAY: {
  979. for (int i = 0; i < 3; i++) {
  980. code += "\t{\n";
  981. code += "\t\tfloat base = " + p_input_vars[0] + "." + axisn[i] + ";\n";
  982. code += "\t\tfloat blend = " + p_input_vars[1] + "." + axisn[i] + ";\n";
  983. code += "\t\tif (base < 0.5) {\n";
  984. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = 2.0 * base * blend;\n";
  985. code += "\t\t} else {\n";
  986. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = 1.0 - 2.0 * (1.0 - blend) * (1.0 - base);\n";
  987. code += "\t\t}\n";
  988. code += "\t}\n";
  989. }
  990. } break;
  991. case OP_DODGE: {
  992. code += "\t" + p_output_vars[0] + " = (" + p_input_vars[0] + ") / (vec3(1.0) - " + p_input_vars[1] + ");\n";
  993. } break;
  994. case OP_BURN: {
  995. code += "\t" + p_output_vars[0] + " = vec3(1.0) - (vec3(1.0) - " + p_input_vars[0] + ") / (" + p_input_vars[1] + ");\n";
  996. } break;
  997. case OP_SOFT_LIGHT: {
  998. for (int i = 0; i < 3; i++) {
  999. code += "\t{\n";
  1000. code += "\t\tfloat base = " + p_input_vars[0] + "." + axisn[i] + ";\n";
  1001. code += "\t\tfloat blend = " + p_input_vars[1] + "." + axisn[i] + ";\n";
  1002. code += "\t\tif (base < 0.5) {\n";
  1003. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = (base * (blend + 0.5));\n";
  1004. code += "\t\t} else {\n";
  1005. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = (1.0 - (1.0 - base) * (1.0 - (blend - 0.5)));\n";
  1006. code += "\t\t}\n";
  1007. code += "\t}\n";
  1008. }
  1009. } break;
  1010. case OP_HARD_LIGHT: {
  1011. for (int i = 0; i < 3; i++) {
  1012. code += "\t{\n";
  1013. code += "\t\tfloat base = " + p_input_vars[0] + "." + axisn[i] + ";\n";
  1014. code += "\t\tfloat blend = " + p_input_vars[1] + "." + axisn[i] + ";\n";
  1015. code += "\t\tif (base < 0.5) {\n";
  1016. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = (base * (2.0 * blend));\n";
  1017. code += "\t\t} else {\n";
  1018. code += "\t\t\t" + p_output_vars[0] + "." + axisn[i] + " = (1.0 - (1.0 - base) * (1.0 - 2.0 * (blend - 0.5)));\n";
  1019. code += "\t\t}\n";
  1020. code += "\t}\n";
  1021. }
  1022. } break;
  1023. }
  1024. return code;
  1025. }
  1026. void VisualShaderNodeColorOp::set_operator(Operator p_op) {
  1027. op = p_op;
  1028. switch (op) {
  1029. case OP_SCREEN:
  1030. simple_decl = true;
  1031. break;
  1032. case OP_DIFFERENCE:
  1033. simple_decl = true;
  1034. break;
  1035. case OP_DARKEN:
  1036. simple_decl = true;
  1037. break;
  1038. case OP_LIGHTEN:
  1039. simple_decl = true;
  1040. break;
  1041. case OP_OVERLAY:
  1042. simple_decl = false;
  1043. break;
  1044. case OP_DODGE:
  1045. simple_decl = true;
  1046. break;
  1047. case OP_BURN:
  1048. simple_decl = true;
  1049. break;
  1050. case OP_SOFT_LIGHT:
  1051. simple_decl = false;
  1052. break;
  1053. case OP_HARD_LIGHT:
  1054. simple_decl = false;
  1055. break;
  1056. }
  1057. emit_changed();
  1058. }
  1059. VisualShaderNodeColorOp::Operator VisualShaderNodeColorOp::get_operator() const {
  1060. return op;
  1061. }
  1062. Vector<StringName> VisualShaderNodeColorOp::get_editable_properties() const {
  1063. Vector<StringName> props;
  1064. props.push_back("operator");
  1065. return props;
  1066. }
  1067. void VisualShaderNodeColorOp::_bind_methods() {
  1068. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeColorOp::set_operator);
  1069. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeColorOp::get_operator);
  1070. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "Screen,Difference,Darken,Lighten,Overlay,Dodge,Burn,SoftLight,HardLight"), "set_operator", "get_operator");
  1071. BIND_ENUM_CONSTANT(OP_SCREEN);
  1072. BIND_ENUM_CONSTANT(OP_DIFFERENCE);
  1073. BIND_ENUM_CONSTANT(OP_DARKEN);
  1074. BIND_ENUM_CONSTANT(OP_LIGHTEN);
  1075. BIND_ENUM_CONSTANT(OP_OVERLAY);
  1076. BIND_ENUM_CONSTANT(OP_DODGE);
  1077. BIND_ENUM_CONSTANT(OP_BURN);
  1078. BIND_ENUM_CONSTANT(OP_SOFT_LIGHT);
  1079. BIND_ENUM_CONSTANT(OP_HARD_LIGHT);
  1080. }
  1081. VisualShaderNodeColorOp::VisualShaderNodeColorOp() {
  1082. op = OP_SCREEN;
  1083. set_input_port_default_value(0, Vector3());
  1084. set_input_port_default_value(1, Vector3());
  1085. }
  1086. ////////////// Transform Mult
  1087. String VisualShaderNodeTransformMult::get_caption() const {
  1088. return "TransformMult";
  1089. }
  1090. int VisualShaderNodeTransformMult::get_input_port_count() const {
  1091. return 2;
  1092. }
  1093. VisualShaderNodeTransformMult::PortType VisualShaderNodeTransformMult::get_input_port_type(int p_port) const {
  1094. return PORT_TYPE_TRANSFORM;
  1095. }
  1096. String VisualShaderNodeTransformMult::get_input_port_name(int p_port) const {
  1097. return p_port == 0 ? "a" : "b";
  1098. }
  1099. int VisualShaderNodeTransformMult::get_output_port_count() const {
  1100. return 1;
  1101. }
  1102. VisualShaderNodeTransformMult::PortType VisualShaderNodeTransformMult::get_output_port_type(int p_port) const {
  1103. return PORT_TYPE_TRANSFORM;
  1104. }
  1105. String VisualShaderNodeTransformMult::get_output_port_name(int p_port) const {
  1106. return "mult"; //no output port means the editor will be used as port
  1107. }
  1108. String VisualShaderNodeTransformMult::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1109. if (op == OP_AxB) {
  1110. return "\t" + p_output_vars[0] + " = " + p_input_vars[0] + " * " + p_input_vars[1] + ";\n";
  1111. } else if (op == OP_BxA) {
  1112. return "\t" + p_output_vars[0] + " = " + p_input_vars[1] + " * " + p_input_vars[0] + ";\n";
  1113. } else if (op == OP_AxB_COMP) {
  1114. return "\t" + p_output_vars[0] + " = matrixCompMult(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1115. } else {
  1116. return "\t" + p_output_vars[0] + " = matrixCompMult(" + p_input_vars[1] + ", " + p_input_vars[0] + ");\n";
  1117. }
  1118. }
  1119. void VisualShaderNodeTransformMult::set_operator(Operator p_op) {
  1120. op = p_op;
  1121. emit_changed();
  1122. }
  1123. VisualShaderNodeTransformMult::Operator VisualShaderNodeTransformMult::get_operator() const {
  1124. return op;
  1125. }
  1126. Vector<StringName> VisualShaderNodeTransformMult::get_editable_properties() const {
  1127. Vector<StringName> props;
  1128. props.push_back("operator");
  1129. return props;
  1130. }
  1131. void VisualShaderNodeTransformMult::_bind_methods() {
  1132. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeTransformMult::set_operator);
  1133. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeTransformMult::get_operator);
  1134. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "A x B,B x A,A x B(per component),B x A(per component)"), "set_operator", "get_operator");
  1135. BIND_ENUM_CONSTANT(OP_AxB);
  1136. BIND_ENUM_CONSTANT(OP_BxA);
  1137. BIND_ENUM_CONSTANT(OP_AxB_COMP);
  1138. BIND_ENUM_CONSTANT(OP_BxA_COMP);
  1139. }
  1140. VisualShaderNodeTransformMult::VisualShaderNodeTransformMult() {
  1141. op = OP_AxB;
  1142. set_input_port_default_value(0, Transform());
  1143. set_input_port_default_value(1, Transform());
  1144. }
  1145. ////////////// TransformVec Mult
  1146. String VisualShaderNodeTransformVecMult::get_caption() const {
  1147. return "TransformVectorMult";
  1148. }
  1149. int VisualShaderNodeTransformVecMult::get_input_port_count() const {
  1150. return 2;
  1151. }
  1152. VisualShaderNodeTransformVecMult::PortType VisualShaderNodeTransformVecMult::get_input_port_type(int p_port) const {
  1153. return p_port == 0 ? PORT_TYPE_TRANSFORM : PORT_TYPE_VECTOR;
  1154. }
  1155. String VisualShaderNodeTransformVecMult::get_input_port_name(int p_port) const {
  1156. return p_port == 0 ? "a" : "b";
  1157. }
  1158. int VisualShaderNodeTransformVecMult::get_output_port_count() const {
  1159. return 1;
  1160. }
  1161. VisualShaderNodeTransformVecMult::PortType VisualShaderNodeTransformVecMult::get_output_port_type(int p_port) const {
  1162. return PORT_TYPE_VECTOR;
  1163. }
  1164. String VisualShaderNodeTransformVecMult::get_output_port_name(int p_port) const {
  1165. return ""; //no output port means the editor will be used as port
  1166. }
  1167. String VisualShaderNodeTransformVecMult::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1168. if (op == OP_AxB) {
  1169. return "\t" + p_output_vars[0] + " = (" + p_input_vars[0] + " * vec4(" + p_input_vars[1] + ", 1.0)).xyz;\n";
  1170. } else if (op == OP_BxA) {
  1171. return "\t" + p_output_vars[0] + " = (vec4(" + p_input_vars[1] + ", 1.0) * " + p_input_vars[0] + ").xyz;\n";
  1172. } else if (op == OP_3x3_AxB) {
  1173. return "\t" + p_output_vars[0] + " = (" + p_input_vars[0] + " * vec4(" + p_input_vars[1] + ", 0.0)).xyz;\n";
  1174. } else {
  1175. return "\t" + p_output_vars[0] + " = (vec4(" + p_input_vars[1] + ", 0.0) * " + p_input_vars[0] + ").xyz;\n";
  1176. }
  1177. }
  1178. void VisualShaderNodeTransformVecMult::set_operator(Operator p_op) {
  1179. op = p_op;
  1180. emit_changed();
  1181. }
  1182. VisualShaderNodeTransformVecMult::Operator VisualShaderNodeTransformVecMult::get_operator() const {
  1183. return op;
  1184. }
  1185. Vector<StringName> VisualShaderNodeTransformVecMult::get_editable_properties() const {
  1186. Vector<StringName> props;
  1187. props.push_back("operator");
  1188. return props;
  1189. }
  1190. void VisualShaderNodeTransformVecMult::_bind_methods() {
  1191. ClassDB::bind_method(D_METHOD("set_operator", "op"), &VisualShaderNodeTransformVecMult::set_operator);
  1192. ClassDB::bind_method(D_METHOD("get_operator"), &VisualShaderNodeTransformVecMult::get_operator);
  1193. ADD_PROPERTY(PropertyInfo(Variant::INT, "operator", PROPERTY_HINT_ENUM, "A x B,B x A,A x B (3x3),B x A (3x3)"), "set_operator", "get_operator");
  1194. BIND_ENUM_CONSTANT(OP_AxB);
  1195. BIND_ENUM_CONSTANT(OP_BxA);
  1196. BIND_ENUM_CONSTANT(OP_3x3_AxB);
  1197. BIND_ENUM_CONSTANT(OP_3x3_BxA);
  1198. }
  1199. VisualShaderNodeTransformVecMult::VisualShaderNodeTransformVecMult() {
  1200. op = OP_AxB;
  1201. set_input_port_default_value(0, Transform());
  1202. set_input_port_default_value(1, Vector3());
  1203. }
  1204. ////////////// Scalar Func
  1205. String VisualShaderNodeScalarFunc::get_caption() const {
  1206. return "ScalarFunc";
  1207. }
  1208. int VisualShaderNodeScalarFunc::get_input_port_count() const {
  1209. return 1;
  1210. }
  1211. VisualShaderNodeScalarFunc::PortType VisualShaderNodeScalarFunc::get_input_port_type(int p_port) const {
  1212. return PORT_TYPE_SCALAR;
  1213. }
  1214. String VisualShaderNodeScalarFunc::get_input_port_name(int p_port) const {
  1215. return "";
  1216. }
  1217. int VisualShaderNodeScalarFunc::get_output_port_count() const {
  1218. return 1;
  1219. }
  1220. VisualShaderNodeScalarFunc::PortType VisualShaderNodeScalarFunc::get_output_port_type(int p_port) const {
  1221. return PORT_TYPE_SCALAR;
  1222. }
  1223. String VisualShaderNodeScalarFunc::get_output_port_name(int p_port) const {
  1224. return ""; //no output port means the editor will be used as port
  1225. }
  1226. String VisualShaderNodeScalarFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1227. static const char *scalar_func_id[FUNC_ONEMINUS + 1] = {
  1228. "sin($)",
  1229. "cos($)",
  1230. "tan($)",
  1231. "asin($)",
  1232. "acos($)",
  1233. "atan($)",
  1234. "sinh($)",
  1235. "cosh($)",
  1236. "tanh($)",
  1237. "log($)",
  1238. "exp($)",
  1239. "sqrt($)",
  1240. "abs($)",
  1241. "sign($)",
  1242. "floor($)",
  1243. "round($)",
  1244. "ceil($)",
  1245. "fract($)",
  1246. "min(max($, 0.0), 1.0)",
  1247. "-($)",
  1248. "acosh($)",
  1249. "asinh($)",
  1250. "atanh($)",
  1251. "degrees($)",
  1252. "exp2($)",
  1253. "inversesqrt($)",
  1254. "log2($)",
  1255. "radians($)",
  1256. "1.0 / ($)",
  1257. "roundEven($)",
  1258. "trunc($)",
  1259. "1.0 - $"
  1260. };
  1261. return "\t" + p_output_vars[0] + " = " + String(scalar_func_id[func]).replace("$", p_input_vars[0]) + ";\n";
  1262. }
  1263. void VisualShaderNodeScalarFunc::set_function(Function p_func) {
  1264. func = p_func;
  1265. emit_changed();
  1266. }
  1267. VisualShaderNodeScalarFunc::Function VisualShaderNodeScalarFunc::get_function() const {
  1268. return func;
  1269. }
  1270. Vector<StringName> VisualShaderNodeScalarFunc::get_editable_properties() const {
  1271. Vector<StringName> props;
  1272. props.push_back("function");
  1273. return props;
  1274. }
  1275. void VisualShaderNodeScalarFunc::_bind_methods() {
  1276. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeScalarFunc::set_function);
  1277. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeScalarFunc::get_function);
  1278. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Sin,Cos,Tan,ASin,ACos,ATan,SinH,CosH,TanH,Log,Exp,Sqrt,Abs,Sign,Floor,Round,Ceil,Frac,Saturate,Negate,ACosH,ASinH,ATanH,Degrees,Exp2,InverseSqrt,Log2,Radians,Reciprocal,RoundEven,Trunc,OneMinus"), "set_function", "get_function");
  1279. BIND_ENUM_CONSTANT(FUNC_SIN);
  1280. BIND_ENUM_CONSTANT(FUNC_COS);
  1281. BIND_ENUM_CONSTANT(FUNC_TAN);
  1282. BIND_ENUM_CONSTANT(FUNC_ASIN);
  1283. BIND_ENUM_CONSTANT(FUNC_ACOS);
  1284. BIND_ENUM_CONSTANT(FUNC_ATAN);
  1285. BIND_ENUM_CONSTANT(FUNC_SINH);
  1286. BIND_ENUM_CONSTANT(FUNC_COSH);
  1287. BIND_ENUM_CONSTANT(FUNC_TANH);
  1288. BIND_ENUM_CONSTANT(FUNC_LOG);
  1289. BIND_ENUM_CONSTANT(FUNC_EXP);
  1290. BIND_ENUM_CONSTANT(FUNC_SQRT);
  1291. BIND_ENUM_CONSTANT(FUNC_ABS);
  1292. BIND_ENUM_CONSTANT(FUNC_SIGN);
  1293. BIND_ENUM_CONSTANT(FUNC_FLOOR);
  1294. BIND_ENUM_CONSTANT(FUNC_ROUND);
  1295. BIND_ENUM_CONSTANT(FUNC_CEIL);
  1296. BIND_ENUM_CONSTANT(FUNC_FRAC);
  1297. BIND_ENUM_CONSTANT(FUNC_SATURATE);
  1298. BIND_ENUM_CONSTANT(FUNC_NEGATE);
  1299. BIND_ENUM_CONSTANT(FUNC_ACOSH);
  1300. BIND_ENUM_CONSTANT(FUNC_ASINH);
  1301. BIND_ENUM_CONSTANT(FUNC_ATANH);
  1302. BIND_ENUM_CONSTANT(FUNC_DEGREES);
  1303. BIND_ENUM_CONSTANT(FUNC_EXP2);
  1304. BIND_ENUM_CONSTANT(FUNC_INVERSE_SQRT);
  1305. BIND_ENUM_CONSTANT(FUNC_LOG2);
  1306. BIND_ENUM_CONSTANT(FUNC_RADIANS);
  1307. BIND_ENUM_CONSTANT(FUNC_RECIPROCAL);
  1308. BIND_ENUM_CONSTANT(FUNC_ROUNDEVEN);
  1309. BIND_ENUM_CONSTANT(FUNC_TRUNC);
  1310. BIND_ENUM_CONSTANT(FUNC_ONEMINUS);
  1311. }
  1312. VisualShaderNodeScalarFunc::VisualShaderNodeScalarFunc() {
  1313. func = FUNC_SIGN;
  1314. set_input_port_default_value(0, 0.0);
  1315. }
  1316. ////////////// Vector Func
  1317. String VisualShaderNodeVectorFunc::get_caption() const {
  1318. return "VectorFunc";
  1319. }
  1320. int VisualShaderNodeVectorFunc::get_input_port_count() const {
  1321. return 1;
  1322. }
  1323. VisualShaderNodeVectorFunc::PortType VisualShaderNodeVectorFunc::get_input_port_type(int p_port) const {
  1324. return PORT_TYPE_VECTOR;
  1325. }
  1326. String VisualShaderNodeVectorFunc::get_input_port_name(int p_port) const {
  1327. return "";
  1328. }
  1329. int VisualShaderNodeVectorFunc::get_output_port_count() const {
  1330. return 1;
  1331. }
  1332. VisualShaderNodeVectorFunc::PortType VisualShaderNodeVectorFunc::get_output_port_type(int p_port) const {
  1333. return PORT_TYPE_VECTOR;
  1334. }
  1335. String VisualShaderNodeVectorFunc::get_output_port_name(int p_port) const {
  1336. return ""; //no output port means the editor will be used as port
  1337. }
  1338. String VisualShaderNodeVectorFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1339. static const char *vec_func_id[FUNC_ONEMINUS + 1] = {
  1340. "normalize($)",
  1341. "max(min($, vec3(1.0)), vec3(0.0))",
  1342. "-($)",
  1343. "1.0 / ($)",
  1344. "",
  1345. "",
  1346. "abs($)",
  1347. "acos($)",
  1348. "acosh($)",
  1349. "asin($)",
  1350. "asinh($)",
  1351. "atan($)",
  1352. "atanh($)",
  1353. "ceil($)",
  1354. "cos($)",
  1355. "cosh($)",
  1356. "degrees($)",
  1357. "exp($)",
  1358. "exp2($)",
  1359. "floor($)",
  1360. "fract($)",
  1361. "inversesqrt($)",
  1362. "log($)",
  1363. "log2($)",
  1364. "radians($)",
  1365. "round($)",
  1366. "roundEven($)",
  1367. "sign($)",
  1368. "sin($)",
  1369. "sinh($)",
  1370. "sqrt($)",
  1371. "tan($)",
  1372. "tanh($)",
  1373. "trunc($)",
  1374. "vec3(1.0, 1.0, 1.0) - $"
  1375. };
  1376. String code;
  1377. if (func == FUNC_RGB2HSV) {
  1378. code += "\t{\n";
  1379. code += "\t\tvec3 c = " + p_input_vars[0] + ";\n";
  1380. code += "\t\tvec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);\n";
  1381. code += "\t\tvec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g));\n";
  1382. code += "\t\tvec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r));\n";
  1383. code += "\t\tfloat d = q.x - min(q.w, q.y);\n";
  1384. code += "\t\tfloat e = 1.0e-10;\n";
  1385. code += "\t\t" + p_output_vars[0] + " = vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);\n";
  1386. code += "\t}\n";
  1387. } else if (func == FUNC_HSV2RGB) {
  1388. code += "\t{\n";
  1389. code += "\t\tvec3 c = " + p_input_vars[0] + ";\n";
  1390. code += "\t\tvec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);\n";
  1391. code += "\t\tvec3 p = abs(fract(c.xxx + K.xyz) * 6.0 - K.www);\n";
  1392. code += "\t\t" + p_output_vars[0] + " = c.z * mix(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);\n";
  1393. code += "\t}\n";
  1394. } else {
  1395. code += "\t" + p_output_vars[0] + " = " + String(vec_func_id[func]).replace("$", p_input_vars[0]) + ";\n";
  1396. }
  1397. return code;
  1398. }
  1399. void VisualShaderNodeVectorFunc::set_function(Function p_func) {
  1400. func = p_func;
  1401. if (func == FUNC_RGB2HSV) {
  1402. simple_decl = false;
  1403. } else if (func == FUNC_HSV2RGB) {
  1404. simple_decl = false;
  1405. } else {
  1406. simple_decl = true;
  1407. }
  1408. emit_changed();
  1409. }
  1410. VisualShaderNodeVectorFunc::Function VisualShaderNodeVectorFunc::get_function() const {
  1411. return func;
  1412. }
  1413. Vector<StringName> VisualShaderNodeVectorFunc::get_editable_properties() const {
  1414. Vector<StringName> props;
  1415. props.push_back("function");
  1416. return props;
  1417. }
  1418. void VisualShaderNodeVectorFunc::_bind_methods() {
  1419. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeVectorFunc::set_function);
  1420. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeVectorFunc::get_function);
  1421. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Normalize,Saturate,Negate,Reciprocal,RGB2HSV,HSV2RGB,Abs,ACos,ACosH,ASin,ASinH,ATan,ATanH,Ceil,Cos,CosH,Degrees,Exp,Exp2,Floor,Frac,InverseSqrt,Log,Log2,Radians,Round,RoundEven,Sign,Sin,SinH,Sqrt,Tan,TanH,Trunc,OneMinus"), "set_function", "get_function");
  1422. BIND_ENUM_CONSTANT(FUNC_NORMALIZE);
  1423. BIND_ENUM_CONSTANT(FUNC_SATURATE);
  1424. BIND_ENUM_CONSTANT(FUNC_NEGATE);
  1425. BIND_ENUM_CONSTANT(FUNC_RECIPROCAL);
  1426. BIND_ENUM_CONSTANT(FUNC_RGB2HSV);
  1427. BIND_ENUM_CONSTANT(FUNC_HSV2RGB);
  1428. BIND_ENUM_CONSTANT(FUNC_ABS);
  1429. BIND_ENUM_CONSTANT(FUNC_ACOS);
  1430. BIND_ENUM_CONSTANT(FUNC_ACOSH);
  1431. BIND_ENUM_CONSTANT(FUNC_ASIN);
  1432. BIND_ENUM_CONSTANT(FUNC_ASINH);
  1433. BIND_ENUM_CONSTANT(FUNC_ATAN);
  1434. BIND_ENUM_CONSTANT(FUNC_ATANH);
  1435. BIND_ENUM_CONSTANT(FUNC_CEIL);
  1436. BIND_ENUM_CONSTANT(FUNC_COS);
  1437. BIND_ENUM_CONSTANT(FUNC_COSH);
  1438. BIND_ENUM_CONSTANT(FUNC_DEGREES);
  1439. BIND_ENUM_CONSTANT(FUNC_EXP);
  1440. BIND_ENUM_CONSTANT(FUNC_EXP2);
  1441. BIND_ENUM_CONSTANT(FUNC_FLOOR);
  1442. BIND_ENUM_CONSTANT(FUNC_FRAC);
  1443. BIND_ENUM_CONSTANT(FUNC_INVERSE_SQRT);
  1444. BIND_ENUM_CONSTANT(FUNC_LOG);
  1445. BIND_ENUM_CONSTANT(FUNC_LOG2);
  1446. BIND_ENUM_CONSTANT(FUNC_RADIANS);
  1447. BIND_ENUM_CONSTANT(FUNC_ROUND);
  1448. BIND_ENUM_CONSTANT(FUNC_ROUNDEVEN);
  1449. BIND_ENUM_CONSTANT(FUNC_SIGN);
  1450. BIND_ENUM_CONSTANT(FUNC_SIN);
  1451. BIND_ENUM_CONSTANT(FUNC_SINH);
  1452. BIND_ENUM_CONSTANT(FUNC_SQRT);
  1453. BIND_ENUM_CONSTANT(FUNC_TAN);
  1454. BIND_ENUM_CONSTANT(FUNC_TANH);
  1455. BIND_ENUM_CONSTANT(FUNC_TRUNC);
  1456. BIND_ENUM_CONSTANT(FUNC_ONEMINUS);
  1457. }
  1458. VisualShaderNodeVectorFunc::VisualShaderNodeVectorFunc() {
  1459. func = FUNC_NORMALIZE;
  1460. set_input_port_default_value(0, Vector3());
  1461. }
  1462. ////////////// ColorFunc
  1463. String VisualShaderNodeColorFunc::get_caption() const {
  1464. return "ColorFunc";
  1465. }
  1466. int VisualShaderNodeColorFunc::get_input_port_count() const {
  1467. return 1;
  1468. }
  1469. VisualShaderNodeColorFunc::PortType VisualShaderNodeColorFunc::get_input_port_type(int p_port) const {
  1470. return PORT_TYPE_VECTOR;
  1471. }
  1472. String VisualShaderNodeColorFunc::get_input_port_name(int p_port) const {
  1473. return "";
  1474. }
  1475. int VisualShaderNodeColorFunc::get_output_port_count() const {
  1476. return 1;
  1477. }
  1478. VisualShaderNodeColorFunc::PortType VisualShaderNodeColorFunc::get_output_port_type(int p_port) const {
  1479. return PORT_TYPE_VECTOR;
  1480. }
  1481. String VisualShaderNodeColorFunc::get_output_port_name(int p_port) const {
  1482. return "";
  1483. }
  1484. String VisualShaderNodeColorFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1485. String code;
  1486. switch (func) {
  1487. case FUNC_GRAYSCALE:
  1488. code += "\t{\n";
  1489. code += "\t\tvec3 c = " + p_input_vars[0] + ";\n";
  1490. code += "\t\tfloat max1 = max(c.r, c.g);\n";
  1491. code += "\t\tfloat max2 = max(max1, c.b);\n";
  1492. code += "\t\t" + p_output_vars[0] + " = vec3(max2, max2, max2);\n";
  1493. code += "\t}\n";
  1494. break;
  1495. case FUNC_SEPIA:
  1496. code += "\t{\n";
  1497. code += "\t\tvec3 c = " + p_input_vars[0] + ";\n";
  1498. code += "\t\tfloat r = (c.r * .393) + (c.g *.769) + (c.b * .189);\n";
  1499. code += "\t\tfloat g = (c.r * .349) + (c.g *.686) + (c.b * .168);\n";
  1500. code += "\t\tfloat b = (c.r * .272) + (c.g *.534) + (c.b * .131);\n";
  1501. code += "\t\t" + p_output_vars[0] + " = vec3(r, g, b);\n";
  1502. code += "\t}\n";
  1503. break;
  1504. }
  1505. return code;
  1506. }
  1507. void VisualShaderNodeColorFunc::set_function(Function p_func) {
  1508. func = p_func;
  1509. emit_changed();
  1510. }
  1511. VisualShaderNodeColorFunc::Function VisualShaderNodeColorFunc::get_function() const {
  1512. return func;
  1513. }
  1514. Vector<StringName> VisualShaderNodeColorFunc::get_editable_properties() const {
  1515. Vector<StringName> props;
  1516. props.push_back("function");
  1517. return props;
  1518. }
  1519. void VisualShaderNodeColorFunc::_bind_methods() {
  1520. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeColorFunc::set_function);
  1521. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeColorFunc::get_function);
  1522. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Grayscale,Sepia"), "set_function", "get_function");
  1523. BIND_ENUM_CONSTANT(FUNC_GRAYSCALE);
  1524. BIND_ENUM_CONSTANT(FUNC_SEPIA);
  1525. }
  1526. VisualShaderNodeColorFunc::VisualShaderNodeColorFunc() {
  1527. func = FUNC_GRAYSCALE;
  1528. set_input_port_default_value(0, Vector3());
  1529. simple_decl = false;
  1530. }
  1531. ////////////// Transform Func
  1532. String VisualShaderNodeTransformFunc::get_caption() const {
  1533. return "TransformFunc";
  1534. }
  1535. int VisualShaderNodeTransformFunc::get_input_port_count() const {
  1536. return 1;
  1537. }
  1538. VisualShaderNodeTransformFunc::PortType VisualShaderNodeTransformFunc::get_input_port_type(int p_port) const {
  1539. return PORT_TYPE_TRANSFORM;
  1540. }
  1541. String VisualShaderNodeTransformFunc::get_input_port_name(int p_port) const {
  1542. return "";
  1543. }
  1544. int VisualShaderNodeTransformFunc::get_output_port_count() const {
  1545. return 1;
  1546. }
  1547. VisualShaderNodeTransformFunc::PortType VisualShaderNodeTransformFunc::get_output_port_type(int p_port) const {
  1548. return PORT_TYPE_TRANSFORM;
  1549. }
  1550. String VisualShaderNodeTransformFunc::get_output_port_name(int p_port) const {
  1551. return "";
  1552. }
  1553. String VisualShaderNodeTransformFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1554. static const char *funcs[FUNC_TRANSPOSE + 1] = {
  1555. "inverse($)",
  1556. "transpose($)"
  1557. };
  1558. String code;
  1559. code += "\t" + p_output_vars[0] + " = " + String(funcs[func]).replace("$", p_input_vars[0]) + ";\n";
  1560. return code;
  1561. }
  1562. void VisualShaderNodeTransformFunc::set_function(Function p_func) {
  1563. func = p_func;
  1564. emit_changed();
  1565. }
  1566. VisualShaderNodeTransformFunc::Function VisualShaderNodeTransformFunc::get_function() const {
  1567. return func;
  1568. }
  1569. Vector<StringName> VisualShaderNodeTransformFunc::get_editable_properties() const {
  1570. Vector<StringName> props;
  1571. props.push_back("function");
  1572. return props;
  1573. }
  1574. void VisualShaderNodeTransformFunc::_bind_methods() {
  1575. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeTransformFunc::set_function);
  1576. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeTransformFunc::get_function);
  1577. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Inverse,Transpose"), "set_function", "get_function");
  1578. BIND_ENUM_CONSTANT(FUNC_INVERSE);
  1579. BIND_ENUM_CONSTANT(FUNC_TRANSPOSE);
  1580. }
  1581. VisualShaderNodeTransformFunc::VisualShaderNodeTransformFunc() {
  1582. func = FUNC_INVERSE;
  1583. set_input_port_default_value(0, Transform());
  1584. }
  1585. ////////////// Dot Product
  1586. String VisualShaderNodeDotProduct::get_caption() const {
  1587. return "DotProduct";
  1588. }
  1589. int VisualShaderNodeDotProduct::get_input_port_count() const {
  1590. return 2;
  1591. }
  1592. VisualShaderNodeDotProduct::PortType VisualShaderNodeDotProduct::get_input_port_type(int p_port) const {
  1593. return PORT_TYPE_VECTOR;
  1594. }
  1595. String VisualShaderNodeDotProduct::get_input_port_name(int p_port) const {
  1596. return p_port == 0 ? "a" : "b";
  1597. }
  1598. int VisualShaderNodeDotProduct::get_output_port_count() const {
  1599. return 1;
  1600. }
  1601. VisualShaderNodeDotProduct::PortType VisualShaderNodeDotProduct::get_output_port_type(int p_port) const {
  1602. return PORT_TYPE_SCALAR;
  1603. }
  1604. String VisualShaderNodeDotProduct::get_output_port_name(int p_port) const {
  1605. return "dot";
  1606. }
  1607. String VisualShaderNodeDotProduct::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1608. return "\t" + p_output_vars[0] + " = dot(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1609. }
  1610. VisualShaderNodeDotProduct::VisualShaderNodeDotProduct() {
  1611. set_input_port_default_value(0, Vector3());
  1612. set_input_port_default_value(1, Vector3());
  1613. }
  1614. ////////////// Vector Len
  1615. String VisualShaderNodeVectorLen::get_caption() const {
  1616. return "VectorLen";
  1617. }
  1618. int VisualShaderNodeVectorLen::get_input_port_count() const {
  1619. return 1;
  1620. }
  1621. VisualShaderNodeVectorLen::PortType VisualShaderNodeVectorLen::get_input_port_type(int p_port) const {
  1622. return PORT_TYPE_VECTOR;
  1623. }
  1624. String VisualShaderNodeVectorLen::get_input_port_name(int p_port) const {
  1625. return "";
  1626. }
  1627. int VisualShaderNodeVectorLen::get_output_port_count() const {
  1628. return 1;
  1629. }
  1630. VisualShaderNodeVectorLen::PortType VisualShaderNodeVectorLen::get_output_port_type(int p_port) const {
  1631. return PORT_TYPE_SCALAR;
  1632. }
  1633. String VisualShaderNodeVectorLen::get_output_port_name(int p_port) const {
  1634. return "length";
  1635. }
  1636. String VisualShaderNodeVectorLen::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1637. return "\t" + p_output_vars[0] + " = length(" + p_input_vars[0] + ");\n";
  1638. }
  1639. VisualShaderNodeVectorLen::VisualShaderNodeVectorLen() {
  1640. set_input_port_default_value(0, Vector3());
  1641. }
  1642. ////////////// Determinant
  1643. String VisualShaderNodeDeterminant::get_caption() const {
  1644. return "Determinant";
  1645. }
  1646. int VisualShaderNodeDeterminant::get_input_port_count() const {
  1647. return 1;
  1648. }
  1649. VisualShaderNodeDeterminant::PortType VisualShaderNodeDeterminant::get_input_port_type(int p_port) const {
  1650. return PORT_TYPE_TRANSFORM;
  1651. }
  1652. String VisualShaderNodeDeterminant::get_input_port_name(int p_port) const {
  1653. return "";
  1654. }
  1655. int VisualShaderNodeDeterminant::get_output_port_count() const {
  1656. return 1;
  1657. }
  1658. VisualShaderNodeDeterminant::PortType VisualShaderNodeDeterminant::get_output_port_type(int p_port) const {
  1659. return PORT_TYPE_SCALAR;
  1660. }
  1661. String VisualShaderNodeDeterminant::get_output_port_name(int p_port) const {
  1662. return "";
  1663. }
  1664. String VisualShaderNodeDeterminant::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1665. return "\t" + p_output_vars[0] + " = determinant(" + p_input_vars[0] + ");\n";
  1666. }
  1667. VisualShaderNodeDeterminant::VisualShaderNodeDeterminant() {
  1668. set_input_port_default_value(0, Transform());
  1669. }
  1670. ////////////// Scalar Derivative Function
  1671. String VisualShaderNodeScalarDerivativeFunc::get_caption() const {
  1672. return "ScalarDerivativeFunc";
  1673. }
  1674. int VisualShaderNodeScalarDerivativeFunc::get_input_port_count() const {
  1675. return 1;
  1676. }
  1677. VisualShaderNodeScalarDerivativeFunc::PortType VisualShaderNodeScalarDerivativeFunc::get_input_port_type(int p_port) const {
  1678. return PORT_TYPE_SCALAR;
  1679. }
  1680. String VisualShaderNodeScalarDerivativeFunc::get_input_port_name(int p_port) const {
  1681. return "";
  1682. }
  1683. int VisualShaderNodeScalarDerivativeFunc::get_output_port_count() const {
  1684. return 1;
  1685. }
  1686. VisualShaderNodeScalarDerivativeFunc::PortType VisualShaderNodeScalarDerivativeFunc::get_output_port_type(int p_port) const {
  1687. return PORT_TYPE_SCALAR;
  1688. }
  1689. String VisualShaderNodeScalarDerivativeFunc::get_output_port_name(int p_port) const {
  1690. return "";
  1691. }
  1692. String VisualShaderNodeScalarDerivativeFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1693. static const char *funcs[FUNC_Y + 1] = {
  1694. "fwidth($)",
  1695. "dFdx($)",
  1696. "dFdy($)"
  1697. };
  1698. String code;
  1699. code += "\t" + p_output_vars[0] + " = " + String(funcs[func]).replace("$", p_input_vars[0]) + ";\n";
  1700. return code;
  1701. }
  1702. void VisualShaderNodeScalarDerivativeFunc::set_function(Function p_func) {
  1703. func = p_func;
  1704. emit_changed();
  1705. }
  1706. VisualShaderNodeScalarDerivativeFunc::Function VisualShaderNodeScalarDerivativeFunc::get_function() const {
  1707. return func;
  1708. }
  1709. Vector<StringName> VisualShaderNodeScalarDerivativeFunc::get_editable_properties() const {
  1710. Vector<StringName> props;
  1711. props.push_back("function");
  1712. return props;
  1713. }
  1714. void VisualShaderNodeScalarDerivativeFunc::_bind_methods() {
  1715. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeScalarDerivativeFunc::set_function);
  1716. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeScalarDerivativeFunc::get_function);
  1717. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Sum,X,Y"), "set_function", "get_function");
  1718. BIND_ENUM_CONSTANT(FUNC_SUM);
  1719. BIND_ENUM_CONSTANT(FUNC_X);
  1720. BIND_ENUM_CONSTANT(FUNC_Y);
  1721. }
  1722. VisualShaderNodeScalarDerivativeFunc::VisualShaderNodeScalarDerivativeFunc() {
  1723. func = FUNC_SUM;
  1724. set_input_port_default_value(0, 0.0);
  1725. }
  1726. ////////////// Vector Derivative Function
  1727. String VisualShaderNodeVectorDerivativeFunc::get_caption() const {
  1728. return "VectorDerivativeFunc";
  1729. }
  1730. int VisualShaderNodeVectorDerivativeFunc::get_input_port_count() const {
  1731. return 1;
  1732. }
  1733. VisualShaderNodeVectorDerivativeFunc::PortType VisualShaderNodeVectorDerivativeFunc::get_input_port_type(int p_port) const {
  1734. return PORT_TYPE_VECTOR;
  1735. }
  1736. String VisualShaderNodeVectorDerivativeFunc::get_input_port_name(int p_port) const {
  1737. return "";
  1738. }
  1739. int VisualShaderNodeVectorDerivativeFunc::get_output_port_count() const {
  1740. return 1;
  1741. }
  1742. VisualShaderNodeVectorDerivativeFunc::PortType VisualShaderNodeVectorDerivativeFunc::get_output_port_type(int p_port) const {
  1743. return PORT_TYPE_VECTOR;
  1744. }
  1745. String VisualShaderNodeVectorDerivativeFunc::get_output_port_name(int p_port) const {
  1746. return "";
  1747. }
  1748. String VisualShaderNodeVectorDerivativeFunc::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1749. static const char *funcs[FUNC_Y + 1] = {
  1750. "fwidth($)",
  1751. "dFdx($)",
  1752. "dFdy($)"
  1753. };
  1754. String code;
  1755. code += "\t" + p_output_vars[0] + " = " + String(funcs[func]).replace("$", p_input_vars[0]) + ";\n";
  1756. return code;
  1757. }
  1758. void VisualShaderNodeVectorDerivativeFunc::set_function(Function p_func) {
  1759. func = p_func;
  1760. emit_changed();
  1761. }
  1762. VisualShaderNodeVectorDerivativeFunc::Function VisualShaderNodeVectorDerivativeFunc::get_function() const {
  1763. return func;
  1764. }
  1765. Vector<StringName> VisualShaderNodeVectorDerivativeFunc::get_editable_properties() const {
  1766. Vector<StringName> props;
  1767. props.push_back("function");
  1768. return props;
  1769. }
  1770. void VisualShaderNodeVectorDerivativeFunc::_bind_methods() {
  1771. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeVectorDerivativeFunc::set_function);
  1772. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeVectorDerivativeFunc::get_function);
  1773. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Sum,X,Y"), "set_function", "get_function");
  1774. BIND_ENUM_CONSTANT(FUNC_SUM);
  1775. BIND_ENUM_CONSTANT(FUNC_X);
  1776. BIND_ENUM_CONSTANT(FUNC_Y);
  1777. }
  1778. VisualShaderNodeVectorDerivativeFunc::VisualShaderNodeVectorDerivativeFunc() {
  1779. func = FUNC_SUM;
  1780. set_input_port_default_value(0, Vector3());
  1781. }
  1782. ////////////// Scalar Clamp
  1783. String VisualShaderNodeScalarClamp::get_caption() const {
  1784. return "ScalarClamp";
  1785. }
  1786. int VisualShaderNodeScalarClamp::get_input_port_count() const {
  1787. return 3;
  1788. }
  1789. VisualShaderNodeScalarClamp::PortType VisualShaderNodeScalarClamp::get_input_port_type(int p_port) const {
  1790. return PORT_TYPE_SCALAR;
  1791. }
  1792. String VisualShaderNodeScalarClamp::get_input_port_name(int p_port) const {
  1793. if (p_port == 0) {
  1794. return "";
  1795. } else if (p_port == 1) {
  1796. return "min";
  1797. } else if (p_port == 2) {
  1798. return "max";
  1799. }
  1800. return "";
  1801. }
  1802. int VisualShaderNodeScalarClamp::get_output_port_count() const {
  1803. return 1;
  1804. }
  1805. VisualShaderNodeScalarClamp::PortType VisualShaderNodeScalarClamp::get_output_port_type(int p_port) const {
  1806. return PORT_TYPE_SCALAR;
  1807. }
  1808. String VisualShaderNodeScalarClamp::get_output_port_name(int p_port) const {
  1809. return "";
  1810. }
  1811. String VisualShaderNodeScalarClamp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1812. return "\t" + p_output_vars[0] + " = clamp(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  1813. }
  1814. VisualShaderNodeScalarClamp::VisualShaderNodeScalarClamp() {
  1815. set_input_port_default_value(0, 0.0);
  1816. set_input_port_default_value(1, 0.0);
  1817. set_input_port_default_value(2, 1.0);
  1818. }
  1819. ////////////// Vector Clamp
  1820. String VisualShaderNodeVectorClamp::get_caption() const {
  1821. return "VectorClamp";
  1822. }
  1823. int VisualShaderNodeVectorClamp::get_input_port_count() const {
  1824. return 3;
  1825. }
  1826. VisualShaderNodeVectorClamp::PortType VisualShaderNodeVectorClamp::get_input_port_type(int p_port) const {
  1827. return PORT_TYPE_VECTOR;
  1828. }
  1829. String VisualShaderNodeVectorClamp::get_input_port_name(int p_port) const {
  1830. if (p_port == 0) {
  1831. return "";
  1832. } else if (p_port == 1) {
  1833. return "min";
  1834. } else if (p_port == 2) {
  1835. return "max";
  1836. }
  1837. return "";
  1838. }
  1839. int VisualShaderNodeVectorClamp::get_output_port_count() const {
  1840. return 1;
  1841. }
  1842. VisualShaderNodeVectorClamp::PortType VisualShaderNodeVectorClamp::get_output_port_type(int p_port) const {
  1843. return PORT_TYPE_VECTOR;
  1844. }
  1845. String VisualShaderNodeVectorClamp::get_output_port_name(int p_port) const {
  1846. return "";
  1847. }
  1848. String VisualShaderNodeVectorClamp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1849. return "\t" + p_output_vars[0] + " = clamp(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  1850. }
  1851. VisualShaderNodeVectorClamp::VisualShaderNodeVectorClamp() {
  1852. set_input_port_default_value(0, Vector3(0, 0, 0));
  1853. set_input_port_default_value(1, Vector3(0, 0, 0));
  1854. set_input_port_default_value(2, Vector3(1, 1, 1));
  1855. }
  1856. ////////////// FaceForward
  1857. String VisualShaderNodeFaceForward::get_caption() const {
  1858. return "FaceForward";
  1859. }
  1860. int VisualShaderNodeFaceForward::get_input_port_count() const {
  1861. return 3;
  1862. }
  1863. VisualShaderNodeFaceForward::PortType VisualShaderNodeFaceForward::get_input_port_type(int p_port) const {
  1864. return PORT_TYPE_VECTOR;
  1865. }
  1866. String VisualShaderNodeFaceForward::get_input_port_name(int p_port) const {
  1867. switch (p_port) {
  1868. case 0:
  1869. return "N";
  1870. case 1:
  1871. return "I";
  1872. case 2:
  1873. return "Nref";
  1874. default:
  1875. return "";
  1876. }
  1877. }
  1878. int VisualShaderNodeFaceForward::get_output_port_count() const {
  1879. return 1;
  1880. }
  1881. VisualShaderNodeFaceForward::PortType VisualShaderNodeFaceForward::get_output_port_type(int p_port) const {
  1882. return PORT_TYPE_VECTOR;
  1883. }
  1884. String VisualShaderNodeFaceForward::get_output_port_name(int p_port) const {
  1885. return "";
  1886. }
  1887. String VisualShaderNodeFaceForward::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1888. return "\t" + p_output_vars[0] + " = faceforward(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  1889. }
  1890. VisualShaderNodeFaceForward::VisualShaderNodeFaceForward() {
  1891. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  1892. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
  1893. set_input_port_default_value(2, Vector3(0.0, 0.0, 0.0));
  1894. }
  1895. ////////////// Outer Product
  1896. String VisualShaderNodeOuterProduct::get_caption() const {
  1897. return "OuterProduct";
  1898. }
  1899. int VisualShaderNodeOuterProduct::get_input_port_count() const {
  1900. return 2;
  1901. }
  1902. VisualShaderNodeOuterProduct::PortType VisualShaderNodeOuterProduct::get_input_port_type(int p_port) const {
  1903. return PORT_TYPE_VECTOR;
  1904. }
  1905. String VisualShaderNodeOuterProduct::get_input_port_name(int p_port) const {
  1906. switch (p_port) {
  1907. case 0:
  1908. return "c";
  1909. case 1:
  1910. return "r";
  1911. default:
  1912. return "";
  1913. }
  1914. }
  1915. int VisualShaderNodeOuterProduct::get_output_port_count() const {
  1916. return 1;
  1917. }
  1918. VisualShaderNodeOuterProduct::PortType VisualShaderNodeOuterProduct::get_output_port_type(int p_port) const {
  1919. return PORT_TYPE_TRANSFORM;
  1920. }
  1921. String VisualShaderNodeOuterProduct::get_output_port_name(int p_port) const {
  1922. return "";
  1923. }
  1924. String VisualShaderNodeOuterProduct::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1925. return "\t" + p_output_vars[0] + " = outerProduct(vec4(" + p_input_vars[0] + ", 0.0), vec4(" + p_input_vars[1] + ", 0.0));\n";
  1926. }
  1927. VisualShaderNodeOuterProduct::VisualShaderNodeOuterProduct() {
  1928. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  1929. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
  1930. }
  1931. ////////////// Vector-Scalar Step
  1932. String VisualShaderNodeVectorScalarStep::get_caption() const {
  1933. return "VectorScalarStep";
  1934. }
  1935. int VisualShaderNodeVectorScalarStep::get_input_port_count() const {
  1936. return 2;
  1937. }
  1938. VisualShaderNodeVectorScalarStep::PortType VisualShaderNodeVectorScalarStep::get_input_port_type(int p_port) const {
  1939. if (p_port == 0) {
  1940. return PORT_TYPE_SCALAR;
  1941. }
  1942. return PORT_TYPE_VECTOR;
  1943. }
  1944. String VisualShaderNodeVectorScalarStep::get_input_port_name(int p_port) const {
  1945. if (p_port == 0) {
  1946. return "edge";
  1947. } else if (p_port == 1) {
  1948. return "x";
  1949. }
  1950. return "";
  1951. }
  1952. int VisualShaderNodeVectorScalarStep::get_output_port_count() const {
  1953. return 1;
  1954. }
  1955. VisualShaderNodeVectorScalarStep::PortType VisualShaderNodeVectorScalarStep::get_output_port_type(int p_port) const {
  1956. return PORT_TYPE_VECTOR;
  1957. }
  1958. String VisualShaderNodeVectorScalarStep::get_output_port_name(int p_port) const {
  1959. return "";
  1960. }
  1961. String VisualShaderNodeVectorScalarStep::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1962. return "\t" + p_output_vars[0] + " = step(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  1963. }
  1964. VisualShaderNodeVectorScalarStep::VisualShaderNodeVectorScalarStep() {
  1965. set_input_port_default_value(0, 0.0);
  1966. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
  1967. }
  1968. ////////////// Scalar SmoothStep
  1969. String VisualShaderNodeScalarSmoothStep::get_caption() const {
  1970. return "ScalarSmoothStep";
  1971. }
  1972. int VisualShaderNodeScalarSmoothStep::get_input_port_count() const {
  1973. return 3;
  1974. }
  1975. VisualShaderNodeScalarSmoothStep::PortType VisualShaderNodeScalarSmoothStep::get_input_port_type(int p_port) const {
  1976. return PORT_TYPE_SCALAR;
  1977. }
  1978. String VisualShaderNodeScalarSmoothStep::get_input_port_name(int p_port) const {
  1979. if (p_port == 0) {
  1980. return "edge0";
  1981. } else if (p_port == 1) {
  1982. return "edge1";
  1983. } else if (p_port == 2) {
  1984. return "x";
  1985. }
  1986. return "";
  1987. }
  1988. int VisualShaderNodeScalarSmoothStep::get_output_port_count() const {
  1989. return 1;
  1990. }
  1991. VisualShaderNodeScalarSmoothStep::PortType VisualShaderNodeScalarSmoothStep::get_output_port_type(int p_port) const {
  1992. return PORT_TYPE_SCALAR;
  1993. }
  1994. String VisualShaderNodeScalarSmoothStep::get_output_port_name(int p_port) const {
  1995. return "";
  1996. }
  1997. String VisualShaderNodeScalarSmoothStep::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  1998. return "\t" + p_output_vars[0] + " = smoothstep(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  1999. }
  2000. VisualShaderNodeScalarSmoothStep::VisualShaderNodeScalarSmoothStep() {
  2001. set_input_port_default_value(0, 0.0);
  2002. set_input_port_default_value(1, 0.0);
  2003. set_input_port_default_value(2, 0.0);
  2004. }
  2005. ////////////// Vector SmoothStep
  2006. String VisualShaderNodeVectorSmoothStep::get_caption() const {
  2007. return "VectorSmoothStep";
  2008. }
  2009. int VisualShaderNodeVectorSmoothStep::get_input_port_count() const {
  2010. return 3;
  2011. }
  2012. VisualShaderNodeVectorSmoothStep::PortType VisualShaderNodeVectorSmoothStep::get_input_port_type(int p_port) const {
  2013. return PORT_TYPE_VECTOR;
  2014. }
  2015. String VisualShaderNodeVectorSmoothStep::get_input_port_name(int p_port) const {
  2016. if (p_port == 0) {
  2017. return "edge0";
  2018. } else if (p_port == 1) {
  2019. return "edge1";
  2020. } else if (p_port == 2) {
  2021. return "x";
  2022. }
  2023. return "";
  2024. }
  2025. int VisualShaderNodeVectorSmoothStep::get_output_port_count() const {
  2026. return 1;
  2027. }
  2028. VisualShaderNodeVectorSmoothStep::PortType VisualShaderNodeVectorSmoothStep::get_output_port_type(int p_port) const {
  2029. return PORT_TYPE_VECTOR;
  2030. }
  2031. String VisualShaderNodeVectorSmoothStep::get_output_port_name(int p_port) const {
  2032. return "";
  2033. }
  2034. String VisualShaderNodeVectorSmoothStep::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2035. return "\t" + p_output_vars[0] + " = smoothstep(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  2036. }
  2037. VisualShaderNodeVectorSmoothStep::VisualShaderNodeVectorSmoothStep() {
  2038. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  2039. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
  2040. set_input_port_default_value(2, Vector3(0.0, 0.0, 0.0));
  2041. }
  2042. ////////////// Vector-Scalar SmoothStep
  2043. String VisualShaderNodeVectorScalarSmoothStep::get_caption() const {
  2044. return "VectorScalarSmoothStep";
  2045. }
  2046. int VisualShaderNodeVectorScalarSmoothStep::get_input_port_count() const {
  2047. return 3;
  2048. }
  2049. VisualShaderNodeVectorScalarSmoothStep::PortType VisualShaderNodeVectorScalarSmoothStep::get_input_port_type(int p_port) const {
  2050. if (p_port == 0) {
  2051. return PORT_TYPE_SCALAR;
  2052. } else if (p_port == 1) {
  2053. return PORT_TYPE_SCALAR;
  2054. }
  2055. return PORT_TYPE_VECTOR;
  2056. }
  2057. String VisualShaderNodeVectorScalarSmoothStep::get_input_port_name(int p_port) const {
  2058. if (p_port == 0) {
  2059. return "edge0";
  2060. } else if (p_port == 1) {
  2061. return "edge1";
  2062. } else if (p_port == 2) {
  2063. return "x";
  2064. }
  2065. return "";
  2066. }
  2067. int VisualShaderNodeVectorScalarSmoothStep::get_output_port_count() const {
  2068. return 1;
  2069. }
  2070. VisualShaderNodeVectorScalarSmoothStep::PortType VisualShaderNodeVectorScalarSmoothStep::get_output_port_type(int p_port) const {
  2071. return PORT_TYPE_VECTOR;
  2072. }
  2073. String VisualShaderNodeVectorScalarSmoothStep::get_output_port_name(int p_port) const {
  2074. return "";
  2075. }
  2076. String VisualShaderNodeVectorScalarSmoothStep::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2077. return "\t" + p_output_vars[0] + " = smoothstep(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  2078. }
  2079. VisualShaderNodeVectorScalarSmoothStep::VisualShaderNodeVectorScalarSmoothStep() {
  2080. set_input_port_default_value(0, 0.0);
  2081. set_input_port_default_value(1, 0.0);
  2082. set_input_port_default_value(2, Vector3(0.0, 0.0, 0.0));
  2083. }
  2084. ////////////// Distance
  2085. String VisualShaderNodeVectorDistance::get_caption() const {
  2086. return "Distance";
  2087. }
  2088. int VisualShaderNodeVectorDistance::get_input_port_count() const {
  2089. return 2;
  2090. }
  2091. VisualShaderNodeVectorDistance::PortType VisualShaderNodeVectorDistance::get_input_port_type(int p_port) const {
  2092. return PORT_TYPE_VECTOR;
  2093. }
  2094. String VisualShaderNodeVectorDistance::get_input_port_name(int p_port) const {
  2095. if (p_port == 0) {
  2096. return "p0";
  2097. } else if (p_port == 1) {
  2098. return "p1";
  2099. }
  2100. return "";
  2101. }
  2102. int VisualShaderNodeVectorDistance::get_output_port_count() const {
  2103. return 1;
  2104. }
  2105. VisualShaderNodeVectorDistance::PortType VisualShaderNodeVectorDistance::get_output_port_type(int p_port) const {
  2106. return PORT_TYPE_SCALAR;
  2107. }
  2108. String VisualShaderNodeVectorDistance::get_output_port_name(int p_port) const {
  2109. return "";
  2110. }
  2111. String VisualShaderNodeVectorDistance::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2112. return "\t" + p_output_vars[0] + " = distance(" + p_input_vars[0] + ", " + p_input_vars[1] + ");\n";
  2113. }
  2114. VisualShaderNodeVectorDistance::VisualShaderNodeVectorDistance() {
  2115. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  2116. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
  2117. }
  2118. ////////////// Refract Vector
  2119. String VisualShaderNodeVectorRefract::get_caption() const {
  2120. return "Refract";
  2121. }
  2122. int VisualShaderNodeVectorRefract::get_input_port_count() const {
  2123. return 3;
  2124. }
  2125. VisualShaderNodeVectorRefract::PortType VisualShaderNodeVectorRefract::get_input_port_type(int p_port) const {
  2126. if (p_port == 2) {
  2127. return PORT_TYPE_SCALAR;
  2128. }
  2129. return PORT_TYPE_VECTOR;
  2130. }
  2131. String VisualShaderNodeVectorRefract::get_input_port_name(int p_port) const {
  2132. if (p_port == 0) {
  2133. return "I";
  2134. } else if (p_port == 1) {
  2135. return "N";
  2136. } else if (p_port == 2) {
  2137. return "eta";
  2138. }
  2139. return "";
  2140. }
  2141. int VisualShaderNodeVectorRefract::get_output_port_count() const {
  2142. return 1;
  2143. }
  2144. VisualShaderNodeVectorRefract::PortType VisualShaderNodeVectorRefract::get_output_port_type(int p_port) const {
  2145. return PORT_TYPE_VECTOR;
  2146. }
  2147. String VisualShaderNodeVectorRefract::get_output_port_name(int p_port) const {
  2148. return "";
  2149. }
  2150. String VisualShaderNodeVectorRefract::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2151. return "\t" + p_output_vars[0] + " = refract(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  2152. }
  2153. VisualShaderNodeVectorRefract::VisualShaderNodeVectorRefract() {
  2154. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  2155. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
  2156. set_input_port_default_value(2, 0.0);
  2157. }
  2158. ////////////// Scalar Mix
  2159. String VisualShaderNodeScalarInterp::get_caption() const {
  2160. return "ScalarMix";
  2161. }
  2162. int VisualShaderNodeScalarInterp::get_input_port_count() const {
  2163. return 3;
  2164. }
  2165. VisualShaderNodeScalarInterp::PortType VisualShaderNodeScalarInterp::get_input_port_type(int p_port) const {
  2166. return PORT_TYPE_SCALAR;
  2167. }
  2168. String VisualShaderNodeScalarInterp::get_input_port_name(int p_port) const {
  2169. if (p_port == 0) {
  2170. return "a";
  2171. } else if (p_port == 1) {
  2172. return "b";
  2173. } else {
  2174. return "weight";
  2175. }
  2176. }
  2177. int VisualShaderNodeScalarInterp::get_output_port_count() const {
  2178. return 1;
  2179. }
  2180. VisualShaderNodeScalarInterp::PortType VisualShaderNodeScalarInterp::get_output_port_type(int p_port) const {
  2181. return PORT_TYPE_SCALAR;
  2182. }
  2183. String VisualShaderNodeScalarInterp::get_output_port_name(int p_port) const {
  2184. return "mix";
  2185. }
  2186. String VisualShaderNodeScalarInterp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2187. return "\t" + p_output_vars[0] + " = mix(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  2188. }
  2189. VisualShaderNodeScalarInterp::VisualShaderNodeScalarInterp() {
  2190. set_input_port_default_value(0, 0.0);
  2191. set_input_port_default_value(1, 1.0);
  2192. set_input_port_default_value(2, 0.5);
  2193. }
  2194. ////////////// Vector Mix
  2195. String VisualShaderNodeVectorInterp::get_caption() const {
  2196. return "VectorMix";
  2197. }
  2198. int VisualShaderNodeVectorInterp::get_input_port_count() const {
  2199. return 3;
  2200. }
  2201. VisualShaderNodeVectorInterp::PortType VisualShaderNodeVectorInterp::get_input_port_type(int p_port) const {
  2202. return PORT_TYPE_VECTOR;
  2203. }
  2204. String VisualShaderNodeVectorInterp::get_input_port_name(int p_port) const {
  2205. if (p_port == 0) {
  2206. return "a";
  2207. } else if (p_port == 1) {
  2208. return "b";
  2209. } else {
  2210. return "weight";
  2211. }
  2212. }
  2213. int VisualShaderNodeVectorInterp::get_output_port_count() const {
  2214. return 1;
  2215. }
  2216. VisualShaderNodeVectorInterp::PortType VisualShaderNodeVectorInterp::get_output_port_type(int p_port) const {
  2217. return PORT_TYPE_VECTOR;
  2218. }
  2219. String VisualShaderNodeVectorInterp::get_output_port_name(int p_port) const {
  2220. return "mix";
  2221. }
  2222. String VisualShaderNodeVectorInterp::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2223. return "\t" + p_output_vars[0] + " = mix(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  2224. }
  2225. VisualShaderNodeVectorInterp::VisualShaderNodeVectorInterp() {
  2226. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  2227. set_input_port_default_value(1, Vector3(1.0, 1.0, 1.0));
  2228. set_input_port_default_value(2, Vector3(0.5, 0.5, 0.5));
  2229. }
  2230. ////////////// Vector Mix (by scalar)
  2231. String VisualShaderNodeVectorScalarMix::get_caption() const {
  2232. return "VectorScalarMix";
  2233. }
  2234. int VisualShaderNodeVectorScalarMix::get_input_port_count() const {
  2235. return 3;
  2236. }
  2237. VisualShaderNodeVectorScalarMix::PortType VisualShaderNodeVectorScalarMix::get_input_port_type(int p_port) const {
  2238. if (p_port == 2) {
  2239. return PORT_TYPE_SCALAR;
  2240. }
  2241. return PORT_TYPE_VECTOR;
  2242. }
  2243. String VisualShaderNodeVectorScalarMix::get_input_port_name(int p_port) const {
  2244. if (p_port == 0) {
  2245. return "a";
  2246. } else if (p_port == 1) {
  2247. return "b";
  2248. } else {
  2249. return "weight";
  2250. }
  2251. }
  2252. int VisualShaderNodeVectorScalarMix::get_output_port_count() const {
  2253. return 1;
  2254. }
  2255. VisualShaderNodeVectorScalarMix::PortType VisualShaderNodeVectorScalarMix::get_output_port_type(int p_port) const {
  2256. return PORT_TYPE_VECTOR;
  2257. }
  2258. String VisualShaderNodeVectorScalarMix::get_output_port_name(int p_port) const {
  2259. return "mix";
  2260. }
  2261. String VisualShaderNodeVectorScalarMix::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2262. return "\t" + p_output_vars[0] + " = mix(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  2263. }
  2264. VisualShaderNodeVectorScalarMix::VisualShaderNodeVectorScalarMix() {
  2265. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  2266. set_input_port_default_value(1, Vector3(1.0, 1.0, 1.0));
  2267. set_input_port_default_value(2, 0.5);
  2268. }
  2269. ////////////// Vector Compose
  2270. String VisualShaderNodeVectorCompose::get_caption() const {
  2271. return "VectorCompose";
  2272. }
  2273. int VisualShaderNodeVectorCompose::get_input_port_count() const {
  2274. return 3;
  2275. }
  2276. VisualShaderNodeVectorCompose::PortType VisualShaderNodeVectorCompose::get_input_port_type(int p_port) const {
  2277. return PORT_TYPE_SCALAR;
  2278. }
  2279. String VisualShaderNodeVectorCompose::get_input_port_name(int p_port) const {
  2280. if (p_port == 0) {
  2281. return "x";
  2282. } else if (p_port == 1) {
  2283. return "y";
  2284. } else {
  2285. return "z";
  2286. }
  2287. }
  2288. int VisualShaderNodeVectorCompose::get_output_port_count() const {
  2289. return 1;
  2290. }
  2291. VisualShaderNodeVectorCompose::PortType VisualShaderNodeVectorCompose::get_output_port_type(int p_port) const {
  2292. return PORT_TYPE_VECTOR;
  2293. }
  2294. String VisualShaderNodeVectorCompose::get_output_port_name(int p_port) const {
  2295. return "vec";
  2296. }
  2297. String VisualShaderNodeVectorCompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2298. return "\t" + p_output_vars[0] + " = vec3(" + p_input_vars[0] + ", " + p_input_vars[1] + ", " + p_input_vars[2] + ");\n";
  2299. }
  2300. VisualShaderNodeVectorCompose::VisualShaderNodeVectorCompose() {
  2301. set_input_port_default_value(0, 0.0);
  2302. set_input_port_default_value(1, 0.0);
  2303. set_input_port_default_value(2, 0.0);
  2304. }
  2305. ////////////// Transform Compose
  2306. String VisualShaderNodeTransformCompose::get_caption() const {
  2307. return "TransformCompose";
  2308. }
  2309. int VisualShaderNodeTransformCompose::get_input_port_count() const {
  2310. return 4;
  2311. }
  2312. VisualShaderNodeTransformCompose::PortType VisualShaderNodeTransformCompose::get_input_port_type(int p_port) const {
  2313. return PORT_TYPE_VECTOR;
  2314. }
  2315. String VisualShaderNodeTransformCompose::get_input_port_name(int p_port) const {
  2316. if (p_port == 0) {
  2317. return "x";
  2318. } else if (p_port == 1) {
  2319. return "y";
  2320. } else if (p_port == 2) {
  2321. return "z";
  2322. } else {
  2323. return "origin";
  2324. }
  2325. }
  2326. int VisualShaderNodeTransformCompose::get_output_port_count() const {
  2327. return 1;
  2328. }
  2329. VisualShaderNodeTransformCompose::PortType VisualShaderNodeTransformCompose::get_output_port_type(int p_port) const {
  2330. return PORT_TYPE_TRANSFORM;
  2331. }
  2332. String VisualShaderNodeTransformCompose::get_output_port_name(int p_port) const {
  2333. return "xform";
  2334. }
  2335. String VisualShaderNodeTransformCompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2336. return "\t" + p_output_vars[0] + " = mat4(vec4(" + p_input_vars[0] + ", 0.0), vec4(" + p_input_vars[1] + ", 0.0), vec4(" + p_input_vars[2] + ", 0.0), vec4(" + p_input_vars[3] + ", 1.0));\n";
  2337. }
  2338. VisualShaderNodeTransformCompose::VisualShaderNodeTransformCompose() {
  2339. set_input_port_default_value(0, Vector3());
  2340. set_input_port_default_value(1, Vector3());
  2341. set_input_port_default_value(2, Vector3());
  2342. set_input_port_default_value(3, Vector3());
  2343. }
  2344. ////////////// Vector Decompose
  2345. String VisualShaderNodeVectorDecompose::get_caption() const {
  2346. return "VectorDecompose";
  2347. }
  2348. int VisualShaderNodeVectorDecompose::get_input_port_count() const {
  2349. return 1;
  2350. }
  2351. VisualShaderNodeVectorDecompose::PortType VisualShaderNodeVectorDecompose::get_input_port_type(int p_port) const {
  2352. return PORT_TYPE_VECTOR;
  2353. }
  2354. String VisualShaderNodeVectorDecompose::get_input_port_name(int p_port) const {
  2355. return "vec";
  2356. }
  2357. int VisualShaderNodeVectorDecompose::get_output_port_count() const {
  2358. return 3;
  2359. }
  2360. VisualShaderNodeVectorDecompose::PortType VisualShaderNodeVectorDecompose::get_output_port_type(int p_port) const {
  2361. return PORT_TYPE_SCALAR;
  2362. }
  2363. String VisualShaderNodeVectorDecompose::get_output_port_name(int p_port) const {
  2364. if (p_port == 0) {
  2365. return "x";
  2366. } else if (p_port == 1) {
  2367. return "y";
  2368. } else {
  2369. return "z";
  2370. }
  2371. }
  2372. String VisualShaderNodeVectorDecompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2373. String code;
  2374. code += "\t" + p_output_vars[0] + " = " + p_input_vars[0] + ".x;\n";
  2375. code += "\t" + p_output_vars[1] + " = " + p_input_vars[0] + ".y;\n";
  2376. code += "\t" + p_output_vars[2] + " = " + p_input_vars[0] + ".z;\n";
  2377. return code;
  2378. }
  2379. VisualShaderNodeVectorDecompose::VisualShaderNodeVectorDecompose() {
  2380. set_input_port_default_value(0, Vector3());
  2381. }
  2382. ////////////// Transform Decompose
  2383. String VisualShaderNodeTransformDecompose::get_caption() const {
  2384. return "TransformDecompose";
  2385. }
  2386. int VisualShaderNodeTransformDecompose::get_input_port_count() const {
  2387. return 1;
  2388. }
  2389. VisualShaderNodeTransformDecompose::PortType VisualShaderNodeTransformDecompose::get_input_port_type(int p_port) const {
  2390. return PORT_TYPE_TRANSFORM;
  2391. }
  2392. String VisualShaderNodeTransformDecompose::get_input_port_name(int p_port) const {
  2393. return "xform";
  2394. }
  2395. int VisualShaderNodeTransformDecompose::get_output_port_count() const {
  2396. return 4;
  2397. }
  2398. VisualShaderNodeTransformDecompose::PortType VisualShaderNodeTransformDecompose::get_output_port_type(int p_port) const {
  2399. return PORT_TYPE_VECTOR;
  2400. }
  2401. String VisualShaderNodeTransformDecompose::get_output_port_name(int p_port) const {
  2402. if (p_port == 0) {
  2403. return "x";
  2404. } else if (p_port == 1) {
  2405. return "y";
  2406. } else if (p_port == 2) {
  2407. return "z";
  2408. } else {
  2409. return "origin";
  2410. }
  2411. }
  2412. String VisualShaderNodeTransformDecompose::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2413. String code;
  2414. code += "\t" + p_output_vars[0] + " = " + p_input_vars[0] + "[0].xyz;\n";
  2415. code += "\t" + p_output_vars[1] + " = " + p_input_vars[0] + "[1].xyz;\n";
  2416. code += "\t" + p_output_vars[2] + " = " + p_input_vars[0] + "[2].xyz;\n";
  2417. code += "\t" + p_output_vars[3] + " = " + p_input_vars[0] + "[3].xyz;\n";
  2418. return code;
  2419. }
  2420. VisualShaderNodeTransformDecompose::VisualShaderNodeTransformDecompose() {
  2421. set_input_port_default_value(0, Transform());
  2422. }
  2423. ////////////// Scalar Uniform
  2424. String VisualShaderNodeScalarUniform::get_caption() const {
  2425. return "ScalarUniform";
  2426. }
  2427. int VisualShaderNodeScalarUniform::get_input_port_count() const {
  2428. return 0;
  2429. }
  2430. VisualShaderNodeScalarUniform::PortType VisualShaderNodeScalarUniform::get_input_port_type(int p_port) const {
  2431. return PORT_TYPE_SCALAR;
  2432. }
  2433. String VisualShaderNodeScalarUniform::get_input_port_name(int p_port) const {
  2434. return String();
  2435. }
  2436. int VisualShaderNodeScalarUniform::get_output_port_count() const {
  2437. return 1;
  2438. }
  2439. VisualShaderNodeScalarUniform::PortType VisualShaderNodeScalarUniform::get_output_port_type(int p_port) const {
  2440. return PORT_TYPE_SCALAR;
  2441. }
  2442. String VisualShaderNodeScalarUniform::get_output_port_name(int p_port) const {
  2443. return ""; //no output port means the editor will be used as port
  2444. }
  2445. String VisualShaderNodeScalarUniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  2446. String code = "uniform float " + get_uniform_name();
  2447. if (hint == HINT_RANGE) {
  2448. code += " : hint_range(" + rtos(hint_range_min) + ", " + rtos(hint_range_max) + ")";
  2449. } else if (hint == HINT_RANGE_STEP) {
  2450. code += " : hint_range(" + rtos(hint_range_min) + ", " + rtos(hint_range_max) + ", " + rtos(hint_range_step) + ")";
  2451. }
  2452. if (default_value_enabled) {
  2453. code += " = " + rtos(default_value);
  2454. }
  2455. code += ";\n";
  2456. return code;
  2457. }
  2458. String VisualShaderNodeScalarUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2459. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  2460. }
  2461. void VisualShaderNodeScalarUniform::set_hint(Hint p_hint) {
  2462. ERR_FAIL_INDEX(int(p_hint), int(HINT_MAX));
  2463. if (hint == p_hint) {
  2464. return;
  2465. }
  2466. hint = p_hint;
  2467. emit_changed();
  2468. }
  2469. VisualShaderNodeScalarUniform::Hint VisualShaderNodeScalarUniform::get_hint() const {
  2470. return hint;
  2471. }
  2472. void VisualShaderNodeScalarUniform::set_min(float p_value) {
  2473. if (Math::is_equal_approx(hint_range_min, p_value)) {
  2474. return;
  2475. }
  2476. hint_range_min = p_value;
  2477. emit_changed();
  2478. }
  2479. float VisualShaderNodeScalarUniform::get_min() const {
  2480. return hint_range_min;
  2481. }
  2482. void VisualShaderNodeScalarUniform::set_max(float p_value) {
  2483. if (Math::is_equal_approx(hint_range_max, p_value)) {
  2484. return;
  2485. }
  2486. hint_range_max = p_value;
  2487. emit_changed();
  2488. }
  2489. float VisualShaderNodeScalarUniform::get_max() const {
  2490. return hint_range_max;
  2491. }
  2492. void VisualShaderNodeScalarUniform::set_step(float p_value) {
  2493. if (Math::is_equal_approx(hint_range_step, p_value)) {
  2494. return;
  2495. }
  2496. hint_range_step = p_value;
  2497. emit_changed();
  2498. }
  2499. float VisualShaderNodeScalarUniform::get_step() const {
  2500. return hint_range_step;
  2501. }
  2502. void VisualShaderNodeScalarUniform::set_default_value_enabled(bool p_default_value_enabled) {
  2503. if (default_value_enabled == p_default_value_enabled) {
  2504. return;
  2505. }
  2506. default_value_enabled = p_default_value_enabled;
  2507. emit_changed();
  2508. }
  2509. bool VisualShaderNodeScalarUniform::is_default_value_enabled() const {
  2510. return default_value_enabled;
  2511. }
  2512. void VisualShaderNodeScalarUniform::set_default_value(float p_default_value) {
  2513. if (Math::is_equal_approx(default_value, p_default_value)) {
  2514. return;
  2515. }
  2516. default_value = p_default_value;
  2517. emit_changed();
  2518. }
  2519. float VisualShaderNodeScalarUniform::get_default_value() const {
  2520. return default_value;
  2521. }
  2522. Vector<StringName> VisualShaderNodeScalarUniform::get_editable_properties() const {
  2523. Vector<StringName> props = VisualShaderNodeUniform::get_editable_properties();
  2524. props.push_back("hint");
  2525. if (hint == HINT_RANGE || hint == HINT_RANGE_STEP) {
  2526. props.push_back("min");
  2527. props.push_back("max");
  2528. }
  2529. if (hint == HINT_RANGE_STEP) {
  2530. props.push_back("step");
  2531. }
  2532. props.push_back("default_value_enabled");
  2533. if (default_value_enabled) {
  2534. props.push_back("default_value");
  2535. }
  2536. return props;
  2537. }
  2538. void VisualShaderNodeScalarUniform::_bind_methods() {
  2539. ClassDB::bind_method(D_METHOD("set_hint", "hint"), &VisualShaderNodeScalarUniform::set_hint);
  2540. ClassDB::bind_method(D_METHOD("get_hint"), &VisualShaderNodeScalarUniform::get_hint);
  2541. ClassDB::bind_method(D_METHOD("set_min", "value"), &VisualShaderNodeScalarUniform::set_min);
  2542. ClassDB::bind_method(D_METHOD("get_min"), &VisualShaderNodeScalarUniform::get_min);
  2543. ClassDB::bind_method(D_METHOD("set_max", "value"), &VisualShaderNodeScalarUniform::set_max);
  2544. ClassDB::bind_method(D_METHOD("get_max"), &VisualShaderNodeScalarUniform::get_max);
  2545. ClassDB::bind_method(D_METHOD("set_step", "value"), &VisualShaderNodeScalarUniform::set_step);
  2546. ClassDB::bind_method(D_METHOD("get_step"), &VisualShaderNodeScalarUniform::get_step);
  2547. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeScalarUniform::set_default_value_enabled);
  2548. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeScalarUniform::is_default_value_enabled);
  2549. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeScalarUniform::set_default_value);
  2550. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeScalarUniform::get_default_value);
  2551. ADD_PROPERTY(PropertyInfo(Variant::INT, "hint", PROPERTY_HINT_ENUM, "None,Range,Range+Step"), "set_hint", "get_hint");
  2552. ADD_PROPERTY(PropertyInfo(Variant::REAL, "min"), "set_min", "get_min");
  2553. ADD_PROPERTY(PropertyInfo(Variant::REAL, "max"), "set_max", "get_max");
  2554. ADD_PROPERTY(PropertyInfo(Variant::REAL, "step"), "set_step", "get_step");
  2555. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  2556. ADD_PROPERTY(PropertyInfo(Variant::REAL, "default_value"), "set_default_value", "get_default_value");
  2557. BIND_ENUM_CONSTANT(HINT_NONE);
  2558. BIND_ENUM_CONSTANT(HINT_RANGE);
  2559. BIND_ENUM_CONSTANT(HINT_RANGE_STEP);
  2560. BIND_ENUM_CONSTANT(HINT_MAX);
  2561. }
  2562. VisualShaderNodeScalarUniform::VisualShaderNodeScalarUniform() {
  2563. hint = HINT_NONE;
  2564. hint_range_min = 0.0f;
  2565. hint_range_max = 1.0f;
  2566. hint_range_step = 0.1f;
  2567. default_value_enabled = false;
  2568. default_value = 0.0f;
  2569. }
  2570. ////////////// Boolean Uniform
  2571. String VisualShaderNodeBooleanUniform::get_caption() const {
  2572. return "BooleanUniform";
  2573. }
  2574. int VisualShaderNodeBooleanUniform::get_input_port_count() const {
  2575. return 0;
  2576. }
  2577. VisualShaderNodeBooleanUniform::PortType VisualShaderNodeBooleanUniform::get_input_port_type(int p_port) const {
  2578. return PORT_TYPE_BOOLEAN;
  2579. }
  2580. String VisualShaderNodeBooleanUniform::get_input_port_name(int p_port) const {
  2581. return String();
  2582. }
  2583. int VisualShaderNodeBooleanUniform::get_output_port_count() const {
  2584. return 1;
  2585. }
  2586. VisualShaderNodeBooleanUniform::PortType VisualShaderNodeBooleanUniform::get_output_port_type(int p_port) const {
  2587. return PORT_TYPE_BOOLEAN;
  2588. }
  2589. String VisualShaderNodeBooleanUniform::get_output_port_name(int p_port) const {
  2590. return ""; //no output port means the editor will be used as port
  2591. }
  2592. String VisualShaderNodeBooleanUniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  2593. String code = "uniform bool " + get_uniform_name();
  2594. if (default_value_enabled) {
  2595. if (default_value) {
  2596. code += " = true";
  2597. } else {
  2598. code += " = false";
  2599. }
  2600. }
  2601. code += ";\n";
  2602. return code;
  2603. }
  2604. String VisualShaderNodeBooleanUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2605. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  2606. }
  2607. void VisualShaderNodeBooleanUniform::set_default_value_enabled(bool p_default_value_enabled) {
  2608. if (default_value_enabled == p_default_value_enabled) {
  2609. return;
  2610. }
  2611. default_value_enabled = p_default_value_enabled;
  2612. emit_changed();
  2613. }
  2614. bool VisualShaderNodeBooleanUniform::is_default_value_enabled() const {
  2615. return default_value_enabled;
  2616. }
  2617. void VisualShaderNodeBooleanUniform::set_default_value(bool p_default_value) {
  2618. if (default_value == p_default_value) {
  2619. return;
  2620. }
  2621. default_value = p_default_value;
  2622. emit_changed();
  2623. }
  2624. bool VisualShaderNodeBooleanUniform::get_default_value() const {
  2625. return default_value;
  2626. }
  2627. void VisualShaderNodeBooleanUniform::_bind_methods() {
  2628. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeBooleanUniform::set_default_value_enabled);
  2629. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeBooleanUniform::is_default_value_enabled);
  2630. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeBooleanUniform::set_default_value);
  2631. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeBooleanUniform::get_default_value);
  2632. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  2633. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value"), "set_default_value", "get_default_value");
  2634. }
  2635. Vector<StringName> VisualShaderNodeBooleanUniform::get_editable_properties() const {
  2636. Vector<StringName> props = VisualShaderNodeUniform::get_editable_properties();
  2637. props.push_back("default_value_enabled");
  2638. if (default_value_enabled) {
  2639. props.push_back("default_value");
  2640. }
  2641. return props;
  2642. }
  2643. VisualShaderNodeBooleanUniform::VisualShaderNodeBooleanUniform() {
  2644. default_value_enabled = false;
  2645. default_value = false;
  2646. }
  2647. ////////////// Color Uniform
  2648. String VisualShaderNodeColorUniform::get_caption() const {
  2649. return "ColorUniform";
  2650. }
  2651. int VisualShaderNodeColorUniform::get_input_port_count() const {
  2652. return 0;
  2653. }
  2654. VisualShaderNodeColorUniform::PortType VisualShaderNodeColorUniform::get_input_port_type(int p_port) const {
  2655. return PORT_TYPE_VECTOR;
  2656. }
  2657. String VisualShaderNodeColorUniform::get_input_port_name(int p_port) const {
  2658. return String();
  2659. }
  2660. int VisualShaderNodeColorUniform::get_output_port_count() const {
  2661. return 2;
  2662. }
  2663. VisualShaderNodeColorUniform::PortType VisualShaderNodeColorUniform::get_output_port_type(int p_port) const {
  2664. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  2665. }
  2666. String VisualShaderNodeColorUniform::get_output_port_name(int p_port) const {
  2667. return p_port == 0 ? "color" : "alpha"; //no output port means the editor will be used as port
  2668. }
  2669. String VisualShaderNodeColorUniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  2670. String code = "uniform vec4 " + get_uniform_name() + " : hint_color";
  2671. if (default_value_enabled) {
  2672. code += vformat(" = vec4(%.6f, %.6f, %.6f, %.6f)", default_value.r, default_value.g, default_value.b, default_value.a);
  2673. }
  2674. code += ";\n";
  2675. return code;
  2676. }
  2677. String VisualShaderNodeColorUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2678. String code = "\t" + p_output_vars[0] + " = " + get_uniform_name() + ".rgb;\n";
  2679. code += "\t" + p_output_vars[1] + " = " + get_uniform_name() + ".a;\n";
  2680. return code;
  2681. }
  2682. void VisualShaderNodeColorUniform::set_default_value_enabled(bool p_enabled) {
  2683. if (default_value_enabled == p_enabled) {
  2684. return;
  2685. }
  2686. default_value_enabled = p_enabled;
  2687. emit_changed();
  2688. }
  2689. bool VisualShaderNodeColorUniform::is_default_value_enabled() const {
  2690. return default_value_enabled;
  2691. }
  2692. void VisualShaderNodeColorUniform::set_default_value(const Color &p_value) {
  2693. if (default_value.is_equal_approx(p_value)) {
  2694. return;
  2695. }
  2696. default_value = p_value;
  2697. emit_changed();
  2698. }
  2699. Color VisualShaderNodeColorUniform::get_default_value() const {
  2700. return default_value;
  2701. }
  2702. void VisualShaderNodeColorUniform::_bind_methods() {
  2703. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeColorUniform::set_default_value_enabled);
  2704. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeColorUniform::is_default_value_enabled);
  2705. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeColorUniform::set_default_value);
  2706. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeColorUniform::get_default_value);
  2707. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  2708. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "default_value"), "set_default_value", "get_default_value");
  2709. }
  2710. Vector<StringName> VisualShaderNodeColorUniform::get_editable_properties() const {
  2711. Vector<StringName> props = VisualShaderNodeUniform::get_editable_properties();
  2712. props.push_back("default_value_enabled");
  2713. if (default_value_enabled) {
  2714. props.push_back("default_value");
  2715. }
  2716. return props;
  2717. }
  2718. VisualShaderNodeColorUniform::VisualShaderNodeColorUniform() {
  2719. default_value_enabled = false;
  2720. default_value = Color(1.0, 1.0, 1.0, 1.0);
  2721. }
  2722. ////////////// Vector Uniform
  2723. String VisualShaderNodeVec3Uniform::get_caption() const {
  2724. return "VectorUniform";
  2725. }
  2726. int VisualShaderNodeVec3Uniform::get_input_port_count() const {
  2727. return 0;
  2728. }
  2729. VisualShaderNodeVec3Uniform::PortType VisualShaderNodeVec3Uniform::get_input_port_type(int p_port) const {
  2730. return PORT_TYPE_VECTOR;
  2731. }
  2732. String VisualShaderNodeVec3Uniform::get_input_port_name(int p_port) const {
  2733. return String();
  2734. }
  2735. int VisualShaderNodeVec3Uniform::get_output_port_count() const {
  2736. return 1;
  2737. }
  2738. VisualShaderNodeVec3Uniform::PortType VisualShaderNodeVec3Uniform::get_output_port_type(int p_port) const {
  2739. return PORT_TYPE_VECTOR;
  2740. }
  2741. String VisualShaderNodeVec3Uniform::get_output_port_name(int p_port) const {
  2742. return ""; //no output port means the editor will be used as port
  2743. }
  2744. String VisualShaderNodeVec3Uniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  2745. String code = "uniform vec3 " + get_uniform_name();
  2746. if (default_value_enabled) {
  2747. code += vformat(" = vec3(%.6f, %.6f, %.6f)", default_value.x, default_value.y, default_value.z);
  2748. }
  2749. code += ";\n";
  2750. return code;
  2751. }
  2752. String VisualShaderNodeVec3Uniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2753. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  2754. }
  2755. void VisualShaderNodeVec3Uniform::set_default_value_enabled(bool p_enabled) {
  2756. default_value_enabled = p_enabled;
  2757. emit_changed();
  2758. }
  2759. bool VisualShaderNodeVec3Uniform::is_default_value_enabled() const {
  2760. return default_value_enabled;
  2761. }
  2762. void VisualShaderNodeVec3Uniform::set_default_value(const Vector3 &p_value) {
  2763. default_value = p_value;
  2764. emit_changed();
  2765. }
  2766. Vector3 VisualShaderNodeVec3Uniform::get_default_value() const {
  2767. return default_value;
  2768. }
  2769. void VisualShaderNodeVec3Uniform::_bind_methods() {
  2770. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeVec3Uniform::set_default_value_enabled);
  2771. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeVec3Uniform::is_default_value_enabled);
  2772. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeVec3Uniform::set_default_value);
  2773. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeVec3Uniform::get_default_value);
  2774. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  2775. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "default_value"), "set_default_value", "get_default_value");
  2776. }
  2777. Vector<StringName> VisualShaderNodeVec3Uniform::get_editable_properties() const {
  2778. Vector<StringName> props = VisualShaderNodeUniform::get_editable_properties();
  2779. props.push_back("default_value_enabled");
  2780. if (default_value_enabled) {
  2781. props.push_back("default_value");
  2782. }
  2783. return props;
  2784. }
  2785. VisualShaderNodeVec3Uniform::VisualShaderNodeVec3Uniform() {
  2786. default_value_enabled = false;
  2787. }
  2788. ////////////// Transform Uniform
  2789. String VisualShaderNodeTransformUniform::get_caption() const {
  2790. return "TransformUniform";
  2791. }
  2792. int VisualShaderNodeTransformUniform::get_input_port_count() const {
  2793. return 0;
  2794. }
  2795. VisualShaderNodeTransformUniform::PortType VisualShaderNodeTransformUniform::get_input_port_type(int p_port) const {
  2796. return PORT_TYPE_VECTOR;
  2797. }
  2798. String VisualShaderNodeTransformUniform::get_input_port_name(int p_port) const {
  2799. return String();
  2800. }
  2801. int VisualShaderNodeTransformUniform::get_output_port_count() const {
  2802. return 1;
  2803. }
  2804. VisualShaderNodeTransformUniform::PortType VisualShaderNodeTransformUniform::get_output_port_type(int p_port) const {
  2805. return PORT_TYPE_TRANSFORM;
  2806. }
  2807. String VisualShaderNodeTransformUniform::get_output_port_name(int p_port) const {
  2808. return ""; //no output port means the editor will be used as port
  2809. }
  2810. String VisualShaderNodeTransformUniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  2811. String code = "uniform mat4 " + get_uniform_name();
  2812. if (default_value_enabled) {
  2813. Vector3 row0 = default_value.basis.get_row(0);
  2814. Vector3 row1 = default_value.basis.get_row(1);
  2815. Vector3 row2 = default_value.basis.get_row(2);
  2816. Vector3 origin = default_value.origin;
  2817. code += " = mat4(" + vformat("vec4(%.6f, %.6f, %.6f, 0.0)", row0.x, row0.y, row0.z) + vformat(", vec4(%.6f, %.6f, %.6f, 0.0)", row1.x, row1.y, row1.z) + vformat(", vec4(%.6f, %.6f, %.6f, 0.0)", row2.x, row2.y, row2.z) + vformat(", vec4(%.6f, %.6f, %.6f, 1.0)", origin.x, origin.y, origin.z) + ")";
  2818. }
  2819. code += ";\n";
  2820. return code;
  2821. }
  2822. String VisualShaderNodeTransformUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2823. return "\t" + p_output_vars[0] + " = " + get_uniform_name() + ";\n";
  2824. }
  2825. void VisualShaderNodeTransformUniform::set_default_value_enabled(bool p_enabled) {
  2826. default_value_enabled = p_enabled;
  2827. emit_changed();
  2828. }
  2829. bool VisualShaderNodeTransformUniform::is_default_value_enabled() const {
  2830. return default_value_enabled;
  2831. }
  2832. void VisualShaderNodeTransformUniform::set_default_value(const Transform &p_value) {
  2833. default_value = p_value;
  2834. emit_changed();
  2835. }
  2836. Transform VisualShaderNodeTransformUniform::get_default_value() const {
  2837. return default_value;
  2838. }
  2839. void VisualShaderNodeTransformUniform::_bind_methods() {
  2840. ClassDB::bind_method(D_METHOD("set_default_value_enabled", "enabled"), &VisualShaderNodeTransformUniform::set_default_value_enabled);
  2841. ClassDB::bind_method(D_METHOD("is_default_value_enabled"), &VisualShaderNodeTransformUniform::is_default_value_enabled);
  2842. ClassDB::bind_method(D_METHOD("set_default_value", "value"), &VisualShaderNodeTransformUniform::set_default_value);
  2843. ClassDB::bind_method(D_METHOD("get_default_value"), &VisualShaderNodeTransformUniform::get_default_value);
  2844. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "default_value_enabled"), "set_default_value_enabled", "is_default_value_enabled");
  2845. ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM, "default_value"), "set_default_value", "get_default_value");
  2846. }
  2847. Vector<StringName> VisualShaderNodeTransformUniform::get_editable_properties() const {
  2848. Vector<StringName> props = VisualShaderNodeUniform::get_editable_properties();
  2849. props.push_back("default_value_enabled");
  2850. if (default_value_enabled) {
  2851. props.push_back("default_value");
  2852. }
  2853. return props;
  2854. }
  2855. VisualShaderNodeTransformUniform::VisualShaderNodeTransformUniform() {
  2856. default_value_enabled = false;
  2857. default_value = Transform(1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0);
  2858. }
  2859. ////////////// Texture Uniform
  2860. String VisualShaderNodeTextureUniform::get_caption() const {
  2861. return "TextureUniform";
  2862. }
  2863. int VisualShaderNodeTextureUniform::get_input_port_count() const {
  2864. return 2;
  2865. }
  2866. VisualShaderNodeTextureUniform::PortType VisualShaderNodeTextureUniform::get_input_port_type(int p_port) const {
  2867. return p_port == 0 ? PORT_TYPE_VECTOR : PORT_TYPE_SCALAR;
  2868. }
  2869. String VisualShaderNodeTextureUniform::get_input_port_name(int p_port) const {
  2870. return p_port == 0 ? "uv" : "lod";
  2871. }
  2872. int VisualShaderNodeTextureUniform::get_output_port_count() const {
  2873. return 3;
  2874. }
  2875. VisualShaderNodeTextureUniform::PortType VisualShaderNodeTextureUniform::get_output_port_type(int p_port) const {
  2876. switch (p_port) {
  2877. case 0:
  2878. return PORT_TYPE_VECTOR;
  2879. case 1:
  2880. return PORT_TYPE_SCALAR;
  2881. case 2:
  2882. return PORT_TYPE_SAMPLER;
  2883. default:
  2884. return PORT_TYPE_SCALAR;
  2885. }
  2886. }
  2887. String VisualShaderNodeTextureUniform::get_output_port_name(int p_port) const {
  2888. switch (p_port) {
  2889. case 0:
  2890. return "rgb";
  2891. case 1:
  2892. return "alpha";
  2893. case 2:
  2894. return "sampler2D";
  2895. default:
  2896. return "";
  2897. }
  2898. }
  2899. bool VisualShaderNodeTextureUniform::is_code_generated() const {
  2900. return is_output_port_connected(0) || is_output_port_connected(1); // rgb or alpha
  2901. }
  2902. bool VisualShaderNodeTextureUniform::is_show_prop_names() const {
  2903. return false;
  2904. }
  2905. String VisualShaderNodeTextureUniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  2906. String code = "uniform sampler2D " + get_uniform_name();
  2907. switch (texture_type) {
  2908. case TYPE_DATA:
  2909. if (color_default == COLOR_DEFAULT_BLACK) {
  2910. code += " : hint_black;\n";
  2911. } else {
  2912. code += ";\n";
  2913. }
  2914. break;
  2915. case TYPE_COLOR:
  2916. if (color_default == COLOR_DEFAULT_BLACK) {
  2917. code += " : hint_black_albedo;\n";
  2918. } else {
  2919. code += " : hint_albedo;\n";
  2920. }
  2921. break;
  2922. case TYPE_NORMALMAP:
  2923. code += " : hint_normal;\n";
  2924. break;
  2925. case TYPE_ANISO:
  2926. code += " : hint_aniso;\n";
  2927. break;
  2928. }
  2929. return code;
  2930. }
  2931. String VisualShaderNodeTextureUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  2932. String id = get_uniform_name();
  2933. String code = "\t{\n";
  2934. if (p_input_vars[0] == String()) { // Use UV by default.
  2935. if (p_input_vars[1] == String()) {
  2936. code += "\t\tvec4 n_tex_read = texture(" + id + ", UV.xy);\n";
  2937. } else {
  2938. code += "\t\tvec4 n_tex_read = textureLod(" + id + ", UV.xy, " + p_input_vars[1] + ");\n";
  2939. }
  2940. } else if (p_input_vars[1] == String()) {
  2941. //no lod
  2942. code += "\t\tvec4 n_tex_read = texture(" + id + ", " + p_input_vars[0] + ".xy);\n";
  2943. } else {
  2944. code += "\t\tvec4 n_tex_read = textureLod(" + id + ", " + p_input_vars[0] + ".xy, " + p_input_vars[1] + ");\n";
  2945. }
  2946. code += "\t\t" + p_output_vars[0] + " = n_tex_read.rgb;\n";
  2947. code += "\t\t" + p_output_vars[1] + " = n_tex_read.a;\n";
  2948. code += "\t}\n";
  2949. return code;
  2950. }
  2951. void VisualShaderNodeTextureUniform::set_texture_type(TextureType p_type) {
  2952. texture_type = p_type;
  2953. emit_changed();
  2954. }
  2955. VisualShaderNodeTextureUniform::TextureType VisualShaderNodeTextureUniform::get_texture_type() const {
  2956. return texture_type;
  2957. }
  2958. void VisualShaderNodeTextureUniform::set_color_default(ColorDefault p_default) {
  2959. color_default = p_default;
  2960. emit_changed();
  2961. }
  2962. VisualShaderNodeTextureUniform::ColorDefault VisualShaderNodeTextureUniform::get_color_default() const {
  2963. return color_default;
  2964. }
  2965. Vector<StringName> VisualShaderNodeTextureUniform::get_editable_properties() const {
  2966. Vector<StringName> props;
  2967. props.push_back("texture_type");
  2968. props.push_back("color_default");
  2969. return props;
  2970. }
  2971. void VisualShaderNodeTextureUniform::_bind_methods() {
  2972. ClassDB::bind_method(D_METHOD("set_texture_type", "type"), &VisualShaderNodeTextureUniform::set_texture_type);
  2973. ClassDB::bind_method(D_METHOD("get_texture_type"), &VisualShaderNodeTextureUniform::get_texture_type);
  2974. ClassDB::bind_method(D_METHOD("set_color_default", "type"), &VisualShaderNodeTextureUniform::set_color_default);
  2975. ClassDB::bind_method(D_METHOD("get_color_default"), &VisualShaderNodeTextureUniform::get_color_default);
  2976. ADD_PROPERTY(PropertyInfo(Variant::INT, "texture_type", PROPERTY_HINT_ENUM, "Data,Color,Normalmap,Aniso"), "set_texture_type", "get_texture_type");
  2977. ADD_PROPERTY(PropertyInfo(Variant::INT, "color_default", PROPERTY_HINT_ENUM, "White Default,Black Default"), "set_color_default", "get_color_default");
  2978. BIND_ENUM_CONSTANT(TYPE_DATA);
  2979. BIND_ENUM_CONSTANT(TYPE_COLOR);
  2980. BIND_ENUM_CONSTANT(TYPE_NORMALMAP);
  2981. BIND_ENUM_CONSTANT(TYPE_ANISO);
  2982. BIND_ENUM_CONSTANT(COLOR_DEFAULT_WHITE);
  2983. BIND_ENUM_CONSTANT(COLOR_DEFAULT_BLACK);
  2984. }
  2985. String VisualShaderNodeTextureUniform::get_input_port_default_hint(int p_port) const {
  2986. if (p_port == 0) {
  2987. return "UV.xy";
  2988. }
  2989. return "";
  2990. }
  2991. VisualShaderNodeTextureUniform::VisualShaderNodeTextureUniform() {
  2992. texture_type = TYPE_DATA;
  2993. color_default = COLOR_DEFAULT_WHITE;
  2994. simple_decl = false;
  2995. }
  2996. ////////////// Texture Uniform (Triplanar)
  2997. String VisualShaderNodeTextureUniformTriplanar::get_caption() const {
  2998. return "TextureUniformTriplanar";
  2999. }
  3000. int VisualShaderNodeTextureUniformTriplanar::get_input_port_count() const {
  3001. return 2;
  3002. }
  3003. VisualShaderNodeTextureUniform::PortType VisualShaderNodeTextureUniformTriplanar::get_input_port_type(int p_port) const {
  3004. if (p_port == 0) {
  3005. return PORT_TYPE_VECTOR;
  3006. } else if (p_port == 1) {
  3007. return PORT_TYPE_VECTOR;
  3008. }
  3009. return PORT_TYPE_SCALAR;
  3010. }
  3011. String VisualShaderNodeTextureUniformTriplanar::get_input_port_name(int p_port) const {
  3012. if (p_port == 0) {
  3013. return "weights";
  3014. } else if (p_port == 1) {
  3015. return "pos";
  3016. }
  3017. return "";
  3018. }
  3019. String VisualShaderNodeTextureUniformTriplanar::generate_global_per_node(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  3020. String code;
  3021. code += "// TRIPLANAR FUNCTION GLOBAL CODE\n";
  3022. code += "\tvec4 triplanar_texture(sampler2D p_sampler, vec3 p_weights, vec3 p_triplanar_pos) {\n";
  3023. code += "\t\tvec4 samp = vec4(0.0);\n";
  3024. code += "\t\tsamp += texture(p_sampler, p_triplanar_pos.xy) * p_weights.z;\n";
  3025. code += "\t\tsamp += texture(p_sampler, p_triplanar_pos.xz) * p_weights.y;\n";
  3026. code += "\t\tsamp += texture(p_sampler, p_triplanar_pos.zy * vec2(-1.0, 1.0)) * p_weights.x;\n";
  3027. code += "\t\treturn samp;\n";
  3028. code += "\t}\n";
  3029. code += "\n";
  3030. code += "\tuniform vec3 triplanar_scale = vec3(1.0, 1.0, 1.0);\n";
  3031. code += "\tuniform vec3 triplanar_offset;\n";
  3032. code += "\tuniform float triplanar_sharpness = 0.5;\n";
  3033. code += "\n";
  3034. code += "\tvarying vec3 triplanar_power_normal;\n";
  3035. code += "\tvarying vec3 triplanar_pos;\n";
  3036. return code;
  3037. }
  3038. String VisualShaderNodeTextureUniformTriplanar::generate_global_per_func(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  3039. String code;
  3040. if (p_type == VisualShader::TYPE_VERTEX) {
  3041. code += "\t// TRIPLANAR FUNCTION VERTEX CODE\n";
  3042. code += "\t\ttriplanar_power_normal = pow(abs(NORMAL), vec3(triplanar_sharpness));\n";
  3043. code += "\t\ttriplanar_power_normal /= dot(triplanar_power_normal, vec3(1.0));\n";
  3044. code += "\t\ttriplanar_pos = VERTEX * triplanar_scale + triplanar_offset;\n";
  3045. code += "\t\ttriplanar_pos *= vec3(1.0, -1.0, 1.0);\n";
  3046. }
  3047. return code;
  3048. }
  3049. String VisualShaderNodeTextureUniformTriplanar::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3050. String id = get_uniform_name();
  3051. String code = "\t{\n";
  3052. if (p_input_vars[0] == String() && p_input_vars[1] == String()) {
  3053. code += "\t\tvec4 n_tex_read = triplanar_texture( " + id + ", triplanar_power_normal, triplanar_pos );\n";
  3054. } else if (p_input_vars[0] != String() && p_input_vars[1] == String()) {
  3055. code += "\t\tvec4 n_tex_read = triplanar_texture( " + id + ", " + p_input_vars[0] + ", triplanar_pos );\n";
  3056. } else if (p_input_vars[0] == String() && p_input_vars[1] != String()) {
  3057. code += "\t\tvec4 n_tex_read = triplanar_texture( " + id + ", triplanar_power_normal," + p_input_vars[1] + " );\n";
  3058. } else {
  3059. code += "\t\tvec4 n_tex_read = triplanar_texture( " + id + ", " + p_input_vars[0] + ", " + p_input_vars[1] + " );\n";
  3060. }
  3061. code += "\t\t" + p_output_vars[0] + " = n_tex_read.rgb;\n";
  3062. code += "\t\t" + p_output_vars[1] + " = n_tex_read.a;\n";
  3063. code += "\t}\n";
  3064. return code;
  3065. }
  3066. String VisualShaderNodeTextureUniformTriplanar::get_input_port_default_hint(int p_port) const {
  3067. if (p_port == 0) {
  3068. return "default";
  3069. } else if (p_port == 1) {
  3070. return "default";
  3071. }
  3072. return "";
  3073. }
  3074. VisualShaderNodeTextureUniformTriplanar::VisualShaderNodeTextureUniformTriplanar() {
  3075. }
  3076. ////////////// CubeMap Uniform
  3077. String VisualShaderNodeCubeMapUniform::get_caption() const {
  3078. return "CubeMapUniform";
  3079. }
  3080. int VisualShaderNodeCubeMapUniform::get_output_port_count() const {
  3081. return 1;
  3082. }
  3083. VisualShaderNodeCubeMapUniform::PortType VisualShaderNodeCubeMapUniform::get_output_port_type(int p_port) const {
  3084. return PORT_TYPE_SAMPLER;
  3085. }
  3086. String VisualShaderNodeCubeMapUniform::get_output_port_name(int p_port) const {
  3087. return "samplerCube";
  3088. }
  3089. int VisualShaderNodeCubeMapUniform::get_input_port_count() const {
  3090. return 0;
  3091. }
  3092. VisualShaderNodeCubeMapUniform::PortType VisualShaderNodeCubeMapUniform::get_input_port_type(int p_port) const {
  3093. return PORT_TYPE_SCALAR;
  3094. }
  3095. String VisualShaderNodeCubeMapUniform::get_input_port_name(int p_port) const {
  3096. return "";
  3097. }
  3098. String VisualShaderNodeCubeMapUniform::get_input_port_default_hint(int p_port) const {
  3099. return "";
  3100. }
  3101. String VisualShaderNodeCubeMapUniform::generate_global(Shader::Mode p_mode, VisualShader::Type p_type, int p_id) const {
  3102. String code = "uniform samplerCube " + get_uniform_name();
  3103. switch (texture_type) {
  3104. case TYPE_DATA:
  3105. if (color_default == COLOR_DEFAULT_BLACK) {
  3106. code += " : hint_black;\n";
  3107. } else {
  3108. code += ";\n";
  3109. }
  3110. break;
  3111. case TYPE_COLOR:
  3112. if (color_default == COLOR_DEFAULT_BLACK) {
  3113. code += " : hint_black_albedo;\n";
  3114. } else {
  3115. code += " : hint_albedo;\n";
  3116. }
  3117. break;
  3118. case TYPE_NORMALMAP:
  3119. code += " : hint_normal;\n";
  3120. break;
  3121. case TYPE_ANISO:
  3122. code += " : hint_aniso;\n";
  3123. break;
  3124. }
  3125. return code;
  3126. }
  3127. String VisualShaderNodeCubeMapUniform::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3128. return String();
  3129. }
  3130. VisualShaderNodeCubeMapUniform::VisualShaderNodeCubeMapUniform() {
  3131. }
  3132. ////////////// If
  3133. String VisualShaderNodeIf::get_caption() const {
  3134. return "If";
  3135. }
  3136. int VisualShaderNodeIf::get_input_port_count() const {
  3137. return 6;
  3138. }
  3139. VisualShaderNodeIf::PortType VisualShaderNodeIf::get_input_port_type(int p_port) const {
  3140. if (p_port == 0 || p_port == 1 || p_port == 2) {
  3141. return PORT_TYPE_SCALAR;
  3142. }
  3143. return PORT_TYPE_VECTOR;
  3144. }
  3145. String VisualShaderNodeIf::get_input_port_name(int p_port) const {
  3146. switch (p_port) {
  3147. case 0:
  3148. return "a";
  3149. case 1:
  3150. return "b";
  3151. case 2:
  3152. return "tolerance";
  3153. case 3:
  3154. return "a == b";
  3155. case 4:
  3156. return "a > b";
  3157. case 5:
  3158. return "a < b";
  3159. default:
  3160. return "";
  3161. }
  3162. }
  3163. int VisualShaderNodeIf::get_output_port_count() const {
  3164. return 1;
  3165. }
  3166. VisualShaderNodeIf::PortType VisualShaderNodeIf::get_output_port_type(int p_port) const {
  3167. return PORT_TYPE_VECTOR;
  3168. }
  3169. String VisualShaderNodeIf::get_output_port_name(int p_port) const {
  3170. return "result";
  3171. }
  3172. String VisualShaderNodeIf::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3173. String code;
  3174. code += "\tif(abs(" + p_input_vars[0] + " - " + p_input_vars[1] + ") < " + p_input_vars[2] + ")\n"; // abs(a - b) < tolerance eg. a == b
  3175. code += "\t{\n";
  3176. code += "\t\t" + p_output_vars[0] + " = " + p_input_vars[3] + ";\n";
  3177. code += "\t}\n";
  3178. code += "\telse if(" + p_input_vars[0] + " < " + p_input_vars[1] + ")\n"; // a < b
  3179. code += "\t{\n";
  3180. code += "\t\t" + p_output_vars[0] + " = " + p_input_vars[5] + ";\n";
  3181. code += "\t}\n";
  3182. code += "\telse\n"; // a > b (or a >= b if abs(a - b) < tolerance is false)
  3183. code += "\t{\n";
  3184. code += "\t\t" + p_output_vars[0] + " = " + p_input_vars[4] + ";\n";
  3185. code += "\t}\n";
  3186. return code;
  3187. }
  3188. VisualShaderNodeIf::VisualShaderNodeIf() {
  3189. set_input_port_default_value(0, 0.0);
  3190. set_input_port_default_value(1, 0.0);
  3191. set_input_port_default_value(2, CMP_EPSILON);
  3192. set_input_port_default_value(3, Vector3(0.0, 0.0, 0.0));
  3193. set_input_port_default_value(4, Vector3(0.0, 0.0, 0.0));
  3194. set_input_port_default_value(5, Vector3(0.0, 0.0, 0.0));
  3195. simple_decl = false;
  3196. }
  3197. ////////////// Switch
  3198. String VisualShaderNodeSwitch::get_caption() const {
  3199. return "VectorSwitch";
  3200. }
  3201. int VisualShaderNodeSwitch::get_input_port_count() const {
  3202. return 3;
  3203. }
  3204. VisualShaderNodeSwitch::PortType VisualShaderNodeSwitch::get_input_port_type(int p_port) const {
  3205. if (p_port == 0) {
  3206. return PORT_TYPE_BOOLEAN;
  3207. }
  3208. return PORT_TYPE_VECTOR;
  3209. }
  3210. String VisualShaderNodeSwitch::get_input_port_name(int p_port) const {
  3211. switch (p_port) {
  3212. case 0:
  3213. return "value";
  3214. case 1:
  3215. return "true";
  3216. case 2:
  3217. return "false";
  3218. default:
  3219. return "";
  3220. }
  3221. }
  3222. int VisualShaderNodeSwitch::get_output_port_count() const {
  3223. return 1;
  3224. }
  3225. VisualShaderNodeSwitch::PortType VisualShaderNodeSwitch::get_output_port_type(int p_port) const {
  3226. return PORT_TYPE_VECTOR;
  3227. }
  3228. String VisualShaderNodeSwitch::get_output_port_name(int p_port) const {
  3229. return "result";
  3230. }
  3231. String VisualShaderNodeSwitch::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3232. String code;
  3233. code += "\tif(" + p_input_vars[0] + ")\n";
  3234. code += "\t{\n";
  3235. code += "\t\t" + p_output_vars[0] + " = " + p_input_vars[1] + ";\n";
  3236. code += "\t}\n";
  3237. code += "\telse\n";
  3238. code += "\t{\n";
  3239. code += "\t\t" + p_output_vars[0] + " = " + p_input_vars[2] + ";\n";
  3240. code += "\t}\n";
  3241. return code;
  3242. }
  3243. VisualShaderNodeSwitch::VisualShaderNodeSwitch() {
  3244. set_input_port_default_value(0, false);
  3245. set_input_port_default_value(1, Vector3(1.0, 1.0, 1.0));
  3246. set_input_port_default_value(2, Vector3(0.0, 0.0, 0.0));
  3247. simple_decl = false;
  3248. }
  3249. ////////////// Switch(scalar)
  3250. String VisualShaderNodeScalarSwitch::get_caption() const {
  3251. return "ScalarSwitch";
  3252. }
  3253. VisualShaderNodeScalarSwitch::PortType VisualShaderNodeScalarSwitch::get_input_port_type(int p_port) const {
  3254. if (p_port == 0) {
  3255. return PORT_TYPE_BOOLEAN;
  3256. }
  3257. return PORT_TYPE_SCALAR;
  3258. }
  3259. VisualShaderNodeScalarSwitch::PortType VisualShaderNodeScalarSwitch::get_output_port_type(int p_port) const {
  3260. return PORT_TYPE_SCALAR;
  3261. }
  3262. VisualShaderNodeScalarSwitch::VisualShaderNodeScalarSwitch() {
  3263. set_input_port_default_value(0, false);
  3264. set_input_port_default_value(1, 1.0);
  3265. set_input_port_default_value(2, 0.0);
  3266. }
  3267. ////////////// Fresnel
  3268. String VisualShaderNodeFresnel::get_caption() const {
  3269. return "Fresnel";
  3270. }
  3271. int VisualShaderNodeFresnel::get_input_port_count() const {
  3272. return 4;
  3273. }
  3274. VisualShaderNodeFresnel::PortType VisualShaderNodeFresnel::get_input_port_type(int p_port) const {
  3275. switch (p_port) {
  3276. case 0:
  3277. return PORT_TYPE_VECTOR;
  3278. case 1:
  3279. return PORT_TYPE_VECTOR;
  3280. case 2:
  3281. return PORT_TYPE_BOOLEAN;
  3282. case 3:
  3283. return PORT_TYPE_SCALAR;
  3284. default:
  3285. return PORT_TYPE_VECTOR;
  3286. }
  3287. }
  3288. String VisualShaderNodeFresnel::get_input_port_name(int p_port) const {
  3289. switch (p_port) {
  3290. case 0:
  3291. return "normal";
  3292. case 1:
  3293. return "view";
  3294. case 2:
  3295. return "invert";
  3296. case 3:
  3297. return "power";
  3298. default:
  3299. return "";
  3300. }
  3301. }
  3302. int VisualShaderNodeFresnel::get_output_port_count() const {
  3303. return 1;
  3304. }
  3305. VisualShaderNodeFresnel::PortType VisualShaderNodeFresnel::get_output_port_type(int p_port) const {
  3306. return PORT_TYPE_SCALAR;
  3307. }
  3308. String VisualShaderNodeFresnel::get_output_port_name(int p_port) const {
  3309. return "result";
  3310. }
  3311. bool VisualShaderNodeFresnel::is_generate_input_var(int p_port) const {
  3312. if (p_port == 2) {
  3313. return false;
  3314. }
  3315. return true;
  3316. }
  3317. String VisualShaderNodeFresnel::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3318. String normal;
  3319. String view;
  3320. if (p_input_vars[0] == String()) {
  3321. normal = "NORMAL";
  3322. } else {
  3323. normal = p_input_vars[0];
  3324. }
  3325. if (p_input_vars[1] == String()) {
  3326. view = "VIEW";
  3327. } else {
  3328. view = p_input_vars[1];
  3329. }
  3330. if (is_input_port_connected(2)) {
  3331. return "\t" + p_output_vars[0] + " = " + p_input_vars[2] + " ? (pow(clamp(dot(" + normal + ", " + view + "), 0.0, 1.0), " + p_input_vars[3] + ")) : (pow(1.0 - clamp(dot(" + normal + ", " + view + "), 0.0, 1.0), " + p_input_vars[3] + "));\n";
  3332. } else {
  3333. if (get_input_port_default_value(2)) {
  3334. return "\t" + p_output_vars[0] + " = pow(clamp(dot(" + normal + ", " + view + "), 0.0, 1.0), " + p_input_vars[3] + ");\n";
  3335. } else {
  3336. return "\t" + p_output_vars[0] + " = pow(1.0 - clamp(dot(" + normal + ", " + view + "), 0.0, 1.0), " + p_input_vars[3] + ");\n";
  3337. }
  3338. }
  3339. }
  3340. String VisualShaderNodeFresnel::get_input_port_default_hint(int p_port) const {
  3341. if (p_port == 0) {
  3342. return "default";
  3343. } else if (p_port == 1) {
  3344. return "default";
  3345. }
  3346. return "";
  3347. }
  3348. VisualShaderNodeFresnel::VisualShaderNodeFresnel() {
  3349. set_input_port_default_value(2, false);
  3350. set_input_port_default_value(3, 1.0);
  3351. }
  3352. ////////////// Is
  3353. String VisualShaderNodeIs::get_caption() const {
  3354. return "Is";
  3355. }
  3356. int VisualShaderNodeIs::get_input_port_count() const {
  3357. return 1;
  3358. }
  3359. VisualShaderNodeIs::PortType VisualShaderNodeIs::get_input_port_type(int p_port) const {
  3360. return PORT_TYPE_SCALAR;
  3361. }
  3362. String VisualShaderNodeIs::get_input_port_name(int p_port) const {
  3363. return "";
  3364. }
  3365. int VisualShaderNodeIs::get_output_port_count() const {
  3366. return 1;
  3367. }
  3368. VisualShaderNodeIs::PortType VisualShaderNodeIs::get_output_port_type(int p_port) const {
  3369. return PORT_TYPE_BOOLEAN;
  3370. }
  3371. String VisualShaderNodeIs::get_output_port_name(int p_port) const {
  3372. return "";
  3373. }
  3374. String VisualShaderNodeIs::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3375. static const char *funcs[FUNC_IS_NAN + 1] = {
  3376. "isinf($)",
  3377. "isnan($)"
  3378. };
  3379. String code;
  3380. code += "\t" + p_output_vars[0] + " = " + String(funcs[func]).replace("$", p_input_vars[0]) + ";\n";
  3381. return code;
  3382. }
  3383. void VisualShaderNodeIs::set_function(Function p_func) {
  3384. func = p_func;
  3385. emit_changed();
  3386. }
  3387. VisualShaderNodeIs::Function VisualShaderNodeIs::get_function() const {
  3388. return func;
  3389. }
  3390. Vector<StringName> VisualShaderNodeIs::get_editable_properties() const {
  3391. Vector<StringName> props;
  3392. props.push_back("function");
  3393. return props;
  3394. }
  3395. void VisualShaderNodeIs::_bind_methods() {
  3396. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeIs::set_function);
  3397. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeIs::get_function);
  3398. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "Inf,NaN"), "set_function", "get_function");
  3399. BIND_ENUM_CONSTANT(FUNC_IS_INF);
  3400. BIND_ENUM_CONSTANT(FUNC_IS_NAN);
  3401. }
  3402. VisualShaderNodeIs::VisualShaderNodeIs() {
  3403. func = FUNC_IS_INF;
  3404. set_input_port_default_value(0, 0.0);
  3405. }
  3406. ////////////// Compare
  3407. String VisualShaderNodeCompare::get_caption() const {
  3408. return "Compare";
  3409. }
  3410. int VisualShaderNodeCompare::get_input_port_count() const {
  3411. if (ctype == CTYPE_SCALAR && (func == FUNC_EQUAL || func == FUNC_NOT_EQUAL)) {
  3412. return 3;
  3413. }
  3414. return 2;
  3415. }
  3416. VisualShaderNodeCompare::PortType VisualShaderNodeCompare::get_input_port_type(int p_port) const {
  3417. if (p_port == 2) {
  3418. return PORT_TYPE_SCALAR;
  3419. }
  3420. switch (ctype) {
  3421. case CTYPE_SCALAR:
  3422. return PORT_TYPE_SCALAR;
  3423. case CTYPE_VECTOR:
  3424. return PORT_TYPE_VECTOR;
  3425. case CTYPE_BOOLEAN:
  3426. return PORT_TYPE_BOOLEAN;
  3427. case CTYPE_TRANSFORM:
  3428. return PORT_TYPE_TRANSFORM;
  3429. }
  3430. return PORT_TYPE_VECTOR;
  3431. }
  3432. String VisualShaderNodeCompare::get_input_port_name(int p_port) const {
  3433. if (p_port == 0) {
  3434. return "a";
  3435. } else if (p_port == 1) {
  3436. return "b";
  3437. } else if (p_port == 2) {
  3438. return "tolerance";
  3439. }
  3440. return "";
  3441. }
  3442. int VisualShaderNodeCompare::get_output_port_count() const {
  3443. return 1;
  3444. }
  3445. VisualShaderNodeCompare::PortType VisualShaderNodeCompare::get_output_port_type(int p_port) const {
  3446. return PORT_TYPE_BOOLEAN;
  3447. }
  3448. String VisualShaderNodeCompare::get_output_port_name(int p_port) const {
  3449. if (p_port == 0) {
  3450. return "result";
  3451. }
  3452. return "";
  3453. }
  3454. String VisualShaderNodeCompare::get_warning(Shader::Mode p_mode, VisualShader::Type p_type) const {
  3455. if (ctype == CTYPE_BOOLEAN || ctype == CTYPE_TRANSFORM) {
  3456. if (func > FUNC_NOT_EQUAL) {
  3457. return TTR("Invalid comparison function for that type.");
  3458. }
  3459. }
  3460. return "";
  3461. }
  3462. String VisualShaderNodeCompare::generate_code(Shader::Mode p_mode, VisualShader::Type p_type, int p_id, const String *p_input_vars, const String *p_output_vars, bool p_for_preview) const {
  3463. static const char *ops[FUNC_LESS_THAN_EQUAL + 1] = {
  3464. "==",
  3465. "!=",
  3466. ">",
  3467. ">=",
  3468. "<",
  3469. "<=",
  3470. };
  3471. static const char *funcs[FUNC_LESS_THAN_EQUAL + 1] = {
  3472. "equal($)",
  3473. "notEqual($)",
  3474. "greaterThan($)",
  3475. "greaterThanEqual($)",
  3476. "lessThan($)",
  3477. "lessThanEqual($)",
  3478. };
  3479. static const char *conds[COND_ANY + 1] = {
  3480. "all($)",
  3481. "any($)",
  3482. };
  3483. String code;
  3484. switch (ctype) {
  3485. case CTYPE_SCALAR:
  3486. if (func == FUNC_EQUAL) {
  3487. code += "\t" + p_output_vars[0] + " = (abs(" + p_input_vars[0] + " - " + p_input_vars[1] + ") < " + p_input_vars[2] + ");";
  3488. } else if (func == FUNC_NOT_EQUAL) {
  3489. code += "\t" + p_output_vars[0] + " = !(abs(" + p_input_vars[0] + " - " + p_input_vars[1] + ") < " + p_input_vars[2] + ");";
  3490. } else {
  3491. code += "\t" + p_output_vars[0] + " = " + (p_input_vars[0] + "$" + p_input_vars[1]).replace("$", ops[func]) + ";\n";
  3492. }
  3493. break;
  3494. case CTYPE_VECTOR:
  3495. code += "\t{\n";
  3496. code += "\t\tbvec3 _bv = " + String(funcs[func]).replace("$", p_input_vars[0] + ", " + p_input_vars[1]) + ";\n";
  3497. code += "\t\t" + p_output_vars[0] + " = " + String(conds[condition]).replace("$", "_bv") + ";\n";
  3498. code += "\t}\n";
  3499. break;
  3500. case CTYPE_BOOLEAN:
  3501. if (func > FUNC_NOT_EQUAL) {
  3502. return "\t" + p_output_vars[0] + " = false;\n";
  3503. }
  3504. code += "\t" + p_output_vars[0] + " = " + (p_input_vars[0] + " $ " + p_input_vars[1]).replace("$", ops[func]) + ";\n";
  3505. break;
  3506. case CTYPE_TRANSFORM:
  3507. if (func > FUNC_NOT_EQUAL) {
  3508. return "\t" + p_output_vars[0] + " = false;\n";
  3509. }
  3510. code += "\t" + p_output_vars[0] + " = " + (p_input_vars[0] + " $ " + p_input_vars[1]).replace("$", ops[func]) + ";\n";
  3511. break;
  3512. default:
  3513. break;
  3514. }
  3515. return code;
  3516. }
  3517. void VisualShaderNodeCompare::set_comparison_type(ComparisonType p_type) {
  3518. ctype = p_type;
  3519. switch (ctype) {
  3520. case CTYPE_SCALAR:
  3521. set_input_port_default_value(0, 0.0);
  3522. set_input_port_default_value(1, 0.0);
  3523. simple_decl = true;
  3524. break;
  3525. case CTYPE_VECTOR:
  3526. set_input_port_default_value(0, Vector3(0.0, 0.0, 0.0));
  3527. set_input_port_default_value(1, Vector3(0.0, 0.0, 0.0));
  3528. simple_decl = false;
  3529. break;
  3530. case CTYPE_BOOLEAN:
  3531. set_input_port_default_value(0, false);
  3532. set_input_port_default_value(1, false);
  3533. simple_decl = true;
  3534. break;
  3535. case CTYPE_TRANSFORM:
  3536. set_input_port_default_value(0, Transform());
  3537. set_input_port_default_value(1, Transform());
  3538. simple_decl = true;
  3539. break;
  3540. }
  3541. emit_changed();
  3542. }
  3543. VisualShaderNodeCompare::ComparisonType VisualShaderNodeCompare::get_comparison_type() const {
  3544. return ctype;
  3545. }
  3546. void VisualShaderNodeCompare::set_function(Function p_func) {
  3547. func = p_func;
  3548. emit_changed();
  3549. }
  3550. VisualShaderNodeCompare::Function VisualShaderNodeCompare::get_function() const {
  3551. return func;
  3552. }
  3553. void VisualShaderNodeCompare::set_condition(Condition p_cond) {
  3554. condition = p_cond;
  3555. emit_changed();
  3556. }
  3557. VisualShaderNodeCompare::Condition VisualShaderNodeCompare::get_condition() const {
  3558. return condition;
  3559. }
  3560. Vector<StringName> VisualShaderNodeCompare::get_editable_properties() const {
  3561. Vector<StringName> props;
  3562. props.push_back("type");
  3563. props.push_back("function");
  3564. if (ctype == CTYPE_VECTOR) {
  3565. props.push_back("condition");
  3566. }
  3567. return props;
  3568. }
  3569. void VisualShaderNodeCompare::_bind_methods() {
  3570. ClassDB::bind_method(D_METHOD("set_comparison_type", "type"), &VisualShaderNodeCompare::set_comparison_type);
  3571. ClassDB::bind_method(D_METHOD("get_comparison_type"), &VisualShaderNodeCompare::get_comparison_type);
  3572. ClassDB::bind_method(D_METHOD("set_function", "func"), &VisualShaderNodeCompare::set_function);
  3573. ClassDB::bind_method(D_METHOD("get_function"), &VisualShaderNodeCompare::get_function);
  3574. ClassDB::bind_method(D_METHOD("set_condition", "condition"), &VisualShaderNodeCompare::set_condition);
  3575. ClassDB::bind_method(D_METHOD("get_condition"), &VisualShaderNodeCompare::get_condition);
  3576. ADD_PROPERTY(PropertyInfo(Variant::INT, "type", PROPERTY_HINT_ENUM, "Scalar,Vector,Boolean,Transform"), "set_comparison_type", "get_comparison_type");
  3577. ADD_PROPERTY(PropertyInfo(Variant::INT, "function", PROPERTY_HINT_ENUM, "a == b,a != b,a > b,a >= b,a < b,a <= b"), "set_function", "get_function");
  3578. ADD_PROPERTY(PropertyInfo(Variant::INT, "condition", PROPERTY_HINT_ENUM, "All,Any"), "set_condition", "get_condition");
  3579. BIND_ENUM_CONSTANT(CTYPE_SCALAR);
  3580. BIND_ENUM_CONSTANT(CTYPE_VECTOR);
  3581. BIND_ENUM_CONSTANT(CTYPE_BOOLEAN);
  3582. BIND_ENUM_CONSTANT(CTYPE_TRANSFORM);
  3583. BIND_ENUM_CONSTANT(FUNC_EQUAL);
  3584. BIND_ENUM_CONSTANT(FUNC_NOT_EQUAL);
  3585. BIND_ENUM_CONSTANT(FUNC_GREATER_THAN);
  3586. BIND_ENUM_CONSTANT(FUNC_GREATER_THAN_EQUAL);
  3587. BIND_ENUM_CONSTANT(FUNC_LESS_THAN);
  3588. BIND_ENUM_CONSTANT(FUNC_LESS_THAN_EQUAL);
  3589. BIND_ENUM_CONSTANT(COND_ALL);
  3590. BIND_ENUM_CONSTANT(COND_ANY);
  3591. }
  3592. VisualShaderNodeCompare::VisualShaderNodeCompare() {
  3593. ctype = CTYPE_SCALAR;
  3594. func = FUNC_EQUAL;
  3595. condition = COND_ALL;
  3596. set_input_port_default_value(0, 0.0);
  3597. set_input_port_default_value(1, 0.0);
  3598. set_input_port_default_value(2, CMP_EPSILON);
  3599. }