node_3d_editor_plugin.cpp 301 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249
  1. /*************************************************************************/
  2. /* node_3d_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "node_3d_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/input/input_map.h"
  34. #include "core/math/camera_matrix.h"
  35. #include "core/math/math_funcs.h"
  36. #include "core/os/keyboard.h"
  37. #include "core/templates/sort_array.h"
  38. #include "editor/debugger/editor_debugger_node.h"
  39. #include "editor/editor_node.h"
  40. #include "editor/editor_settings.h"
  41. #include "editor/plugins/animation_player_editor_plugin.h"
  42. #include "editor/plugins/node_3d_editor_gizmos.h"
  43. #include "editor/plugins/script_editor_plugin.h"
  44. #include "editor/scene_tree_dock.h"
  45. #include "scene/3d/camera_3d.h"
  46. #include "scene/3d/collision_shape_3d.h"
  47. #include "scene/3d/light_3d.h"
  48. #include "scene/3d/mesh_instance_3d.h"
  49. #include "scene/3d/physics_body_3d.h"
  50. #include "scene/3d/visual_instance_3d.h"
  51. #include "scene/3d/world_environment.h"
  52. #include "scene/gui/center_container.h"
  53. #include "scene/gui/subviewport_container.h"
  54. #include "scene/resources/packed_scene.h"
  55. #include "scene/resources/surface_tool.h"
  56. constexpr real_t DISTANCE_DEFAULT = 4;
  57. constexpr real_t GIZMO_ARROW_SIZE = 0.35;
  58. constexpr real_t GIZMO_RING_HALF_WIDTH = 0.1;
  59. constexpr real_t GIZMO_PLANE_SIZE = 0.2;
  60. constexpr real_t GIZMO_PLANE_DST = 0.3;
  61. constexpr real_t GIZMO_CIRCLE_SIZE = 1.1;
  62. constexpr real_t GIZMO_SCALE_OFFSET = GIZMO_CIRCLE_SIZE + 0.3;
  63. constexpr real_t GIZMO_ARROW_OFFSET = GIZMO_CIRCLE_SIZE + 0.3;
  64. constexpr real_t ZOOM_FREELOOK_MIN = 0.01;
  65. constexpr real_t ZOOM_FREELOOK_MULTIPLIER = 1.08;
  66. constexpr real_t ZOOM_FREELOOK_INDICATOR_DELAY_S = 1.5;
  67. #ifdef REAL_T_IS_DOUBLE
  68. constexpr double ZOOM_FREELOOK_MAX = 1'000'000'000'000;
  69. #else
  70. constexpr float ZOOM_FREELOOK_MAX = 10'000;
  71. #endif
  72. constexpr real_t MIN_Z = 0.01;
  73. constexpr real_t MAX_Z = 1000000.0;
  74. constexpr real_t MIN_FOV = 0.01;
  75. constexpr real_t MAX_FOV = 179;
  76. void ViewportRotationControl::_notification(int p_what) {
  77. switch (p_what) {
  78. case NOTIFICATION_ENTER_TREE: {
  79. axis_menu_options.clear();
  80. axis_menu_options.push_back(Node3DEditorViewport::VIEW_RIGHT);
  81. axis_menu_options.push_back(Node3DEditorViewport::VIEW_TOP);
  82. axis_menu_options.push_back(Node3DEditorViewport::VIEW_REAR);
  83. axis_menu_options.push_back(Node3DEditorViewport::VIEW_LEFT);
  84. axis_menu_options.push_back(Node3DEditorViewport::VIEW_BOTTOM);
  85. axis_menu_options.push_back(Node3DEditorViewport::VIEW_FRONT);
  86. axis_colors.clear();
  87. axis_colors.push_back(get_theme_color(SNAME("axis_x_color"), SNAME("Editor")));
  88. axis_colors.push_back(get_theme_color(SNAME("axis_y_color"), SNAME("Editor")));
  89. axis_colors.push_back(get_theme_color(SNAME("axis_z_color"), SNAME("Editor")));
  90. update();
  91. if (!is_connected("mouse_exited", callable_mp(this, &ViewportRotationControl::_on_mouse_exited))) {
  92. connect("mouse_exited", callable_mp(this, &ViewportRotationControl::_on_mouse_exited));
  93. }
  94. } break;
  95. case NOTIFICATION_DRAW: {
  96. if (viewport != nullptr) {
  97. _draw();
  98. }
  99. } break;
  100. }
  101. }
  102. void ViewportRotationControl::_draw() {
  103. const Vector2i center = get_size() / 2.0;
  104. const real_t radius = get_size().x / 2.0;
  105. if (focused_axis > -2 || orbiting) {
  106. draw_circle(center, radius, Color(0.5, 0.5, 0.5, 0.25));
  107. }
  108. Vector<Axis2D> axis_to_draw;
  109. _get_sorted_axis(axis_to_draw);
  110. for (int i = 0; i < axis_to_draw.size(); ++i) {
  111. _draw_axis(axis_to_draw[i]);
  112. }
  113. }
  114. void ViewportRotationControl::_draw_axis(const Axis2D &p_axis) {
  115. const bool focused = focused_axis == p_axis.axis;
  116. const bool positive = p_axis.axis < 3;
  117. const int direction = p_axis.axis % 3;
  118. const Color axis_color = axis_colors[direction];
  119. const double alpha = focused ? 1.0 : ((p_axis.z_axis + 1.0) / 2.0) * 0.5 + 0.5;
  120. const Color c = focused ? Color(0.9, 0.9, 0.9) : Color(axis_color, alpha);
  121. if (positive) {
  122. // Draw axis lines for the positive axes.
  123. const Vector2i center = get_size() / 2.0;
  124. draw_line(center, p_axis.screen_point, c, 1.5 * EDSCALE);
  125. draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS, c);
  126. // Draw the axis letter for the positive axes.
  127. const String axis_name = direction == 0 ? "X" : (direction == 1 ? "Y" : "Z");
  128. draw_char(get_theme_font(SNAME("rotation_control"), SNAME("EditorFonts")), p_axis.screen_point + Vector2i(Math::round(-4.0 * EDSCALE), Math::round(5.0 * EDSCALE)), axis_name, "", get_theme_font_size(SNAME("rotation_control_size"), SNAME("EditorFonts")), Color(0.0, 0.0, 0.0, alpha));
  129. } else {
  130. // Draw an outline around the negative axes.
  131. draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS, c);
  132. draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS * 0.8, c.darkened(0.4));
  133. }
  134. }
  135. void ViewportRotationControl::_get_sorted_axis(Vector<Axis2D> &r_axis) {
  136. const Vector2i center = get_size() / 2.0;
  137. const real_t radius = get_size().x / 2.0 - AXIS_CIRCLE_RADIUS - 2.0 * EDSCALE;
  138. const Basis camera_basis = viewport->to_camera_transform(viewport->cursor).get_basis().inverse();
  139. for (int i = 0; i < 3; ++i) {
  140. Vector3 axis_3d = camera_basis.get_column(i);
  141. Vector2i axis_vector = Vector2(axis_3d.x, -axis_3d.y) * radius;
  142. if (Math::abs(axis_3d.z) < 1.0) {
  143. Axis2D pos_axis;
  144. pos_axis.axis = i;
  145. pos_axis.screen_point = center + axis_vector;
  146. pos_axis.z_axis = axis_3d.z;
  147. r_axis.push_back(pos_axis);
  148. Axis2D neg_axis;
  149. neg_axis.axis = i + 3;
  150. neg_axis.screen_point = center - axis_vector;
  151. neg_axis.z_axis = -axis_3d.z;
  152. r_axis.push_back(neg_axis);
  153. } else {
  154. // Special case when the camera is aligned with one axis
  155. Axis2D axis;
  156. axis.axis = i + (axis_3d.z < 0 ? 0 : 3);
  157. axis.screen_point = center;
  158. axis.z_axis = 1.0;
  159. r_axis.push_back(axis);
  160. }
  161. }
  162. r_axis.sort_custom<Axis2DCompare>();
  163. }
  164. void ViewportRotationControl::gui_input(const Ref<InputEvent> &p_event) {
  165. ERR_FAIL_COND(p_event.is_null());
  166. const Ref<InputEventMouseButton> mb = p_event;
  167. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  168. Vector2 pos = mb->get_position();
  169. if (mb->is_pressed()) {
  170. if (pos.distance_to(get_size() / 2.0) < get_size().x / 2.0) {
  171. orbiting = true;
  172. }
  173. } else {
  174. if (focused_axis > -1) {
  175. viewport->_menu_option(axis_menu_options[focused_axis]);
  176. _update_focus();
  177. }
  178. orbiting = false;
  179. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
  180. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  181. Input::get_singleton()->warp_mouse(orbiting_mouse_start);
  182. }
  183. }
  184. }
  185. const Ref<InputEventMouseMotion> mm = p_event;
  186. if (mm.is_valid()) {
  187. if (orbiting) {
  188. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) {
  189. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  190. orbiting_mouse_start = mm->get_global_position();
  191. }
  192. viewport->_nav_orbit(mm, viewport->_get_warped_mouse_motion(mm));
  193. focused_axis = -1;
  194. } else {
  195. _update_focus();
  196. }
  197. }
  198. }
  199. void ViewportRotationControl::_update_focus() {
  200. int original_focus = focused_axis;
  201. focused_axis = -2;
  202. Vector2 mouse_pos = get_local_mouse_position();
  203. if (mouse_pos.distance_to(get_size() / 2.0) < get_size().x / 2.0) {
  204. focused_axis = -1;
  205. }
  206. Vector<Axis2D> axes;
  207. _get_sorted_axis(axes);
  208. for (int i = 0; i < axes.size(); i++) {
  209. const Axis2D &axis = axes[i];
  210. if (mouse_pos.distance_to(axis.screen_point) < AXIS_CIRCLE_RADIUS) {
  211. focused_axis = axis.axis;
  212. }
  213. }
  214. if (focused_axis != original_focus) {
  215. update();
  216. }
  217. }
  218. void ViewportRotationControl::_on_mouse_exited() {
  219. focused_axis = -2;
  220. update();
  221. }
  222. void ViewportRotationControl::set_viewport(Node3DEditorViewport *p_viewport) {
  223. viewport = p_viewport;
  224. }
  225. void Node3DEditorViewport::_view_settings_confirmed(real_t p_interp_delta) {
  226. // Set FOV override multiplier back to the default, so that the FOV
  227. // setting specified in the View menu is correctly applied.
  228. cursor.fov_scale = 1.0;
  229. _update_camera(p_interp_delta);
  230. }
  231. void Node3DEditorViewport::_update_camera(real_t p_interp_delta) {
  232. bool is_orthogonal = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL;
  233. Cursor old_camera_cursor = camera_cursor;
  234. camera_cursor = cursor;
  235. if (p_interp_delta > 0) {
  236. //-------
  237. // Perform smoothing
  238. if (is_freelook_active()) {
  239. // Higher inertia should increase "lag" (lerp with factor between 0 and 1)
  240. // Inertia of zero should produce instant movement (lerp with factor of 1) in this case it returns a really high value and gets clamped to 1.
  241. const real_t inertia = EDITOR_GET("editors/3d/freelook/freelook_inertia");
  242. real_t factor = (1.0 / inertia) * p_interp_delta;
  243. // We interpolate a different point here, because in freelook mode the focus point (cursor.pos) orbits around eye_pos
  244. camera_cursor.eye_pos = old_camera_cursor.eye_pos.lerp(cursor.eye_pos, CLAMP(factor, 0, 1));
  245. const real_t orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  246. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  247. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  248. if (Math::abs(camera_cursor.x_rot - cursor.x_rot) < 0.1) {
  249. camera_cursor.x_rot = cursor.x_rot;
  250. }
  251. if (Math::abs(camera_cursor.y_rot - cursor.y_rot) < 0.1) {
  252. camera_cursor.y_rot = cursor.y_rot;
  253. }
  254. Vector3 forward = to_camera_transform(camera_cursor).basis.xform(Vector3(0, 0, -1));
  255. camera_cursor.pos = camera_cursor.eye_pos + forward * camera_cursor.distance;
  256. } else {
  257. const real_t orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  258. const real_t translation_inertia = EDITOR_GET("editors/3d/navigation_feel/translation_inertia");
  259. const real_t zoom_inertia = EDITOR_GET("editors/3d/navigation_feel/zoom_inertia");
  260. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  261. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  262. if (Math::abs(camera_cursor.x_rot - cursor.x_rot) < 0.1) {
  263. camera_cursor.x_rot = cursor.x_rot;
  264. }
  265. if (Math::abs(camera_cursor.y_rot - cursor.y_rot) < 0.1) {
  266. camera_cursor.y_rot = cursor.y_rot;
  267. }
  268. camera_cursor.pos = old_camera_cursor.pos.lerp(cursor.pos, MIN(1.f, p_interp_delta * (1 / translation_inertia)));
  269. camera_cursor.distance = Math::lerp(old_camera_cursor.distance, cursor.distance, MIN((real_t)1.0, p_interp_delta * (1 / zoom_inertia)));
  270. }
  271. }
  272. //-------
  273. // Apply camera transform
  274. real_t tolerance = 0.001;
  275. bool equal = true;
  276. if (!Math::is_equal_approx(old_camera_cursor.x_rot, camera_cursor.x_rot, tolerance) || !Math::is_equal_approx(old_camera_cursor.y_rot, camera_cursor.y_rot, tolerance)) {
  277. equal = false;
  278. } else if (!old_camera_cursor.pos.is_equal_approx(camera_cursor.pos)) {
  279. equal = false;
  280. } else if (!Math::is_equal_approx(old_camera_cursor.distance, camera_cursor.distance, tolerance)) {
  281. equal = false;
  282. } else if (!Math::is_equal_approx(old_camera_cursor.fov_scale, camera_cursor.fov_scale, tolerance)) {
  283. equal = false;
  284. }
  285. if (!equal || p_interp_delta == 0 || is_orthogonal != orthogonal) {
  286. camera->set_global_transform(to_camera_transform(camera_cursor));
  287. if (orthogonal) {
  288. float half_fov = Math::deg2rad(get_fov()) / 2.0;
  289. float height = 2.0 * cursor.distance * Math::tan(half_fov);
  290. camera->set_orthogonal(height, get_znear(), get_zfar());
  291. } else {
  292. camera->set_perspective(get_fov(), get_znear(), get_zfar());
  293. }
  294. update_transform_gizmo_view();
  295. rotation_control->update();
  296. spatial_editor->update_grid();
  297. }
  298. }
  299. Transform3D Node3DEditorViewport::to_camera_transform(const Cursor &p_cursor) const {
  300. Transform3D camera_transform;
  301. camera_transform.translate(p_cursor.pos);
  302. camera_transform.basis.rotate(Vector3(1, 0, 0), -p_cursor.x_rot);
  303. camera_transform.basis.rotate(Vector3(0, 1, 0), -p_cursor.y_rot);
  304. if (orthogonal) {
  305. camera_transform.translate(0, 0, (get_zfar() - get_znear()) / 2.0);
  306. } else {
  307. camera_transform.translate(0, 0, p_cursor.distance);
  308. }
  309. return camera_transform;
  310. }
  311. int Node3DEditorViewport::get_selected_count() const {
  312. const HashMap<Node *, Object *> &selection = editor_selection->get_selection();
  313. int count = 0;
  314. for (const KeyValue<Node *, Object *> &E : selection) {
  315. Node3D *sp = Object::cast_to<Node3D>(E.key);
  316. if (!sp) {
  317. continue;
  318. }
  319. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  320. if (!se) {
  321. continue;
  322. }
  323. count++;
  324. }
  325. return count;
  326. }
  327. void Node3DEditorViewport::cancel_transform() {
  328. List<Node *> &selection = editor_selection->get_selected_node_list();
  329. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  330. Node3D *sp = Object::cast_to<Node3D>(E->get());
  331. if (!sp) {
  332. continue;
  333. }
  334. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  335. if (!se) {
  336. continue;
  337. }
  338. sp->set_global_transform(se->original);
  339. }
  340. finish_transform();
  341. set_message(TTR("Transform Aborted."), 3);
  342. }
  343. void Node3DEditorViewport::_update_shrink() {
  344. bool shrink = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION));
  345. subviewport_container->set_stretch_shrink(shrink ? 2 : 1);
  346. subviewport_container->set_texture_filter(shrink ? TEXTURE_FILTER_NEAREST : TEXTURE_FILTER_PARENT_NODE);
  347. }
  348. float Node3DEditorViewport::get_znear() const {
  349. return CLAMP(spatial_editor->get_znear(), MIN_Z, MAX_Z);
  350. }
  351. float Node3DEditorViewport::get_zfar() const {
  352. return CLAMP(spatial_editor->get_zfar(), MIN_Z, MAX_Z);
  353. }
  354. float Node3DEditorViewport::get_fov() const {
  355. return CLAMP(spatial_editor->get_fov() * cursor.fov_scale, MIN_FOV, MAX_FOV);
  356. }
  357. Transform3D Node3DEditorViewport::_get_camera_transform() const {
  358. return camera->get_global_transform();
  359. }
  360. Vector3 Node3DEditorViewport::_get_camera_position() const {
  361. return _get_camera_transform().origin;
  362. }
  363. Point2 Node3DEditorViewport::_point_to_screen(const Vector3 &p_point) {
  364. return camera->unproject_position(p_point) * subviewport_container->get_stretch_shrink();
  365. }
  366. Vector3 Node3DEditorViewport::_get_ray_pos(const Vector2 &p_pos) const {
  367. return camera->project_ray_origin(p_pos / subviewport_container->get_stretch_shrink());
  368. }
  369. Vector3 Node3DEditorViewport::_get_camera_normal() const {
  370. return -_get_camera_transform().basis.get_column(2);
  371. }
  372. Vector3 Node3DEditorViewport::_get_ray(const Vector2 &p_pos) const {
  373. return camera->project_ray_normal(p_pos / subviewport_container->get_stretch_shrink());
  374. }
  375. void Node3DEditorViewport::_clear_selected() {
  376. _edit.gizmo = Ref<EditorNode3DGizmo>();
  377. _edit.gizmo_handle = -1;
  378. _edit.gizmo_handle_secondary = false;
  379. _edit.gizmo_initial_value = Variant();
  380. Node3D *selected = spatial_editor->get_single_selected_node();
  381. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  382. if (se && se->gizmo.is_valid()) {
  383. se->subgizmos.clear();
  384. se->gizmo->redraw();
  385. se->gizmo.unref();
  386. spatial_editor->update_transform_gizmo();
  387. } else {
  388. editor_selection->clear();
  389. Node3DEditor::get_singleton()->edit(nullptr);
  390. }
  391. }
  392. void Node3DEditorViewport::_select_clicked(bool p_allow_locked) {
  393. Node *node = Object::cast_to<Node3D>(ObjectDB::get_instance(clicked));
  394. Node3D *selected = Object::cast_to<Node3D>(node);
  395. clicked = ObjectID();
  396. if (!selected) {
  397. return;
  398. }
  399. if (!p_allow_locked) {
  400. // Replace the node by the group if grouped
  401. while (node && node != EditorNode::get_singleton()->get_edited_scene()->get_parent()) {
  402. Node3D *selected_tmp = Object::cast_to<Node3D>(node);
  403. if (selected_tmp && node->has_meta("_edit_group_")) {
  404. selected = selected_tmp;
  405. }
  406. node = node->get_parent();
  407. }
  408. }
  409. if (p_allow_locked || !_is_node_locked(selected)) {
  410. if (clicked_wants_append) {
  411. if (editor_selection->is_selected(selected)) {
  412. editor_selection->remove_node(selected);
  413. } else {
  414. editor_selection->add_node(selected);
  415. }
  416. } else {
  417. if (!editor_selection->is_selected(selected)) {
  418. editor_selection->clear();
  419. editor_selection->add_node(selected);
  420. EditorNode::get_singleton()->edit_node(selected);
  421. }
  422. }
  423. if (editor_selection->get_selected_node_list().size() == 1) {
  424. EditorNode::get_singleton()->edit_node(editor_selection->get_selected_node_list()[0]);
  425. }
  426. }
  427. }
  428. ObjectID Node3DEditorViewport::_select_ray(const Point2 &p_pos) {
  429. Vector3 ray = _get_ray(p_pos);
  430. Vector3 pos = _get_ray_pos(p_pos);
  431. Vector2 shrinked_pos = p_pos / subviewport_container->get_stretch_shrink();
  432. if (viewport->get_debug_draw() == Viewport::DEBUG_DRAW_SDFGI_PROBES) {
  433. RS::get_singleton()->sdfgi_set_debug_probe_select(pos, ray);
  434. }
  435. Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_ray(pos, pos + ray * camera->get_far(), get_tree()->get_root()->get_world_3d()->get_scenario());
  436. HashSet<Ref<EditorNode3DGizmo>> found_gizmos;
  437. Node *edited_scene = get_tree()->get_edited_scene_root();
  438. ObjectID closest;
  439. Node *item = nullptr;
  440. float closest_dist = 1e20;
  441. for (int i = 0; i < instances.size(); i++) {
  442. Node3D *spat = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i]));
  443. if (!spat) {
  444. continue;
  445. }
  446. Vector<Ref<Node3DGizmo>> gizmos = spat->get_gizmos();
  447. for (int j = 0; j < gizmos.size(); j++) {
  448. Ref<EditorNode3DGizmo> seg = gizmos[j];
  449. if ((!seg.is_valid()) || found_gizmos.has(seg)) {
  450. continue;
  451. }
  452. found_gizmos.insert(seg);
  453. Vector3 point;
  454. Vector3 normal;
  455. bool inters = seg->intersect_ray(camera, shrinked_pos, point, normal);
  456. if (!inters) {
  457. continue;
  458. }
  459. const real_t dist = pos.distance_to(point);
  460. if (dist < 0) {
  461. continue;
  462. }
  463. if (dist < closest_dist) {
  464. item = Object::cast_to<Node>(spat);
  465. if (item != edited_scene) {
  466. item = edited_scene->get_deepest_editable_node(item);
  467. }
  468. closest = item->get_instance_id();
  469. closest_dist = dist;
  470. }
  471. }
  472. }
  473. if (!item) {
  474. return ObjectID();
  475. }
  476. return closest;
  477. }
  478. void Node3DEditorViewport::_find_items_at_pos(const Point2 &p_pos, Vector<_RayResult> &r_results, bool p_include_locked_nodes) {
  479. Vector3 ray = _get_ray(p_pos);
  480. Vector3 pos = _get_ray_pos(p_pos);
  481. Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_ray(pos, pos + ray * camera->get_far(), get_tree()->get_root()->get_world_3d()->get_scenario());
  482. HashSet<Node3D *> found_nodes;
  483. for (int i = 0; i < instances.size(); i++) {
  484. Node3D *spat = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i]));
  485. if (!spat) {
  486. continue;
  487. }
  488. if (found_nodes.has(spat)) {
  489. continue;
  490. }
  491. if (!p_include_locked_nodes && _is_node_locked(spat)) {
  492. continue;
  493. }
  494. Vector<Ref<Node3DGizmo>> gizmos = spat->get_gizmos();
  495. for (int j = 0; j < gizmos.size(); j++) {
  496. Ref<EditorNode3DGizmo> seg = gizmos[j];
  497. if (!seg.is_valid()) {
  498. continue;
  499. }
  500. Vector3 point;
  501. Vector3 normal;
  502. bool inters = seg->intersect_ray(camera, p_pos, point, normal);
  503. if (!inters) {
  504. continue;
  505. }
  506. const real_t dist = pos.distance_to(point);
  507. if (dist < 0) {
  508. continue;
  509. }
  510. found_nodes.insert(spat);
  511. _RayResult res;
  512. res.item = spat;
  513. res.depth = dist;
  514. r_results.push_back(res);
  515. break;
  516. }
  517. }
  518. r_results.sort();
  519. }
  520. Vector3 Node3DEditorViewport::_get_screen_to_space(const Vector3 &p_vector3) {
  521. CameraMatrix cm;
  522. if (orthogonal) {
  523. cm.set_orthogonal(camera->get_size(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  524. } else {
  525. cm.set_perspective(get_fov(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  526. }
  527. Vector2 screen_he = cm.get_viewport_half_extents();
  528. Transform3D camera_transform;
  529. camera_transform.translate(cursor.pos);
  530. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  531. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  532. camera_transform.translate(0, 0, cursor.distance);
  533. return camera_transform.xform(Vector3(((p_vector3.x / get_size().width) * 2.0 - 1.0) * screen_he.x, ((1.0 - (p_vector3.y / get_size().height)) * 2.0 - 1.0) * screen_he.y, -(get_znear() + p_vector3.z)));
  534. }
  535. void Node3DEditorViewport::_select_region() {
  536. if (cursor.region_begin == cursor.region_end) {
  537. if (!clicked_wants_append) {
  538. _clear_selected();
  539. }
  540. return; //nothing really
  541. }
  542. const real_t z_offset = MAX(0.0, 5.0 - get_znear());
  543. Vector3 box[4] = {
  544. Vector3(
  545. MIN(cursor.region_begin.x, cursor.region_end.x),
  546. MIN(cursor.region_begin.y, cursor.region_end.y),
  547. z_offset),
  548. Vector3(
  549. MAX(cursor.region_begin.x, cursor.region_end.x),
  550. MIN(cursor.region_begin.y, cursor.region_end.y),
  551. z_offset),
  552. Vector3(
  553. MAX(cursor.region_begin.x, cursor.region_end.x),
  554. MAX(cursor.region_begin.y, cursor.region_end.y),
  555. z_offset),
  556. Vector3(
  557. MIN(cursor.region_begin.x, cursor.region_end.x),
  558. MAX(cursor.region_begin.y, cursor.region_end.y),
  559. z_offset)
  560. };
  561. Vector<Plane> frustum;
  562. Vector3 cam_pos = _get_camera_position();
  563. for (int i = 0; i < 4; i++) {
  564. Vector3 a = _get_screen_to_space(box[i]);
  565. Vector3 b = _get_screen_to_space(box[(i + 1) % 4]);
  566. if (orthogonal) {
  567. frustum.push_back(Plane((a - b).normalized(), a));
  568. } else {
  569. frustum.push_back(Plane(a, b, cam_pos));
  570. }
  571. }
  572. Plane near(-_get_camera_normal(), cam_pos);
  573. near.d -= get_znear();
  574. frustum.push_back(near);
  575. Plane far = -near;
  576. far.d += get_zfar();
  577. frustum.push_back(far);
  578. if (spatial_editor->get_single_selected_node()) {
  579. Node3D *single_selected = spatial_editor->get_single_selected_node();
  580. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(single_selected);
  581. if (se) {
  582. Ref<EditorNode3DGizmo> old_gizmo;
  583. if (!clicked_wants_append) {
  584. se->subgizmos.clear();
  585. old_gizmo = se->gizmo;
  586. se->gizmo.unref();
  587. }
  588. bool found_subgizmos = false;
  589. Vector<Ref<Node3DGizmo>> gizmos = single_selected->get_gizmos();
  590. for (int j = 0; j < gizmos.size(); j++) {
  591. Ref<EditorNode3DGizmo> seg = gizmos[j];
  592. if (!seg.is_valid()) {
  593. continue;
  594. }
  595. if (se->gizmo.is_valid() && se->gizmo != seg) {
  596. continue;
  597. }
  598. Vector<int> subgizmos = seg->subgizmos_intersect_frustum(camera, frustum);
  599. if (!subgizmos.is_empty()) {
  600. se->gizmo = seg;
  601. for (int i = 0; i < subgizmos.size(); i++) {
  602. int subgizmo_id = subgizmos[i];
  603. if (!se->subgizmos.has(subgizmo_id)) {
  604. se->subgizmos.insert(subgizmo_id, se->gizmo->get_subgizmo_transform(subgizmo_id));
  605. }
  606. }
  607. found_subgizmos = true;
  608. break;
  609. }
  610. }
  611. if (!clicked_wants_append || found_subgizmos) {
  612. if (se->gizmo.is_valid()) {
  613. se->gizmo->redraw();
  614. }
  615. if (old_gizmo != se->gizmo && old_gizmo.is_valid()) {
  616. old_gizmo->redraw();
  617. }
  618. spatial_editor->update_transform_gizmo();
  619. }
  620. if (found_subgizmos) {
  621. return;
  622. }
  623. }
  624. }
  625. if (!clicked_wants_append) {
  626. _clear_selected();
  627. }
  628. Vector<ObjectID> instances = RenderingServer::get_singleton()->instances_cull_convex(frustum, get_tree()->get_root()->get_world_3d()->get_scenario());
  629. HashSet<Node3D *> found_nodes;
  630. Vector<Node *> selected;
  631. Node *edited_scene = get_tree()->get_edited_scene_root();
  632. for (int i = 0; i < instances.size(); i++) {
  633. Node3D *sp = Object::cast_to<Node3D>(ObjectDB::get_instance(instances[i]));
  634. if (!sp || _is_node_locked(sp)) {
  635. continue;
  636. }
  637. if (found_nodes.has(sp)) {
  638. continue;
  639. }
  640. found_nodes.insert(sp);
  641. Node *item = Object::cast_to<Node>(sp);
  642. if (item != edited_scene) {
  643. item = edited_scene->get_deepest_editable_node(item);
  644. }
  645. // Replace the node by the group if grouped
  646. if (item->is_class("Node3D")) {
  647. Node3D *sel = Object::cast_to<Node3D>(item);
  648. while (item && item != EditorNode::get_singleton()->get_edited_scene()->get_parent()) {
  649. Node3D *selected_tmp = Object::cast_to<Node3D>(item);
  650. if (selected_tmp && item->has_meta("_edit_group_")) {
  651. sel = selected_tmp;
  652. }
  653. item = item->get_parent();
  654. }
  655. item = sel;
  656. }
  657. if (_is_node_locked(item)) {
  658. continue;
  659. }
  660. Vector<Ref<Node3DGizmo>> gizmos = sp->get_gizmos();
  661. for (int j = 0; j < gizmos.size(); j++) {
  662. Ref<EditorNode3DGizmo> seg = gizmos[j];
  663. if (!seg.is_valid()) {
  664. continue;
  665. }
  666. if (seg->intersect_frustum(camera, frustum)) {
  667. selected.push_back(item);
  668. }
  669. }
  670. }
  671. for (int i = 0; i < selected.size(); i++) {
  672. if (!editor_selection->is_selected(selected[i])) {
  673. editor_selection->add_node(selected[i]);
  674. }
  675. }
  676. if (editor_selection->get_selected_node_list().size() == 1) {
  677. EditorNode::get_singleton()->edit_node(editor_selection->get_selected_node_list()[0]);
  678. }
  679. }
  680. void Node3DEditorViewport::_update_name() {
  681. String name;
  682. switch (view_type) {
  683. case VIEW_TYPE_USER: {
  684. if (orthogonal) {
  685. name = TTR("Orthogonal");
  686. } else {
  687. name = TTR("Perspective");
  688. }
  689. } break;
  690. case VIEW_TYPE_TOP: {
  691. if (orthogonal) {
  692. name = TTR("Top Orthogonal");
  693. } else {
  694. name = TTR("Top Perspective");
  695. }
  696. } break;
  697. case VIEW_TYPE_BOTTOM: {
  698. if (orthogonal) {
  699. name = TTR("Bottom Orthogonal");
  700. } else {
  701. name = TTR("Bottom Perspective");
  702. }
  703. } break;
  704. case VIEW_TYPE_LEFT: {
  705. if (orthogonal) {
  706. name = TTR("Left Orthogonal");
  707. } else {
  708. name = TTR("Left Perspective");
  709. }
  710. } break;
  711. case VIEW_TYPE_RIGHT: {
  712. if (orthogonal) {
  713. name = TTR("Right Orthogonal");
  714. } else {
  715. name = TTR("Right Perspective");
  716. }
  717. } break;
  718. case VIEW_TYPE_FRONT: {
  719. if (orthogonal) {
  720. name = TTR("Front Orthogonal");
  721. } else {
  722. name = TTR("Front Perspective");
  723. }
  724. } break;
  725. case VIEW_TYPE_REAR: {
  726. if (orthogonal) {
  727. name = TTR("Rear Orthogonal");
  728. } else {
  729. name = TTR("Rear Perspective");
  730. }
  731. } break;
  732. }
  733. if (auto_orthogonal) {
  734. // TRANSLATORS: This will be appended to the view name when Auto Orthogonal is enabled.
  735. name += TTR(" [auto]");
  736. }
  737. view_menu->set_text(name);
  738. view_menu->reset_size();
  739. }
  740. void Node3DEditorViewport::_compute_edit(const Point2 &p_point) {
  741. _edit.original_local = spatial_editor->are_local_coords_enabled();
  742. _edit.click_ray = _get_ray(p_point);
  743. _edit.click_ray_pos = _get_ray_pos(p_point);
  744. _edit.plane = TRANSFORM_VIEW;
  745. spatial_editor->update_transform_gizmo();
  746. _edit.center = spatial_editor->get_gizmo_transform().origin;
  747. Node3D *selected = spatial_editor->get_single_selected_node();
  748. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  749. if (se && se->gizmo.is_valid()) {
  750. for (const KeyValue<int, Transform3D> &E : se->subgizmos) {
  751. int subgizmo_id = E.key;
  752. se->subgizmos[subgizmo_id] = se->gizmo->get_subgizmo_transform(subgizmo_id);
  753. }
  754. se->original_local = selected->get_transform();
  755. se->original = selected->get_global_transform();
  756. } else {
  757. List<Node *> &selection = editor_selection->get_selected_node_list();
  758. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  759. Node3D *sp = Object::cast_to<Node3D>(E->get());
  760. if (!sp) {
  761. continue;
  762. }
  763. Node3DEditorSelectedItem *sel_item = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  764. if (!sel_item) {
  765. continue;
  766. }
  767. sel_item->original_local = sel_item->sp->get_local_gizmo_transform();
  768. sel_item->original = sel_item->sp->get_global_gizmo_transform();
  769. }
  770. }
  771. }
  772. static Key _get_key_modifier_setting(const String &p_property) {
  773. switch (EditorSettings::get_singleton()->get(p_property).operator int()) {
  774. case 0:
  775. return Key::NONE;
  776. case 1:
  777. return Key::SHIFT;
  778. case 2:
  779. return Key::ALT;
  780. case 3:
  781. return Key::META;
  782. case 4:
  783. return Key::CTRL;
  784. }
  785. return Key::NONE;
  786. }
  787. static Key _get_key_modifier(Ref<InputEventWithModifiers> e) {
  788. if (e->is_shift_pressed()) {
  789. return Key::SHIFT;
  790. }
  791. if (e->is_alt_pressed()) {
  792. return Key::ALT;
  793. }
  794. if (e->is_ctrl_pressed()) {
  795. return Key::CTRL;
  796. }
  797. if (e->is_meta_pressed()) {
  798. return Key::META;
  799. }
  800. return Key::NONE;
  801. }
  802. bool Node3DEditorViewport::_transform_gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only) {
  803. if (!spatial_editor->is_gizmo_visible()) {
  804. return false;
  805. }
  806. if (get_selected_count() == 0) {
  807. if (p_highlight_only) {
  808. spatial_editor->select_gizmo_highlight_axis(-1);
  809. }
  810. return false;
  811. }
  812. Vector3 ray_pos = _get_ray_pos(p_screenpos);
  813. Vector3 ray = _get_ray(p_screenpos);
  814. Transform3D gt = spatial_editor->get_gizmo_transform();
  815. if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE) {
  816. int col_axis = -1;
  817. real_t col_d = 1e20;
  818. for (int i = 0; i < 3; i++) {
  819. const Vector3 grabber_pos = gt.origin + gt.basis.get_column(i).normalized() * gizmo_scale * (GIZMO_ARROW_OFFSET + (GIZMO_ARROW_SIZE * 0.5));
  820. const real_t grabber_radius = gizmo_scale * GIZMO_ARROW_SIZE;
  821. Vector3 r;
  822. if (Geometry3D::segment_intersects_sphere(ray_pos, ray_pos + ray * MAX_Z, grabber_pos, grabber_radius, &r)) {
  823. const real_t d = r.distance_to(ray_pos);
  824. if (d < col_d) {
  825. col_d = d;
  826. col_axis = i;
  827. }
  828. }
  829. }
  830. bool is_plane_translate = false;
  831. // plane select
  832. if (col_axis == -1) {
  833. col_d = 1e20;
  834. for (int i = 0; i < 3; i++) {
  835. Vector3 ivec2 = gt.basis.get_column((i + 1) % 3).normalized();
  836. Vector3 ivec3 = gt.basis.get_column((i + 2) % 3).normalized();
  837. // Allow some tolerance to make the plane easier to click,
  838. // even if the click is actually slightly outside the plane.
  839. const Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gizmo_scale * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST * 0.6667);
  840. Vector3 r;
  841. Plane plane(gt.basis.get_column(i).normalized(), gt.origin);
  842. if (plane.intersects_ray(ray_pos, ray, &r)) {
  843. const real_t dist = r.distance_to(grabber_pos);
  844. // Allow some tolerance to make the plane easier to click,
  845. // even if the click is actually slightly outside the plane.
  846. if (dist < (gizmo_scale * GIZMO_PLANE_SIZE * 1.5)) {
  847. const real_t d = ray_pos.distance_to(r);
  848. if (d < col_d) {
  849. col_d = d;
  850. col_axis = i;
  851. is_plane_translate = true;
  852. }
  853. }
  854. }
  855. }
  856. }
  857. if (col_axis != -1) {
  858. if (p_highlight_only) {
  859. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_translate ? 6 : 0));
  860. } else {
  861. //handle plane translate
  862. _edit.mode = TRANSFORM_TRANSLATE;
  863. _compute_edit(p_screenpos);
  864. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_translate ? 3 : 0));
  865. }
  866. return true;
  867. }
  868. }
  869. if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE) {
  870. int col_axis = -1;
  871. Vector3 hit_position;
  872. Vector3 hit_normal;
  873. real_t ray_length = gt.origin.distance_to(ray_pos) + (GIZMO_CIRCLE_SIZE * gizmo_scale) * 4.0f;
  874. if (Geometry3D::segment_intersects_sphere(ray_pos, ray_pos + ray * ray_length, gt.origin, gizmo_scale * (GIZMO_CIRCLE_SIZE), &hit_position, &hit_normal)) {
  875. if (hit_normal.dot(_get_camera_normal()) < 0.05) {
  876. hit_position = gt.xform_inv(hit_position).abs();
  877. int min_axis = hit_position.min_axis_index();
  878. if (hit_position[min_axis] < gizmo_scale * GIZMO_RING_HALF_WIDTH) {
  879. col_axis = min_axis;
  880. }
  881. }
  882. }
  883. if (col_axis == -1) {
  884. float col_d = 1e20;
  885. for (int i = 0; i < 3; i++) {
  886. Plane plane(gt.basis.get_column(i).normalized(), gt.origin);
  887. Vector3 r;
  888. if (!plane.intersects_ray(ray_pos, ray, &r)) {
  889. continue;
  890. }
  891. const real_t dist = r.distance_to(gt.origin);
  892. const Vector3 r_dir = (r - gt.origin).normalized();
  893. if (_get_camera_normal().dot(r_dir) <= 0.005) {
  894. if (dist > gizmo_scale * (GIZMO_CIRCLE_SIZE - GIZMO_RING_HALF_WIDTH) && dist < gizmo_scale * (GIZMO_CIRCLE_SIZE + GIZMO_RING_HALF_WIDTH)) {
  895. const real_t d = ray_pos.distance_to(r);
  896. if (d < col_d) {
  897. col_d = d;
  898. col_axis = i;
  899. }
  900. }
  901. }
  902. }
  903. }
  904. if (col_axis != -1) {
  905. if (p_highlight_only) {
  906. spatial_editor->select_gizmo_highlight_axis(col_axis + 3);
  907. } else {
  908. //handle rotate
  909. _edit.mode = TRANSFORM_ROTATE;
  910. _compute_edit(p_screenpos);
  911. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis);
  912. }
  913. return true;
  914. }
  915. }
  916. if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE) {
  917. int col_axis = -1;
  918. float col_d = 1e20;
  919. for (int i = 0; i < 3; i++) {
  920. const Vector3 grabber_pos = gt.origin + gt.basis.get_column(i).normalized() * gizmo_scale * GIZMO_SCALE_OFFSET;
  921. const real_t grabber_radius = gizmo_scale * GIZMO_ARROW_SIZE;
  922. Vector3 r;
  923. if (Geometry3D::segment_intersects_sphere(ray_pos, ray_pos + ray * MAX_Z, grabber_pos, grabber_radius, &r)) {
  924. const real_t d = r.distance_to(ray_pos);
  925. if (d < col_d) {
  926. col_d = d;
  927. col_axis = i;
  928. }
  929. }
  930. }
  931. bool is_plane_scale = false;
  932. // plane select
  933. if (col_axis == -1) {
  934. col_d = 1e20;
  935. for (int i = 0; i < 3; i++) {
  936. const Vector3 ivec2 = gt.basis.get_column((i + 1) % 3).normalized();
  937. const Vector3 ivec3 = gt.basis.get_column((i + 2) % 3).normalized();
  938. // Allow some tolerance to make the plane easier to click,
  939. // even if the click is actually slightly outside the plane.
  940. const Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gizmo_scale * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST * 0.6667);
  941. Vector3 r;
  942. Plane plane(gt.basis.get_column(i).normalized(), gt.origin);
  943. if (plane.intersects_ray(ray_pos, ray, &r)) {
  944. const real_t dist = r.distance_to(grabber_pos);
  945. // Allow some tolerance to make the plane easier to click,
  946. // even if the click is actually slightly outside the plane.
  947. if (dist < (gizmo_scale * GIZMO_PLANE_SIZE * 1.5)) {
  948. const real_t d = ray_pos.distance_to(r);
  949. if (d < col_d) {
  950. col_d = d;
  951. col_axis = i;
  952. is_plane_scale = true;
  953. }
  954. }
  955. }
  956. }
  957. }
  958. if (col_axis != -1) {
  959. if (p_highlight_only) {
  960. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_scale ? 12 : 9));
  961. } else {
  962. //handle scale
  963. _edit.mode = TRANSFORM_SCALE;
  964. _compute_edit(p_screenpos);
  965. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_scale ? 3 : 0));
  966. }
  967. return true;
  968. }
  969. }
  970. if (p_highlight_only) {
  971. spatial_editor->select_gizmo_highlight_axis(-1);
  972. }
  973. return false;
  974. }
  975. void Node3DEditorViewport::_transform_gizmo_apply(Node3D *p_node, const Transform3D &p_transform, bool p_local) {
  976. if (p_transform.basis.determinant() == 0) {
  977. return;
  978. }
  979. if (p_local) {
  980. p_node->set_transform(p_transform);
  981. } else {
  982. p_node->set_global_transform(p_transform);
  983. }
  984. }
  985. Transform3D Node3DEditorViewport::_compute_transform(TransformMode p_mode, const Transform3D &p_original, const Transform3D &p_original_local, Vector3 p_motion, double p_extra, bool p_local, bool p_orthogonal) {
  986. switch (p_mode) {
  987. case TRANSFORM_SCALE: {
  988. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  989. p_motion.snap(Vector3(p_extra, p_extra, p_extra));
  990. }
  991. Transform3D s;
  992. if (p_local) {
  993. s.basis = p_original_local.basis.scaled_local(p_motion + Vector3(1, 1, 1));
  994. s.origin = p_original_local.origin;
  995. } else {
  996. s.basis.scale(p_motion + Vector3(1, 1, 1));
  997. Transform3D base = Transform3D(Basis(), _edit.center);
  998. s = base * (s * (base.inverse() * p_original));
  999. // Recalculate orthogonalized scale without moving origin.
  1000. if (p_orthogonal) {
  1001. s.basis = p_original_local.basis.scaled_orthogonal(p_motion + Vector3(1, 1, 1));
  1002. // The scaled_orthogonal() does not require orthogonal Basis,
  1003. // but it may make a bit skew by precision problems.
  1004. s.basis.orthogonalize();
  1005. }
  1006. }
  1007. return s;
  1008. }
  1009. case TRANSFORM_TRANSLATE: {
  1010. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1011. p_motion.snap(Vector3(p_extra, p_extra, p_extra));
  1012. }
  1013. if (p_local) {
  1014. p_motion = p_original.basis.xform(p_motion);
  1015. }
  1016. // Apply translation
  1017. Transform3D t = p_original;
  1018. t.origin += p_motion;
  1019. return t;
  1020. }
  1021. case TRANSFORM_ROTATE: {
  1022. Transform3D r;
  1023. if (p_local) {
  1024. Vector3 axis = p_original_local.basis.xform(p_motion);
  1025. r.basis = Basis(axis.normalized(), p_extra) * p_original_local.basis;
  1026. r.origin = p_original_local.origin;
  1027. } else {
  1028. Basis local = p_original.basis * p_original_local.basis.inverse();
  1029. Vector3 axis = local.xform_inv(p_motion);
  1030. r.basis = local * Basis(axis.normalized(), p_extra) * p_original_local.basis;
  1031. r.origin = Basis(p_motion, p_extra).xform(p_original.origin - _edit.center) + _edit.center;
  1032. }
  1033. return r;
  1034. }
  1035. default: {
  1036. ERR_FAIL_V_MSG(Transform3D(), "Invalid mode in '_compute_transform'");
  1037. }
  1038. }
  1039. }
  1040. void Node3DEditorViewport::_surface_mouse_enter() {
  1041. if (!surface->has_focus() && (!get_viewport()->gui_get_focus_owner() || !get_viewport()->gui_get_focus_owner()->is_text_field())) {
  1042. surface->grab_focus();
  1043. }
  1044. }
  1045. void Node3DEditorViewport::_surface_mouse_exit() {
  1046. _remove_preview();
  1047. }
  1048. void Node3DEditorViewport::_surface_focus_enter() {
  1049. view_menu->set_disable_shortcuts(false);
  1050. }
  1051. void Node3DEditorViewport::_surface_focus_exit() {
  1052. view_menu->set_disable_shortcuts(true);
  1053. }
  1054. bool Node3DEditorViewport ::_is_node_locked(const Node *p_node) {
  1055. return p_node->get_meta("_edit_lock_", false);
  1056. }
  1057. void Node3DEditorViewport::_list_select(Ref<InputEventMouseButton> b) {
  1058. _find_items_at_pos(b->get_position(), selection_results, spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT);
  1059. Node *scene = EditorNode::get_singleton()->get_edited_scene();
  1060. for (int i = 0; i < selection_results.size(); i++) {
  1061. Node3D *item = selection_results[i].item;
  1062. if (item != scene && item->get_owner() != scene && item != scene->get_deepest_editable_node(item)) {
  1063. //invalid result
  1064. selection_results.remove_at(i);
  1065. i--;
  1066. }
  1067. }
  1068. clicked_wants_append = b->is_shift_pressed();
  1069. if (selection_results.size() == 1) {
  1070. clicked = selection_results[0].item->get_instance_id();
  1071. selection_results.clear();
  1072. if (clicked.is_valid()) {
  1073. _select_clicked(spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT);
  1074. }
  1075. } else if (!selection_results.is_empty()) {
  1076. NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
  1077. StringName root_name = root_path.get_name(root_path.get_name_count() - 1);
  1078. for (int i = 0; i < selection_results.size(); i++) {
  1079. Node3D *spat = selection_results[i].item;
  1080. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(spat, "Node");
  1081. String node_path = "/" + root_name + "/" + root_path.rel_path_to(spat->get_path());
  1082. int locked = 0;
  1083. if (_is_node_locked(spat)) {
  1084. locked = 1;
  1085. } else {
  1086. Node *ed_scene = EditorNode::get_singleton()->get_edited_scene();
  1087. Node *node = spat;
  1088. while (node && node != ed_scene->get_parent()) {
  1089. Node3D *selected_tmp = Object::cast_to<Node3D>(node);
  1090. if (selected_tmp && node->has_meta("_edit_group_")) {
  1091. locked = 2;
  1092. }
  1093. node = node->get_parent();
  1094. }
  1095. }
  1096. String suffix = String();
  1097. if (locked == 1) {
  1098. suffix = " (" + TTR("Locked") + ")";
  1099. } else if (locked == 2) {
  1100. suffix = " (" + TTR("Grouped") + ")";
  1101. }
  1102. selection_menu->add_item((String)spat->get_name() + suffix);
  1103. selection_menu->set_item_icon(i, icon);
  1104. selection_menu->set_item_metadata(i, node_path);
  1105. selection_menu->set_item_tooltip(i, String(spat->get_name()) + "\nType: " + spat->get_class() + "\nPath: " + node_path);
  1106. }
  1107. selection_menu->set_position(get_screen_position() + b->get_position());
  1108. selection_menu->reset_size();
  1109. selection_menu->popup();
  1110. }
  1111. }
  1112. void Node3DEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
  1113. if (previewing) {
  1114. return; //do NONE
  1115. }
  1116. EditorPlugin::AfterGUIInput after = EditorPlugin::AFTER_GUI_INPUT_PASS;
  1117. {
  1118. EditorNode *en = EditorNode::get_singleton();
  1119. EditorPluginList *force_input_forwarding_list = en->get_editor_plugins_force_input_forwarding();
  1120. if (!force_input_forwarding_list->is_empty()) {
  1121. EditorPlugin::AfterGUIInput discard = force_input_forwarding_list->forward_spatial_gui_input(camera, p_event, true);
  1122. if (discard == EditorPlugin::AFTER_GUI_INPUT_STOP) {
  1123. return;
  1124. }
  1125. if (discard == EditorPlugin::AFTER_GUI_INPUT_DESELECT) {
  1126. after = EditorPlugin::AFTER_GUI_INPUT_DESELECT;
  1127. }
  1128. }
  1129. }
  1130. {
  1131. EditorNode *en = EditorNode::get_singleton();
  1132. EditorPluginList *over_plugin_list = en->get_editor_plugins_over();
  1133. if (!over_plugin_list->is_empty()) {
  1134. EditorPlugin::AfterGUIInput discard = over_plugin_list->forward_spatial_gui_input(camera, p_event, false);
  1135. if (discard == EditorPlugin::AFTER_GUI_INPUT_STOP) {
  1136. return;
  1137. }
  1138. if (discard == EditorPlugin::AFTER_GUI_INPUT_DESELECT) {
  1139. after = EditorPlugin::AFTER_GUI_INPUT_DESELECT;
  1140. }
  1141. }
  1142. }
  1143. Ref<InputEventMouseButton> b = p_event;
  1144. if (b.is_valid()) {
  1145. emit_signal(SNAME("clicked"), this);
  1146. const real_t zoom_factor = 1 + (ZOOM_FREELOOK_MULTIPLIER - 1) * b->get_factor();
  1147. switch (b->get_button_index()) {
  1148. case MouseButton::WHEEL_UP: {
  1149. if (b->is_alt_pressed()) {
  1150. scale_fov(-0.05);
  1151. } else {
  1152. if (is_freelook_active()) {
  1153. scale_freelook_speed(zoom_factor);
  1154. } else {
  1155. scale_cursor_distance(1.0 / zoom_factor);
  1156. }
  1157. }
  1158. } break;
  1159. case MouseButton::WHEEL_DOWN: {
  1160. if (b->is_alt_pressed()) {
  1161. scale_fov(0.05);
  1162. } else {
  1163. if (is_freelook_active()) {
  1164. scale_freelook_speed(1.0 / zoom_factor);
  1165. } else {
  1166. scale_cursor_distance(zoom_factor);
  1167. }
  1168. }
  1169. } break;
  1170. case MouseButton::RIGHT: {
  1171. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1172. if (b->is_pressed() && _edit.gizmo.is_valid()) {
  1173. //restore
  1174. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, _edit.gizmo_initial_value, true);
  1175. _edit.gizmo = Ref<EditorNode3DGizmo>();
  1176. }
  1177. if (_edit.mode == TRANSFORM_NONE && b->is_pressed()) {
  1178. if (b->is_alt_pressed()) {
  1179. if (nav_scheme == NAVIGATION_MAYA) {
  1180. break;
  1181. }
  1182. _list_select(b);
  1183. return;
  1184. }
  1185. }
  1186. if (_edit.mode != TRANSFORM_NONE && b->is_pressed()) {
  1187. cancel_transform();
  1188. }
  1189. if (b->is_pressed()) {
  1190. const Key mod = _get_key_modifier(b);
  1191. if (!orthogonal) {
  1192. if (mod == _get_key_modifier_setting("editors/3d/freelook/freelook_activation_modifier")) {
  1193. set_freelook_active(true);
  1194. }
  1195. }
  1196. } else {
  1197. set_freelook_active(false);
  1198. }
  1199. if (freelook_active && !surface->has_focus()) {
  1200. // Focus usually doesn't trigger on right-click, but in case of freelook it should,
  1201. // otherwise using keyboard navigation would misbehave
  1202. surface->grab_focus();
  1203. }
  1204. } break;
  1205. case MouseButton::MIDDLE: {
  1206. if (b->is_pressed() && _edit.mode != TRANSFORM_NONE) {
  1207. switch (_edit.plane) {
  1208. case TRANSFORM_VIEW: {
  1209. _edit.plane = TRANSFORM_X_AXIS;
  1210. set_message(TTR("X-Axis Transform."), 2);
  1211. view_type = VIEW_TYPE_USER;
  1212. _update_name();
  1213. } break;
  1214. case TRANSFORM_X_AXIS: {
  1215. _edit.plane = TRANSFORM_Y_AXIS;
  1216. set_message(TTR("Y-Axis Transform."), 2);
  1217. } break;
  1218. case TRANSFORM_Y_AXIS: {
  1219. _edit.plane = TRANSFORM_Z_AXIS;
  1220. set_message(TTR("Z-Axis Transform."), 2);
  1221. } break;
  1222. case TRANSFORM_Z_AXIS: {
  1223. _edit.plane = TRANSFORM_VIEW;
  1224. set_message(TTR("View Plane Transform."), 2);
  1225. } break;
  1226. case TRANSFORM_YZ:
  1227. case TRANSFORM_XZ:
  1228. case TRANSFORM_XY: {
  1229. } break;
  1230. }
  1231. }
  1232. } break;
  1233. case MouseButton::LEFT: {
  1234. if (b->is_pressed()) {
  1235. clicked_wants_append = b->is_shift_pressed();
  1236. if (_edit.mode != TRANSFORM_NONE && _edit.instant) {
  1237. commit_transform();
  1238. break; // just commit the edit, stop processing the event so we don't deselect the object
  1239. }
  1240. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1241. if ((nav_scheme == NAVIGATION_MAYA || nav_scheme == NAVIGATION_MODO) && b->is_alt_pressed()) {
  1242. break;
  1243. }
  1244. if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_LIST_SELECT) {
  1245. _list_select(b);
  1246. break;
  1247. }
  1248. _edit.mouse_pos = b->get_position();
  1249. _edit.original_mouse_pos = b->get_position();
  1250. _edit.snap = spatial_editor->is_snap_enabled();
  1251. _edit.mode = TRANSFORM_NONE;
  1252. _edit.original = spatial_editor->get_gizmo_transform(); // To prevent to break when flipping with scale.
  1253. bool can_select_gizmos = spatial_editor->get_single_selected_node();
  1254. {
  1255. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  1256. can_select_gizmos = can_select_gizmos && view_menu->get_popup()->is_item_checked(idx);
  1257. }
  1258. // Gizmo handles
  1259. if (can_select_gizmos) {
  1260. Vector<Ref<Node3DGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos();
  1261. bool intersected_handle = false;
  1262. for (int i = 0; i < gizmos.size(); i++) {
  1263. Ref<EditorNode3DGizmo> seg = gizmos[i];
  1264. if ((!seg.is_valid())) {
  1265. continue;
  1266. }
  1267. int gizmo_handle = -1;
  1268. bool gizmo_secondary = false;
  1269. seg->handles_intersect_ray(camera, _edit.mouse_pos, b->is_shift_pressed(), gizmo_handle, gizmo_secondary);
  1270. if (gizmo_handle != -1) {
  1271. _edit.gizmo = seg;
  1272. _edit.gizmo_handle = gizmo_handle;
  1273. _edit.gizmo_handle_secondary = gizmo_secondary;
  1274. _edit.gizmo_initial_value = seg->get_handle_value(gizmo_handle, gizmo_secondary);
  1275. intersected_handle = true;
  1276. break;
  1277. }
  1278. }
  1279. if (intersected_handle) {
  1280. break;
  1281. }
  1282. }
  1283. // Transform gizmo
  1284. if (_transform_gizmo_select(_edit.mouse_pos)) {
  1285. break;
  1286. }
  1287. // Subgizmos
  1288. if (can_select_gizmos) {
  1289. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(spatial_editor->get_single_selected_node());
  1290. Vector<Ref<Node3DGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos();
  1291. bool intersected_subgizmo = false;
  1292. for (int i = 0; i < gizmos.size(); i++) {
  1293. Ref<EditorNode3DGizmo> seg = gizmos[i];
  1294. if ((!seg.is_valid())) {
  1295. continue;
  1296. }
  1297. int subgizmo_id = seg->subgizmos_intersect_ray(camera, _edit.mouse_pos);
  1298. if (subgizmo_id != -1) {
  1299. ERR_CONTINUE(!se);
  1300. if (b->is_shift_pressed()) {
  1301. if (se->subgizmos.has(subgizmo_id)) {
  1302. se->subgizmos.erase(subgizmo_id);
  1303. } else {
  1304. se->subgizmos.insert(subgizmo_id, seg->get_subgizmo_transform(subgizmo_id));
  1305. }
  1306. } else {
  1307. se->subgizmos.clear();
  1308. se->subgizmos.insert(subgizmo_id, seg->get_subgizmo_transform(subgizmo_id));
  1309. }
  1310. if (se->subgizmos.is_empty()) {
  1311. se->gizmo = Ref<EditorNode3DGizmo>();
  1312. } else {
  1313. se->gizmo = seg;
  1314. }
  1315. seg->redraw();
  1316. spatial_editor->update_transform_gizmo();
  1317. intersected_subgizmo = true;
  1318. break;
  1319. }
  1320. }
  1321. if (intersected_subgizmo) {
  1322. break;
  1323. }
  1324. }
  1325. clicked = ObjectID();
  1326. if ((spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT && b->is_command_pressed()) || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE) {
  1327. begin_transform(TRANSFORM_ROTATE, false);
  1328. break;
  1329. }
  1330. if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE) {
  1331. begin_transform(TRANSFORM_TRANSLATE, false);
  1332. break;
  1333. }
  1334. if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE) {
  1335. begin_transform(TRANSFORM_SCALE, false);
  1336. break;
  1337. }
  1338. if (after != EditorPlugin::AFTER_GUI_INPUT_DESELECT) {
  1339. //clicking is always deferred to either move or release
  1340. clicked = _select_ray(b->get_position());
  1341. selection_in_progress = true;
  1342. if (clicked.is_null()) {
  1343. //default to regionselect
  1344. cursor.region_select = true;
  1345. cursor.region_begin = b->get_position();
  1346. cursor.region_end = b->get_position();
  1347. }
  1348. }
  1349. surface->update();
  1350. } else {
  1351. if (_edit.gizmo.is_valid()) {
  1352. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, _edit.gizmo_initial_value, false);
  1353. _edit.gizmo = Ref<EditorNode3DGizmo>();
  1354. break;
  1355. }
  1356. if (after != EditorPlugin::AFTER_GUI_INPUT_DESELECT) {
  1357. selection_in_progress = false;
  1358. if (clicked.is_valid()) {
  1359. _select_clicked(false);
  1360. }
  1361. if (cursor.region_select) {
  1362. _select_region();
  1363. cursor.region_select = false;
  1364. surface->update();
  1365. }
  1366. }
  1367. if (_edit.mode != TRANSFORM_NONE) {
  1368. Node3D *selected = spatial_editor->get_single_selected_node();
  1369. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  1370. if (se && se->gizmo.is_valid()) {
  1371. Vector<int> ids;
  1372. Vector<Transform3D> restore;
  1373. for (const KeyValue<int, Transform3D> &GE : se->subgizmos) {
  1374. ids.push_back(GE.key);
  1375. restore.push_back(GE.value);
  1376. }
  1377. se->gizmo->commit_subgizmos(ids, restore, false);
  1378. spatial_editor->update_transform_gizmo();
  1379. } else {
  1380. commit_transform();
  1381. }
  1382. _edit.mode = TRANSFORM_NONE;
  1383. set_message("");
  1384. }
  1385. surface->update();
  1386. }
  1387. } break;
  1388. default:
  1389. break;
  1390. }
  1391. }
  1392. Ref<InputEventMouseMotion> m = p_event;
  1393. if (m.is_valid()) {
  1394. _edit.mouse_pos = m->get_position();
  1395. if (spatial_editor->get_single_selected_node()) {
  1396. Vector<Ref<Node3DGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos();
  1397. Ref<EditorNode3DGizmo> found_gizmo;
  1398. int found_handle = -1;
  1399. bool found_handle_secondary = false;
  1400. for (int i = 0; i < gizmos.size(); i++) {
  1401. Ref<EditorNode3DGizmo> seg = gizmos[i];
  1402. if (!seg.is_valid()) {
  1403. continue;
  1404. }
  1405. seg->handles_intersect_ray(camera, _edit.mouse_pos, false, found_handle, found_handle_secondary);
  1406. if (found_handle != -1) {
  1407. found_gizmo = seg;
  1408. break;
  1409. }
  1410. }
  1411. if (found_gizmo.is_valid()) {
  1412. spatial_editor->select_gizmo_highlight_axis(-1);
  1413. }
  1414. bool current_hover_handle_secondary = false;
  1415. int curreny_hover_handle = spatial_editor->get_current_hover_gizmo_handle(current_hover_handle_secondary);
  1416. if (found_gizmo != spatial_editor->get_current_hover_gizmo() || found_handle != curreny_hover_handle || found_handle_secondary != current_hover_handle_secondary) {
  1417. spatial_editor->set_current_hover_gizmo(found_gizmo);
  1418. spatial_editor->set_current_hover_gizmo_handle(found_handle, found_handle_secondary);
  1419. spatial_editor->get_single_selected_node()->update_gizmos();
  1420. }
  1421. }
  1422. if (spatial_editor->get_current_hover_gizmo().is_null() && (m->get_button_mask() & MouseButton::MASK_LEFT) == MouseButton::NONE && !_edit.gizmo.is_valid()) {
  1423. _transform_gizmo_select(_edit.mouse_pos, true);
  1424. }
  1425. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1426. NavigationMode nav_mode = NAVIGATION_NONE;
  1427. if (_edit.gizmo.is_valid()) {
  1428. _edit.gizmo->set_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, camera, m->get_position());
  1429. Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle, _edit.gizmo_handle_secondary);
  1430. String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle, _edit.gizmo_handle_secondary);
  1431. set_message(n + ": " + String(v));
  1432. } else if ((m->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE || _edit.instant) {
  1433. if (nav_scheme == NAVIGATION_MAYA && m->is_alt_pressed()) {
  1434. nav_mode = NAVIGATION_ORBIT;
  1435. } else if (nav_scheme == NAVIGATION_MODO && m->is_alt_pressed() && m->is_shift_pressed()) {
  1436. nav_mode = NAVIGATION_PAN;
  1437. } else if (nav_scheme == NAVIGATION_MODO && m->is_alt_pressed() && m->is_ctrl_pressed()) {
  1438. nav_mode = NAVIGATION_ZOOM;
  1439. } else if (nav_scheme == NAVIGATION_MODO && m->is_alt_pressed()) {
  1440. nav_mode = NAVIGATION_ORBIT;
  1441. } else {
  1442. const bool movement_threshold_passed = _edit.original_mouse_pos.distance_to(_edit.mouse_pos) > 8 * EDSCALE;
  1443. // enable region-select if nothing has been selected yet or multi-select (shift key) is active
  1444. if (selection_in_progress && movement_threshold_passed) {
  1445. if (get_selected_count() == 0 || clicked_wants_append) {
  1446. cursor.region_select = true;
  1447. cursor.region_begin = _edit.original_mouse_pos;
  1448. clicked = ObjectID();
  1449. }
  1450. }
  1451. if (cursor.region_select) {
  1452. cursor.region_end = m->get_position();
  1453. surface->update();
  1454. return;
  1455. }
  1456. if (clicked.is_valid() && movement_threshold_passed) {
  1457. _compute_edit(_edit.original_mouse_pos);
  1458. clicked = ObjectID();
  1459. _edit.mode = TRANSFORM_TRANSLATE;
  1460. }
  1461. if (_edit.mode == TRANSFORM_NONE) {
  1462. return;
  1463. }
  1464. update_transform(m->get_position(), _get_key_modifier(m) == Key::SHIFT);
  1465. }
  1466. } else if ((m->get_button_mask() & MouseButton::MASK_RIGHT) != MouseButton::NONE || freelook_active) {
  1467. if (nav_scheme == NAVIGATION_MAYA && m->is_alt_pressed()) {
  1468. nav_mode = NAVIGATION_ZOOM;
  1469. } else if (freelook_active) {
  1470. nav_mode = NAVIGATION_LOOK;
  1471. } else if (orthogonal) {
  1472. nav_mode = NAVIGATION_PAN;
  1473. }
  1474. } else if ((m->get_button_mask() & MouseButton::MASK_MIDDLE) != MouseButton::NONE) {
  1475. const Key mod = _get_key_modifier(m);
  1476. if (nav_scheme == NAVIGATION_GODOT) {
  1477. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier")) {
  1478. nav_mode = NAVIGATION_PAN;
  1479. } else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier")) {
  1480. nav_mode = NAVIGATION_ZOOM;
  1481. } else if (mod == Key::ALT || mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier")) {
  1482. // Always allow Alt as a modifier to better support graphic tablets.
  1483. nav_mode = NAVIGATION_ORBIT;
  1484. }
  1485. } else if (nav_scheme == NAVIGATION_MAYA) {
  1486. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier")) {
  1487. nav_mode = NAVIGATION_PAN;
  1488. }
  1489. }
  1490. } else if (EditorSettings::get_singleton()->get("editors/3d/navigation/emulate_3_button_mouse")) {
  1491. // Handle trackpad (no external mouse) use case
  1492. const Key mod = _get_key_modifier(m);
  1493. if (mod != Key::NONE) {
  1494. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier")) {
  1495. nav_mode = NAVIGATION_PAN;
  1496. } else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier")) {
  1497. nav_mode = NAVIGATION_ZOOM;
  1498. } else if (mod == Key::ALT || mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier")) {
  1499. // Always allow Alt as a modifier to better support graphic tablets.
  1500. nav_mode = NAVIGATION_ORBIT;
  1501. }
  1502. }
  1503. }
  1504. switch (nav_mode) {
  1505. case NAVIGATION_PAN: {
  1506. _nav_pan(m, _get_warped_mouse_motion(m));
  1507. } break;
  1508. case NAVIGATION_ZOOM: {
  1509. _nav_zoom(m, m->get_relative());
  1510. } break;
  1511. case NAVIGATION_ORBIT: {
  1512. _nav_orbit(m, _get_warped_mouse_motion(m));
  1513. } break;
  1514. case NAVIGATION_LOOK: {
  1515. _nav_look(m, _get_warped_mouse_motion(m));
  1516. } break;
  1517. default: {
  1518. }
  1519. }
  1520. }
  1521. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  1522. if (magnify_gesture.is_valid()) {
  1523. if (is_freelook_active()) {
  1524. scale_freelook_speed(magnify_gesture->get_factor());
  1525. } else {
  1526. scale_cursor_distance(1.0 / magnify_gesture->get_factor());
  1527. }
  1528. }
  1529. Ref<InputEventPanGesture> pan_gesture = p_event;
  1530. if (pan_gesture.is_valid()) {
  1531. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1532. NavigationMode nav_mode = NAVIGATION_NONE;
  1533. if (nav_scheme == NAVIGATION_GODOT) {
  1534. const Key mod = _get_key_modifier(pan_gesture);
  1535. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier")) {
  1536. nav_mode = NAVIGATION_PAN;
  1537. } else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier")) {
  1538. nav_mode = NAVIGATION_ZOOM;
  1539. } else if (mod == Key::ALT || mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier")) {
  1540. // Always allow Alt as a modifier to better support graphic tablets.
  1541. nav_mode = NAVIGATION_ORBIT;
  1542. }
  1543. } else if (nav_scheme == NAVIGATION_MAYA) {
  1544. if (pan_gesture->is_alt_pressed()) {
  1545. nav_mode = NAVIGATION_PAN;
  1546. }
  1547. }
  1548. switch (nav_mode) {
  1549. case NAVIGATION_PAN: {
  1550. _nav_pan(pan_gesture, pan_gesture->get_delta());
  1551. } break;
  1552. case NAVIGATION_ZOOM: {
  1553. _nav_zoom(pan_gesture, pan_gesture->get_delta());
  1554. } break;
  1555. case NAVIGATION_ORBIT: {
  1556. _nav_orbit(pan_gesture, pan_gesture->get_delta());
  1557. } break;
  1558. case NAVIGATION_LOOK: {
  1559. _nav_look(pan_gesture, pan_gesture->get_delta());
  1560. } break;
  1561. default: {
  1562. }
  1563. }
  1564. }
  1565. Ref<InputEventKey> k = p_event;
  1566. if (k.is_valid()) {
  1567. if (!k->is_pressed()) {
  1568. return;
  1569. }
  1570. if (EditorSettings::get_singleton()->get("editors/3d/navigation/emulate_numpad")) {
  1571. const Key code = k->get_physical_keycode();
  1572. if (code >= Key::KEY_0 && code <= Key::KEY_9) {
  1573. k->set_keycode(code - Key::KEY_0 + Key::KP_0);
  1574. }
  1575. }
  1576. if (_edit.mode == TRANSFORM_NONE) {
  1577. if (k->get_keycode() == Key::ESCAPE && !cursor.region_select) {
  1578. _clear_selected();
  1579. return;
  1580. }
  1581. } else {
  1582. // We're actively transforming, handle keys specially
  1583. TransformPlane new_plane = TRANSFORM_VIEW;
  1584. String new_message;
  1585. if (ED_IS_SHORTCUT("spatial_editor/lock_transform_x", p_event)) {
  1586. new_plane = TRANSFORM_X_AXIS;
  1587. new_message = TTR("X-Axis Transform.");
  1588. } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_y", p_event)) {
  1589. new_plane = TRANSFORM_Y_AXIS;
  1590. new_message = TTR("Y-Axis Transform.");
  1591. } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_z", p_event)) {
  1592. new_plane = TRANSFORM_Z_AXIS;
  1593. new_message = TTR("Z-Axis Transform.");
  1594. } else if (_edit.mode != TRANSFORM_ROTATE) { // rotating on a plane doesn't make sense
  1595. if (ED_IS_SHORTCUT("spatial_editor/lock_transform_yz", p_event)) {
  1596. new_plane = TRANSFORM_YZ;
  1597. new_message = TTR("YZ-Plane Transform.");
  1598. } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_xz", p_event)) {
  1599. new_plane = TRANSFORM_XZ;
  1600. new_message = TTR("XZ-Plane Transform.");
  1601. } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_xy", p_event)) {
  1602. new_plane = TRANSFORM_XY;
  1603. new_message = TTR("XY-Plane Transform.");
  1604. }
  1605. }
  1606. if (new_plane != TRANSFORM_VIEW) {
  1607. if (new_plane != _edit.plane) {
  1608. // lock me once and get a global constraint
  1609. _edit.plane = new_plane;
  1610. spatial_editor->set_local_coords_enabled(false);
  1611. } else if (!spatial_editor->are_local_coords_enabled()) {
  1612. // lock me twice and get a local constraint
  1613. spatial_editor->set_local_coords_enabled(true);
  1614. } else {
  1615. // lock me thrice and we're back where we started
  1616. _edit.plane = TRANSFORM_VIEW;
  1617. spatial_editor->set_local_coords_enabled(false);
  1618. }
  1619. update_transform(_edit.mouse_pos, Input::get_singleton()->is_key_pressed(Key::SHIFT));
  1620. set_message(new_message, 2);
  1621. accept_event();
  1622. return;
  1623. }
  1624. }
  1625. if (ED_IS_SHORTCUT("spatial_editor/snap", p_event)) {
  1626. if (_edit.mode != TRANSFORM_NONE) {
  1627. _edit.snap = !_edit.snap;
  1628. }
  1629. }
  1630. if (ED_IS_SHORTCUT("spatial_editor/bottom_view", p_event)) {
  1631. _menu_option(VIEW_BOTTOM);
  1632. }
  1633. if (ED_IS_SHORTCUT("spatial_editor/top_view", p_event)) {
  1634. _menu_option(VIEW_TOP);
  1635. }
  1636. if (ED_IS_SHORTCUT("spatial_editor/rear_view", p_event)) {
  1637. _menu_option(VIEW_REAR);
  1638. }
  1639. if (ED_IS_SHORTCUT("spatial_editor/front_view", p_event)) {
  1640. _menu_option(VIEW_FRONT);
  1641. }
  1642. if (ED_IS_SHORTCUT("spatial_editor/left_view", p_event)) {
  1643. _menu_option(VIEW_LEFT);
  1644. }
  1645. if (ED_IS_SHORTCUT("spatial_editor/right_view", p_event)) {
  1646. _menu_option(VIEW_RIGHT);
  1647. }
  1648. if (ED_IS_SHORTCUT("spatial_editor/orbit_view_down", p_event)) {
  1649. // Clamp rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1650. cursor.x_rot = CLAMP(cursor.x_rot - Math_PI / 12.0, -1.57, 1.57);
  1651. view_type = VIEW_TYPE_USER;
  1652. _update_name();
  1653. }
  1654. if (ED_IS_SHORTCUT("spatial_editor/orbit_view_up", p_event)) {
  1655. // Clamp rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1656. cursor.x_rot = CLAMP(cursor.x_rot + Math_PI / 12.0, -1.57, 1.57);
  1657. view_type = VIEW_TYPE_USER;
  1658. _update_name();
  1659. }
  1660. if (ED_IS_SHORTCUT("spatial_editor/orbit_view_right", p_event)) {
  1661. cursor.y_rot -= Math_PI / 12.0;
  1662. view_type = VIEW_TYPE_USER;
  1663. _update_name();
  1664. }
  1665. if (ED_IS_SHORTCUT("spatial_editor/orbit_view_left", p_event)) {
  1666. cursor.y_rot += Math_PI / 12.0;
  1667. view_type = VIEW_TYPE_USER;
  1668. _update_name();
  1669. }
  1670. if (ED_IS_SHORTCUT("spatial_editor/orbit_view_180", p_event)) {
  1671. cursor.y_rot += Math_PI;
  1672. view_type = VIEW_TYPE_USER;
  1673. _update_name();
  1674. }
  1675. if (ED_IS_SHORTCUT("spatial_editor/focus_origin", p_event)) {
  1676. _menu_option(VIEW_CENTER_TO_ORIGIN);
  1677. }
  1678. if (ED_IS_SHORTCUT("spatial_editor/focus_selection", p_event)) {
  1679. _menu_option(VIEW_CENTER_TO_SELECTION);
  1680. }
  1681. // Orthgonal mode doesn't work in freelook.
  1682. if (!freelook_active && ED_IS_SHORTCUT("spatial_editor/switch_perspective_orthogonal", p_event)) {
  1683. _menu_option(orthogonal ? VIEW_PERSPECTIVE : VIEW_ORTHOGONAL);
  1684. _update_name();
  1685. }
  1686. if (ED_IS_SHORTCUT("spatial_editor/align_transform_with_view", p_event)) {
  1687. _menu_option(VIEW_ALIGN_TRANSFORM_WITH_VIEW);
  1688. }
  1689. if (ED_IS_SHORTCUT("spatial_editor/align_rotation_with_view", p_event)) {
  1690. _menu_option(VIEW_ALIGN_ROTATION_WITH_VIEW);
  1691. }
  1692. if (ED_IS_SHORTCUT("spatial_editor/insert_anim_key", p_event)) {
  1693. if (!get_selected_count() || _edit.mode != TRANSFORM_NONE) {
  1694. return;
  1695. }
  1696. if (!AnimationPlayerEditor::get_singleton()->get_track_editor()->has_keying()) {
  1697. set_message(TTR("Keying is disabled (no key inserted)."));
  1698. return;
  1699. }
  1700. List<Node *> &selection = editor_selection->get_selected_node_list();
  1701. for (Node *E : selection) {
  1702. Node3D *sp = Object::cast_to<Node3D>(E);
  1703. if (!sp) {
  1704. continue;
  1705. }
  1706. spatial_editor->emit_signal(SNAME("transform_key_request"), sp, "", sp->get_transform());
  1707. }
  1708. set_message(TTR("Animation Key Inserted."));
  1709. }
  1710. if (ED_IS_SHORTCUT("spatial_editor/cancel_transform", p_event) && _edit.mode != TRANSFORM_NONE) {
  1711. cancel_transform();
  1712. }
  1713. if (!is_freelook_active()) {
  1714. if (ED_IS_SHORTCUT("spatial_editor/instant_translate", p_event)) {
  1715. begin_transform(TRANSFORM_TRANSLATE, true);
  1716. }
  1717. if (ED_IS_SHORTCUT("spatial_editor/instant_rotate", p_event)) {
  1718. begin_transform(TRANSFORM_ROTATE, true);
  1719. }
  1720. if (ED_IS_SHORTCUT("spatial_editor/instant_scale", p_event)) {
  1721. begin_transform(TRANSFORM_SCALE, true);
  1722. }
  1723. }
  1724. // Freelook doesn't work in orthogonal mode.
  1725. if (!orthogonal && ED_IS_SHORTCUT("spatial_editor/freelook_toggle", p_event)) {
  1726. set_freelook_active(!is_freelook_active());
  1727. } else if (k->get_keycode() == Key::ESCAPE) {
  1728. set_freelook_active(false);
  1729. }
  1730. if (k->get_keycode() == Key::SPACE) {
  1731. if (!k->is_pressed()) {
  1732. emit_signal(SNAME("toggle_maximize_view"), this);
  1733. }
  1734. }
  1735. if (ED_IS_SHORTCUT("spatial_editor/decrease_fov", p_event)) {
  1736. scale_fov(-0.05);
  1737. }
  1738. if (ED_IS_SHORTCUT("spatial_editor/increase_fov", p_event)) {
  1739. scale_fov(0.05);
  1740. }
  1741. if (ED_IS_SHORTCUT("spatial_editor/reset_fov", p_event)) {
  1742. reset_fov();
  1743. }
  1744. }
  1745. // freelook uses most of the useful shortcuts, like save, so its ok
  1746. // to consider freelook active as end of the line for future events.
  1747. if (freelook_active) {
  1748. accept_event();
  1749. }
  1750. }
  1751. void Node3DEditorViewport::_nav_pan(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1752. const NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1753. real_t pan_speed = 1 / 150.0;
  1754. if (nav_scheme == NAVIGATION_MAYA && p_event->is_shift_pressed()) {
  1755. pan_speed *= 10;
  1756. }
  1757. Transform3D camera_transform;
  1758. camera_transform.translate(cursor.pos);
  1759. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  1760. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  1761. const bool invert_x_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_x_axis");
  1762. const bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y_axis");
  1763. Vector3 translation(
  1764. (invert_x_axis ? -1 : 1) * -p_relative.x * pan_speed,
  1765. (invert_y_axis ? -1 : 1) * p_relative.y * pan_speed,
  1766. 0);
  1767. translation *= cursor.distance / DISTANCE_DEFAULT;
  1768. camera_transform.translate(translation);
  1769. cursor.pos = camera_transform.origin;
  1770. }
  1771. void Node3DEditorViewport::_nav_zoom(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1772. const NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1773. real_t zoom_speed = 1 / 80.0;
  1774. if (nav_scheme == NAVIGATION_MAYA && p_event->is_shift_pressed()) {
  1775. zoom_speed *= 10;
  1776. }
  1777. NavigationZoomStyle zoom_style = (NavigationZoomStyle)EditorSettings::get_singleton()->get("editors/3d/navigation/zoom_style").operator int();
  1778. if (zoom_style == NAVIGATION_ZOOM_HORIZONTAL) {
  1779. if (p_relative.x > 0) {
  1780. scale_cursor_distance(1 - p_relative.x * zoom_speed);
  1781. } else if (p_relative.x < 0) {
  1782. scale_cursor_distance(1.0 / (1 + p_relative.x * zoom_speed));
  1783. }
  1784. } else {
  1785. if (p_relative.y > 0) {
  1786. scale_cursor_distance(1 + p_relative.y * zoom_speed);
  1787. } else if (p_relative.y < 0) {
  1788. scale_cursor_distance(1.0 / (1 - p_relative.y * zoom_speed));
  1789. }
  1790. }
  1791. }
  1792. void Node3DEditorViewport::_nav_orbit(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1793. if (lock_rotation) {
  1794. _nav_pan(p_event, p_relative);
  1795. return;
  1796. }
  1797. if (orthogonal && auto_orthogonal) {
  1798. _menu_option(VIEW_PERSPECTIVE);
  1799. }
  1800. const real_t degrees_per_pixel = EditorSettings::get_singleton()->get("editors/3d/navigation_feel/orbit_sensitivity");
  1801. const real_t radians_per_pixel = Math::deg2rad(degrees_per_pixel);
  1802. const bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y_axis");
  1803. const bool invert_x_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_x_axis");
  1804. if (invert_y_axis) {
  1805. cursor.x_rot -= p_relative.y * radians_per_pixel;
  1806. } else {
  1807. cursor.x_rot += p_relative.y * radians_per_pixel;
  1808. }
  1809. // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1810. cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57);
  1811. if (invert_x_axis) {
  1812. cursor.y_rot -= p_relative.x * radians_per_pixel;
  1813. } else {
  1814. cursor.y_rot += p_relative.x * radians_per_pixel;
  1815. }
  1816. view_type = VIEW_TYPE_USER;
  1817. _update_name();
  1818. }
  1819. void Node3DEditorViewport::_nav_look(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1820. if (orthogonal) {
  1821. _nav_pan(p_event, p_relative);
  1822. return;
  1823. }
  1824. if (orthogonal && auto_orthogonal) {
  1825. _menu_option(VIEW_PERSPECTIVE);
  1826. }
  1827. // Scale mouse sensitivity with camera FOV scale when zoomed in to make it easier to point at things.
  1828. const real_t degrees_per_pixel = real_t(EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_sensitivity")) * MIN(1.0, cursor.fov_scale);
  1829. const real_t radians_per_pixel = Math::deg2rad(degrees_per_pixel);
  1830. const bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y_axis");
  1831. // Note: do NOT assume the camera has the "current" transform, because it is interpolated and may have "lag".
  1832. const Transform3D prev_camera_transform = to_camera_transform(cursor);
  1833. if (invert_y_axis) {
  1834. cursor.x_rot -= p_relative.y * radians_per_pixel;
  1835. } else {
  1836. cursor.x_rot += p_relative.y * radians_per_pixel;
  1837. }
  1838. // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1839. cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57);
  1840. cursor.y_rot += p_relative.x * radians_per_pixel;
  1841. // Look is like the opposite of Orbit: the focus point rotates around the camera
  1842. Transform3D camera_transform = to_camera_transform(cursor);
  1843. Vector3 pos = camera_transform.xform(Vector3(0, 0, 0));
  1844. Vector3 prev_pos = prev_camera_transform.xform(Vector3(0, 0, 0));
  1845. Vector3 diff = prev_pos - pos;
  1846. cursor.pos += diff;
  1847. view_type = VIEW_TYPE_USER;
  1848. _update_name();
  1849. }
  1850. void Node3DEditorViewport::set_freelook_active(bool active_now) {
  1851. if (!freelook_active && active_now) {
  1852. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  1853. cursor = camera_cursor;
  1854. // Make sure eye_pos is synced, because freelook referential is eye pos rather than orbit pos
  1855. Vector3 forward = to_camera_transform(cursor).basis.xform(Vector3(0, 0, -1));
  1856. cursor.eye_pos = cursor.pos - cursor.distance * forward;
  1857. // Also sync the camera cursor, otherwise switching to freelook will be trippy if inertia is active
  1858. camera_cursor.eye_pos = cursor.eye_pos;
  1859. if (EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_speed_zoom_link")) {
  1860. // Re-adjust freelook speed from the current zoom level
  1861. real_t base_speed = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_base_speed");
  1862. freelook_speed = base_speed * cursor.distance;
  1863. }
  1864. previous_mouse_position = get_local_mouse_position();
  1865. // Hide mouse like in an FPS (warping doesn't work)
  1866. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  1867. } else if (freelook_active && !active_now) {
  1868. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  1869. cursor = camera_cursor;
  1870. // Restore mouse
  1871. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1872. // Restore the previous mouse position when leaving freelook mode.
  1873. // This is done because leaving `Input.MOUSE_MODE_CAPTURED` will center the cursor
  1874. // due to OS limitations.
  1875. warp_mouse(previous_mouse_position);
  1876. }
  1877. freelook_active = active_now;
  1878. }
  1879. void Node3DEditorViewport::scale_fov(real_t p_fov_offset) {
  1880. cursor.fov_scale = CLAMP(cursor.fov_scale + p_fov_offset, 0.1, 2.5);
  1881. surface->update();
  1882. }
  1883. void Node3DEditorViewport::reset_fov() {
  1884. cursor.fov_scale = 1.0;
  1885. surface->update();
  1886. }
  1887. void Node3DEditorViewport::scale_cursor_distance(real_t scale) {
  1888. real_t min_distance = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN);
  1889. real_t max_distance = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX);
  1890. if (unlikely(min_distance > max_distance)) {
  1891. cursor.distance = (min_distance + max_distance) / 2;
  1892. } else {
  1893. cursor.distance = CLAMP(cursor.distance * scale, min_distance, max_distance);
  1894. }
  1895. if (cursor.distance == max_distance || cursor.distance == min_distance) {
  1896. zoom_failed_attempts_count++;
  1897. } else {
  1898. zoom_failed_attempts_count = 0;
  1899. }
  1900. zoom_indicator_delay = ZOOM_FREELOOK_INDICATOR_DELAY_S;
  1901. surface->update();
  1902. }
  1903. void Node3DEditorViewport::scale_freelook_speed(real_t scale) {
  1904. real_t min_speed = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN);
  1905. real_t max_speed = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX);
  1906. if (unlikely(min_speed > max_speed)) {
  1907. freelook_speed = (min_speed + max_speed) / 2;
  1908. } else {
  1909. freelook_speed = CLAMP(freelook_speed * scale, min_speed, max_speed);
  1910. }
  1911. zoom_indicator_delay = ZOOM_FREELOOK_INDICATOR_DELAY_S;
  1912. surface->update();
  1913. }
  1914. Point2i Node3DEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const {
  1915. Point2i relative;
  1916. if (bool(EDITOR_GET("editors/3d/navigation/warped_mouse_panning"))) {
  1917. relative = Input::get_singleton()->warp_mouse_motion(p_ev_mouse_motion, surface->get_global_rect());
  1918. } else {
  1919. relative = p_ev_mouse_motion->get_relative();
  1920. }
  1921. return relative;
  1922. }
  1923. void Node3DEditorViewport::_update_freelook(real_t delta) {
  1924. if (!is_freelook_active()) {
  1925. return;
  1926. }
  1927. const FreelookNavigationScheme navigation_scheme = (FreelookNavigationScheme)EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_navigation_scheme").operator int();
  1928. Vector3 forward;
  1929. if (navigation_scheme == FREELOOK_FULLY_AXIS_LOCKED) {
  1930. // Forward/backward keys will always go straight forward/backward, never moving on the Y axis.
  1931. forward = Vector3(0, 0, -1).rotated(Vector3(0, 1, 0), camera->get_rotation().y);
  1932. } else {
  1933. // Forward/backward keys will be relative to the camera pitch.
  1934. forward = camera->get_transform().basis.xform(Vector3(0, 0, -1));
  1935. }
  1936. const Vector3 right = camera->get_transform().basis.xform(Vector3(1, 0, 0));
  1937. Vector3 up;
  1938. if (navigation_scheme == FREELOOK_PARTIALLY_AXIS_LOCKED || navigation_scheme == FREELOOK_FULLY_AXIS_LOCKED) {
  1939. // Up/down keys will always go up/down regardless of camera pitch.
  1940. up = Vector3(0, 1, 0);
  1941. } else {
  1942. // Up/down keys will be relative to the camera pitch.
  1943. up = camera->get_transform().basis.xform(Vector3(0, 1, 0));
  1944. }
  1945. Vector3 direction;
  1946. // Use actions from the inputmap, as this is the only way to reliably detect input in this method.
  1947. // See #54469 for more discussion and explanation.
  1948. Input *inp = Input::get_singleton();
  1949. if (inp->is_action_pressed("spatial_editor/freelook_left")) {
  1950. direction -= right;
  1951. }
  1952. if (inp->is_action_pressed("spatial_editor/freelook_right")) {
  1953. direction += right;
  1954. }
  1955. if (inp->is_action_pressed("spatial_editor/freelook_forward")) {
  1956. direction += forward;
  1957. }
  1958. if (inp->is_action_pressed("spatial_editor/freelook_backwards")) {
  1959. direction -= forward;
  1960. }
  1961. if (inp->is_action_pressed("spatial_editor/freelook_up")) {
  1962. direction += up;
  1963. }
  1964. if (inp->is_action_pressed("spatial_editor/freelook_down")) {
  1965. direction -= up;
  1966. }
  1967. real_t speed = freelook_speed;
  1968. if (inp->is_action_pressed("spatial_editor/freelook_speed_modifier")) {
  1969. speed *= 3.0;
  1970. }
  1971. if (inp->is_action_pressed("spatial_editor/freelook_slow_modifier")) {
  1972. speed *= 0.333333;
  1973. }
  1974. const Vector3 motion = direction * speed * delta;
  1975. cursor.pos += motion;
  1976. cursor.eye_pos += motion;
  1977. }
  1978. void Node3DEditorViewport::set_message(String p_message, float p_time) {
  1979. message = p_message;
  1980. message_time = p_time;
  1981. }
  1982. void Node3DEditorPlugin::edited_scene_changed() {
  1983. for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {
  1984. Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(i);
  1985. if (viewport->is_visible()) {
  1986. viewport->notification(Control::NOTIFICATION_VISIBILITY_CHANGED);
  1987. }
  1988. }
  1989. }
  1990. void Node3DEditorViewport::_project_settings_changed() {
  1991. //update shadow atlas if changed
  1992. int shadowmap_size = ProjectSettings::get_singleton()->get("rendering/shadows/shadow_atlas/size");
  1993. bool shadowmap_16_bits = ProjectSettings::get_singleton()->get("rendering/shadows/shadow_atlas/16_bits");
  1994. int atlas_q0 = ProjectSettings::get_singleton()->get("rendering/shadows/shadow_atlas/quadrant_0_subdiv");
  1995. int atlas_q1 = ProjectSettings::get_singleton()->get("rendering/shadows/shadow_atlas/quadrant_1_subdiv");
  1996. int atlas_q2 = ProjectSettings::get_singleton()->get("rendering/shadows/shadow_atlas/quadrant_2_subdiv");
  1997. int atlas_q3 = ProjectSettings::get_singleton()->get("rendering/shadows/shadow_atlas/quadrant_3_subdiv");
  1998. viewport->set_shadow_atlas_size(shadowmap_size);
  1999. viewport->set_shadow_atlas_16_bits(shadowmap_16_bits);
  2000. viewport->set_shadow_atlas_quadrant_subdiv(0, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q0));
  2001. viewport->set_shadow_atlas_quadrant_subdiv(1, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q1));
  2002. viewport->set_shadow_atlas_quadrant_subdiv(2, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q2));
  2003. viewport->set_shadow_atlas_quadrant_subdiv(3, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q3));
  2004. _update_shrink();
  2005. // Update MSAA, screen-space AA and debanding if changed
  2006. const int msaa_mode = ProjectSettings::get_singleton()->get("rendering/anti_aliasing/quality/msaa");
  2007. viewport->set_msaa(Viewport::MSAA(msaa_mode));
  2008. const int ssaa_mode = GLOBAL_GET("rendering/anti_aliasing/quality/screen_space_aa");
  2009. viewport->set_screen_space_aa(Viewport::ScreenSpaceAA(ssaa_mode));
  2010. const bool use_taa = GLOBAL_GET("rendering/anti_aliasing/quality/use_taa");
  2011. viewport->set_use_taa(use_taa);
  2012. const bool use_debanding = GLOBAL_GET("rendering/anti_aliasing/quality/use_debanding");
  2013. viewport->set_use_debanding(use_debanding);
  2014. const bool use_occlusion_culling = GLOBAL_GET("rendering/occlusion_culling/use_occlusion_culling");
  2015. viewport->set_use_occlusion_culling(use_occlusion_culling);
  2016. const float mesh_lod_threshold = GLOBAL_GET("rendering/mesh_lod/lod_change/threshold_pixels");
  2017. viewport->set_mesh_lod_threshold(mesh_lod_threshold);
  2018. }
  2019. void Node3DEditorViewport::_notification(int p_what) {
  2020. switch (p_what) {
  2021. case NOTIFICATION_READY: {
  2022. EditorNode::get_singleton()->connect("project_settings_changed", callable_mp(this, &Node3DEditorViewport::_project_settings_changed));
  2023. } break;
  2024. case NOTIFICATION_VISIBILITY_CHANGED: {
  2025. bool visible = is_visible_in_tree();
  2026. set_process(visible);
  2027. if (visible) {
  2028. orthogonal = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL));
  2029. _update_name();
  2030. _update_camera(0);
  2031. } else {
  2032. set_freelook_active(false);
  2033. }
  2034. call_deferred(SNAME("update_transform_gizmo_view"));
  2035. rotation_control->set_visible(EditorSettings::get_singleton()->get("editors/3d/navigation/show_viewport_rotation_gizmo"));
  2036. } break;
  2037. case NOTIFICATION_RESIZED: {
  2038. call_deferred(SNAME("update_transform_gizmo_view"));
  2039. } break;
  2040. case NOTIFICATION_PROCESS: {
  2041. real_t delta = get_process_delta_time();
  2042. if (zoom_indicator_delay > 0) {
  2043. zoom_indicator_delay -= delta;
  2044. if (zoom_indicator_delay <= 0) {
  2045. surface->update();
  2046. zoom_limit_label->hide();
  2047. }
  2048. }
  2049. _update_freelook(delta);
  2050. Node *scene_root = SceneTreeDock::get_singleton()->get_editor_data()->get_edited_scene_root();
  2051. if (previewing_cinema && scene_root != nullptr) {
  2052. Camera3D *cam = scene_root->get_viewport()->get_camera_3d();
  2053. if (cam != nullptr && cam != previewing) {
  2054. //then switch the viewport's camera to the scene's viewport camera
  2055. if (previewing != nullptr) {
  2056. previewing->disconnect("tree_exited", callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  2057. }
  2058. previewing = cam;
  2059. previewing->connect("tree_exited", callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  2060. RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), cam->get_camera());
  2061. surface->update();
  2062. }
  2063. }
  2064. _update_camera(delta);
  2065. const HashMap<Node *, Object *> &selection = editor_selection->get_selection();
  2066. bool changed = false;
  2067. bool exist = false;
  2068. for (const KeyValue<Node *, Object *> &E : selection) {
  2069. Node3D *sp = Object::cast_to<Node3D>(E.key);
  2070. if (!sp) {
  2071. continue;
  2072. }
  2073. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  2074. if (!se) {
  2075. continue;
  2076. }
  2077. Transform3D t = sp->get_global_gizmo_transform();
  2078. VisualInstance3D *vi = Object::cast_to<VisualInstance3D>(sp);
  2079. AABB new_aabb = vi ? vi->get_aabb() : _calculate_spatial_bounds(sp);
  2080. exist = true;
  2081. if (se->last_xform == t && se->aabb == new_aabb && !se->last_xform_dirty) {
  2082. continue;
  2083. }
  2084. changed = true;
  2085. se->last_xform_dirty = false;
  2086. se->last_xform = t;
  2087. se->aabb = new_aabb;
  2088. Transform3D t_offset = t;
  2089. // apply AABB scaling before item's global transform
  2090. {
  2091. const Vector3 offset(0.005, 0.005, 0.005);
  2092. Basis aabb_s;
  2093. aabb_s.scale(se->aabb.size + offset);
  2094. t.translate(se->aabb.position - offset / 2);
  2095. t.basis = t.basis * aabb_s;
  2096. }
  2097. {
  2098. const Vector3 offset(0.01, 0.01, 0.01);
  2099. Basis aabb_s;
  2100. aabb_s.scale(se->aabb.size + offset);
  2101. t_offset.translate(se->aabb.position - offset / 2);
  2102. t_offset.basis = t_offset.basis * aabb_s;
  2103. }
  2104. RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance, t);
  2105. RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_offset, t_offset);
  2106. RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_xray, t);
  2107. RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_xray_offset, t_offset);
  2108. }
  2109. if (changed || (spatial_editor->is_gizmo_visible() && !exist)) {
  2110. spatial_editor->update_transform_gizmo();
  2111. }
  2112. if (message_time > 0) {
  2113. if (message != last_message) {
  2114. surface->update();
  2115. last_message = message;
  2116. }
  2117. message_time -= get_physics_process_delta_time();
  2118. if (message_time < 0) {
  2119. surface->update();
  2120. }
  2121. }
  2122. bool show_info = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  2123. if (show_info != info_label->is_visible()) {
  2124. info_label->set_visible(show_info);
  2125. }
  2126. Camera3D *current_camera;
  2127. if (previewing) {
  2128. current_camera = previewing;
  2129. } else {
  2130. current_camera = camera;
  2131. }
  2132. if (show_info) {
  2133. const String viewport_size = vformat(String::utf8("%d × %d"), viewport->get_size().x, viewport->get_size().y);
  2134. String text;
  2135. text += vformat(TTR("X: %s\n"), rtos(current_camera->get_position().x).pad_decimals(1));
  2136. text += vformat(TTR("Y: %s\n"), rtos(current_camera->get_position().y).pad_decimals(1));
  2137. text += vformat(TTR("Z: %s\n"), rtos(current_camera->get_position().z).pad_decimals(1));
  2138. text += "\n";
  2139. text += vformat(
  2140. TTR("Size: %s (%.1fMP)\n"),
  2141. viewport_size,
  2142. viewport->get_size().x * viewport->get_size().y * 0.000001);
  2143. text += "\n";
  2144. text += vformat(TTR("Objects: %d\n"), viewport->get_render_info(Viewport::RENDER_INFO_TYPE_VISIBLE, Viewport::RENDER_INFO_OBJECTS_IN_FRAME));
  2145. text += vformat(TTR("Primitive Indices: %d\n"), viewport->get_render_info(Viewport::RENDER_INFO_TYPE_VISIBLE, Viewport::RENDER_INFO_PRIMITIVES_IN_FRAME));
  2146. text += vformat(TTR("Draw Calls: %d"), viewport->get_render_info(Viewport::RENDER_INFO_TYPE_VISIBLE, Viewport::RENDER_INFO_DRAW_CALLS_IN_FRAME));
  2147. info_label->set_text(text);
  2148. }
  2149. // FPS Counter.
  2150. bool show_fps = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME));
  2151. if (show_fps != fps_label->is_visible()) {
  2152. cpu_time_label->set_visible(show_fps);
  2153. gpu_time_label->set_visible(show_fps);
  2154. fps_label->set_visible(show_fps);
  2155. RS::get_singleton()->viewport_set_measure_render_time(viewport->get_viewport_rid(), show_fps);
  2156. for (int i = 0; i < FRAME_TIME_HISTORY; i++) {
  2157. cpu_time_history[i] = 0;
  2158. gpu_time_history[i] = 0;
  2159. }
  2160. cpu_time_history_index = 0;
  2161. gpu_time_history_index = 0;
  2162. }
  2163. if (show_fps) {
  2164. cpu_time_history[cpu_time_history_index] = RS::get_singleton()->viewport_get_measured_render_time_cpu(viewport->get_viewport_rid());
  2165. cpu_time_history_index = (cpu_time_history_index + 1) % FRAME_TIME_HISTORY;
  2166. double cpu_time = 0.0;
  2167. for (int i = 0; i < FRAME_TIME_HISTORY; i++) {
  2168. cpu_time += cpu_time_history[i];
  2169. }
  2170. cpu_time /= FRAME_TIME_HISTORY;
  2171. // Prevent unrealistically low values.
  2172. cpu_time = MAX(0.01, cpu_time);
  2173. gpu_time_history[gpu_time_history_index] = RS::get_singleton()->viewport_get_measured_render_time_gpu(viewport->get_viewport_rid());
  2174. gpu_time_history_index = (gpu_time_history_index + 1) % FRAME_TIME_HISTORY;
  2175. double gpu_time = 0.0;
  2176. for (int i = 0; i < FRAME_TIME_HISTORY; i++) {
  2177. gpu_time += gpu_time_history[i];
  2178. }
  2179. gpu_time /= FRAME_TIME_HISTORY;
  2180. // Prevent division by zero for the FPS counter (and unrealistically low values).
  2181. // This limits the reported FPS to 100000.
  2182. gpu_time = MAX(0.01, gpu_time);
  2183. // Color labels depending on performance level ("good" = green, "OK" = yellow, "bad" = red).
  2184. // Middle point is at 15 ms.
  2185. cpu_time_label->set_text(vformat(TTR("CPU Time: %s ms"), rtos(cpu_time).pad_decimals(2)));
  2186. cpu_time_label->add_theme_color_override(
  2187. "font_color",
  2188. frame_time_gradient->get_color_at_offset(
  2189. Math::range_lerp(cpu_time, 0, 30, 0, 1)));
  2190. gpu_time_label->set_text(vformat(TTR("GPU Time: %s ms"), rtos(gpu_time).pad_decimals(2)));
  2191. // Middle point is at 15 ms.
  2192. gpu_time_label->add_theme_color_override(
  2193. "font_color",
  2194. frame_time_gradient->get_color_at_offset(
  2195. Math::range_lerp(gpu_time, 0, 30, 0, 1)));
  2196. const double fps = 1000.0 / gpu_time;
  2197. fps_label->set_text(vformat(TTR("FPS: %d"), fps));
  2198. // Middle point is at 60 FPS.
  2199. fps_label->add_theme_color_override(
  2200. "font_color",
  2201. frame_time_gradient->get_color_at_offset(
  2202. Math::range_lerp(fps, 110, 10, 0, 1)));
  2203. }
  2204. bool show_cinema = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW));
  2205. cinema_label->set_visible(show_cinema);
  2206. if (show_cinema) {
  2207. float cinema_half_width = cinema_label->get_size().width / 2.0f;
  2208. cinema_label->set_anchor_and_offset(SIDE_LEFT, 0.5f, -cinema_half_width);
  2209. }
  2210. if (lock_rotation) {
  2211. float locked_half_width = locked_label->get_size().width / 2.0f;
  2212. locked_label->set_anchor_and_offset(SIDE_LEFT, 0.5f, -locked_half_width);
  2213. }
  2214. } break;
  2215. case NOTIFICATION_ENTER_TREE: {
  2216. surface->connect("draw", callable_mp(this, &Node3DEditorViewport::_draw));
  2217. surface->connect("gui_input", callable_mp(this, &Node3DEditorViewport::_sinput));
  2218. surface->connect("mouse_entered", callable_mp(this, &Node3DEditorViewport::_surface_mouse_enter));
  2219. surface->connect("mouse_exited", callable_mp(this, &Node3DEditorViewport::_surface_mouse_exit));
  2220. surface->connect("focus_entered", callable_mp(this, &Node3DEditorViewport::_surface_focus_enter));
  2221. surface->connect("focus_exited", callable_mp(this, &Node3DEditorViewport::_surface_focus_exit));
  2222. _init_gizmo_instance(index);
  2223. } break;
  2224. case NOTIFICATION_EXIT_TREE: {
  2225. _finish_gizmo_instances();
  2226. } break;
  2227. case NOTIFICATION_THEME_CHANGED: {
  2228. view_menu->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  2229. preview_camera->set_icon(get_theme_icon(SNAME("Camera3D"), SNAME("EditorIcons")));
  2230. Control *gui_base = EditorNode::get_singleton()->get_gui_base();
  2231. view_menu->add_theme_style_override("normal", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2232. view_menu->add_theme_style_override("hover", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2233. view_menu->add_theme_style_override("pressed", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2234. view_menu->add_theme_style_override("focus", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2235. view_menu->add_theme_style_override("disabled", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2236. preview_camera->add_theme_style_override("normal", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2237. preview_camera->add_theme_style_override("hover", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2238. preview_camera->add_theme_style_override("pressed", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2239. preview_camera->add_theme_style_override("focus", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2240. preview_camera->add_theme_style_override("disabled", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2241. frame_time_gradient->set_color(0, get_theme_color(SNAME("success_color"), SNAME("Editor")));
  2242. frame_time_gradient->set_color(1, get_theme_color(SNAME("warning_color"), SNAME("Editor")));
  2243. frame_time_gradient->set_color(2, get_theme_color(SNAME("error_color"), SNAME("Editor")));
  2244. info_label->add_theme_style_override("normal", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2245. cpu_time_label->add_theme_style_override("normal", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2246. gpu_time_label->add_theme_style_override("normal", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2247. fps_label->add_theme_style_override("normal", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2248. cinema_label->add_theme_style_override("normal", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2249. locked_label->add_theme_style_override("normal", gui_base->get_theme_stylebox(SNAME("Information3dViewport"), SNAME("EditorStyles")));
  2250. } break;
  2251. }
  2252. }
  2253. static void draw_indicator_bar(Control &p_surface, real_t p_fill, const Ref<Texture2D> p_icon, const Ref<Font> p_font, int p_font_size, const String &p_text, const Color &p_color) {
  2254. // Adjust bar size from control height
  2255. const Vector2 surface_size = p_surface.get_size();
  2256. const real_t h = surface_size.y / 2.0;
  2257. const real_t y = (surface_size.y - h) / 2.0;
  2258. const Rect2 r(10 * EDSCALE, y, 6 * EDSCALE, h);
  2259. const real_t sy = r.size.y * p_fill;
  2260. // Note: because this bar appears over the viewport, it has to stay readable for any background color
  2261. // Draw both neutral dark and bright colors to account this
  2262. p_surface.draw_rect(r, p_color * Color(1, 1, 1, 0.2));
  2263. p_surface.draw_rect(Rect2(r.position.x, r.position.y + r.size.y - sy, r.size.x, sy), p_color * Color(1, 1, 1, 0.6));
  2264. p_surface.draw_rect(r.grow(1), Color(0, 0, 0, 0.7), false, Math::round(EDSCALE));
  2265. const Vector2 icon_size = p_icon->get_size();
  2266. const Vector2 icon_pos = Vector2(r.position.x - (icon_size.x - r.size.x) / 2, r.position.y + r.size.y + 2 * EDSCALE);
  2267. p_surface.draw_texture(p_icon, icon_pos, p_color);
  2268. // Draw text below the bar (for speed/zoom information).
  2269. p_surface.draw_string(p_font, Vector2(icon_pos.x, icon_pos.y + icon_size.y + 16 * EDSCALE), p_text, HORIZONTAL_ALIGNMENT_LEFT, -1.f, p_font_size, p_color, Math::round(2 * EDSCALE), Color(0, 0, 0));
  2270. }
  2271. void Node3DEditorViewport::_draw() {
  2272. EditorPluginList *over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_over();
  2273. if (!over_plugin_list->is_empty()) {
  2274. over_plugin_list->forward_spatial_draw_over_viewport(surface);
  2275. }
  2276. EditorPluginList *force_over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_force_over();
  2277. if (!force_over_plugin_list->is_empty()) {
  2278. force_over_plugin_list->forward_spatial_force_draw_over_viewport(surface);
  2279. }
  2280. if (surface->has_focus()) {
  2281. Size2 size = surface->get_size();
  2282. Rect2 r = Rect2(Point2(), size);
  2283. get_theme_stylebox(SNAME("FocusViewport"), SNAME("EditorStyles"))->draw(surface->get_canvas_item(), r);
  2284. }
  2285. if (cursor.region_select) {
  2286. const Rect2 selection_rect = Rect2(cursor.region_begin, cursor.region_end - cursor.region_begin);
  2287. surface->draw_rect(
  2288. selection_rect,
  2289. get_theme_color(SNAME("box_selection_fill_color"), SNAME("Editor")));
  2290. surface->draw_rect(
  2291. selection_rect,
  2292. get_theme_color(SNAME("box_selection_stroke_color"), SNAME("Editor")),
  2293. false,
  2294. Math::round(EDSCALE));
  2295. }
  2296. RID ci = surface->get_canvas_item();
  2297. if (message_time > 0) {
  2298. Ref<Font> font = get_theme_font(SNAME("font"), SNAME("Label"));
  2299. int font_size = get_theme_font_size(SNAME("font_size"), SNAME("Label"));
  2300. Point2 msgpos = Point2(5, get_size().y - 20);
  2301. font->draw_string(ci, msgpos + Point2(1, 1), message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
  2302. font->draw_string(ci, msgpos + Point2(-1, -1), message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
  2303. font->draw_string(ci, msgpos, message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1, 1, 1, 1));
  2304. }
  2305. if (_edit.mode == TRANSFORM_ROTATE && _edit.show_rotation_line) {
  2306. Point2 center = _point_to_screen(_edit.center);
  2307. Color handle_color;
  2308. switch (_edit.plane) {
  2309. case TRANSFORM_X_AXIS:
  2310. handle_color = get_theme_color(SNAME("axis_x_color"), SNAME("Editor"));
  2311. break;
  2312. case TRANSFORM_Y_AXIS:
  2313. handle_color = get_theme_color(SNAME("axis_y_color"), SNAME("Editor"));
  2314. break;
  2315. case TRANSFORM_Z_AXIS:
  2316. handle_color = get_theme_color(SNAME("axis_z_color"), SNAME("Editor"));
  2317. break;
  2318. default:
  2319. handle_color = get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  2320. break;
  2321. }
  2322. handle_color = handle_color.from_hsv(handle_color.get_h(), 0.25, 1.0, 1);
  2323. RenderingServer::get_singleton()->canvas_item_add_line(
  2324. ci,
  2325. _edit.mouse_pos,
  2326. center,
  2327. handle_color,
  2328. Math::round(2 * EDSCALE));
  2329. }
  2330. if (previewing) {
  2331. Size2 ss = Size2(ProjectSettings::get_singleton()->get("display/window/size/viewport_width"), ProjectSettings::get_singleton()->get("display/window/size/viewport_height"));
  2332. float aspect = ss.aspect();
  2333. Size2 s = get_size();
  2334. Rect2 draw_rect;
  2335. switch (previewing->get_keep_aspect_mode()) {
  2336. case Camera3D::KEEP_WIDTH: {
  2337. draw_rect.size = Size2(s.width, s.width / aspect);
  2338. draw_rect.position.x = 0;
  2339. draw_rect.position.y = (s.height - draw_rect.size.y) * 0.5;
  2340. } break;
  2341. case Camera3D::KEEP_HEIGHT: {
  2342. draw_rect.size = Size2(s.height * aspect, s.height);
  2343. draw_rect.position.y = 0;
  2344. draw_rect.position.x = (s.width - draw_rect.size.x) * 0.5;
  2345. } break;
  2346. }
  2347. draw_rect = Rect2(Vector2(), s).intersection(draw_rect);
  2348. surface->draw_rect(draw_rect, Color(0.6, 0.6, 0.1, 0.5), false, Math::round(2 * EDSCALE));
  2349. } else {
  2350. if (zoom_indicator_delay > 0.0) {
  2351. if (is_freelook_active()) {
  2352. // Show speed
  2353. real_t min_speed = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN);
  2354. real_t max_speed = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX);
  2355. real_t scale_length = (max_speed - min_speed);
  2356. if (!Math::is_zero_approx(scale_length)) {
  2357. real_t logscale_t = 1.0 - Math::log1p(freelook_speed - min_speed) / Math::log1p(scale_length);
  2358. // Display the freelook speed to help the user get a better sense of scale.
  2359. const int precision = freelook_speed < 1.0 ? 2 : 1;
  2360. draw_indicator_bar(
  2361. *surface,
  2362. 1.0 - logscale_t,
  2363. get_theme_icon(SNAME("ViewportSpeed"), SNAME("EditorIcons")),
  2364. get_theme_font(SNAME("font"), SNAME("Label")),
  2365. get_theme_font_size(SNAME("font_size"), SNAME("Label")),
  2366. vformat("%s u/s", String::num(freelook_speed).pad_decimals(precision)),
  2367. Color(1.0, 0.95, 0.7));
  2368. }
  2369. } else {
  2370. // Show zoom
  2371. zoom_limit_label->set_visible(zoom_failed_attempts_count > 15);
  2372. real_t min_distance = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN);
  2373. real_t max_distance = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX);
  2374. real_t scale_length = (max_distance - min_distance);
  2375. if (!Math::is_zero_approx(scale_length)) {
  2376. real_t logscale_t = 1.0 - Math::log1p(cursor.distance - min_distance) / Math::log1p(scale_length);
  2377. // Display the zoom center distance to help the user get a better sense of scale.
  2378. const int precision = cursor.distance < 1.0 ? 2 : 1;
  2379. draw_indicator_bar(
  2380. *surface,
  2381. logscale_t,
  2382. get_theme_icon(SNAME("ViewportZoom"), SNAME("EditorIcons")),
  2383. get_theme_font(SNAME("font"), SNAME("Label")),
  2384. get_theme_font_size(SNAME("font_size"), SNAME("Label")),
  2385. vformat("%s u", String::num(cursor.distance).pad_decimals(precision)),
  2386. Color(0.7, 0.95, 1.0));
  2387. }
  2388. }
  2389. }
  2390. }
  2391. }
  2392. void Node3DEditorViewport::_menu_option(int p_option) {
  2393. switch (p_option) {
  2394. case VIEW_TOP: {
  2395. cursor.y_rot = 0;
  2396. cursor.x_rot = Math_PI / 2.0;
  2397. set_message(TTR("Top View."), 2);
  2398. view_type = VIEW_TYPE_TOP;
  2399. _set_auto_orthogonal();
  2400. _update_name();
  2401. } break;
  2402. case VIEW_BOTTOM: {
  2403. cursor.y_rot = 0;
  2404. cursor.x_rot = -Math_PI / 2.0;
  2405. set_message(TTR("Bottom View."), 2);
  2406. view_type = VIEW_TYPE_BOTTOM;
  2407. _set_auto_orthogonal();
  2408. _update_name();
  2409. } break;
  2410. case VIEW_LEFT: {
  2411. cursor.x_rot = 0;
  2412. cursor.y_rot = Math_PI / 2.0;
  2413. set_message(TTR("Left View."), 2);
  2414. view_type = VIEW_TYPE_LEFT;
  2415. _set_auto_orthogonal();
  2416. _update_name();
  2417. } break;
  2418. case VIEW_RIGHT: {
  2419. cursor.x_rot = 0;
  2420. cursor.y_rot = -Math_PI / 2.0;
  2421. set_message(TTR("Right View."), 2);
  2422. view_type = VIEW_TYPE_RIGHT;
  2423. _set_auto_orthogonal();
  2424. _update_name();
  2425. } break;
  2426. case VIEW_FRONT: {
  2427. cursor.x_rot = 0;
  2428. cursor.y_rot = Math_PI;
  2429. set_message(TTR("Front View."), 2);
  2430. view_type = VIEW_TYPE_FRONT;
  2431. _set_auto_orthogonal();
  2432. _update_name();
  2433. } break;
  2434. case VIEW_REAR: {
  2435. cursor.x_rot = 0;
  2436. cursor.y_rot = 0;
  2437. set_message(TTR("Rear View."), 2);
  2438. view_type = VIEW_TYPE_REAR;
  2439. _set_auto_orthogonal();
  2440. _update_name();
  2441. } break;
  2442. case VIEW_CENTER_TO_ORIGIN: {
  2443. cursor.pos = Vector3(0, 0, 0);
  2444. } break;
  2445. case VIEW_CENTER_TO_SELECTION: {
  2446. focus_selection();
  2447. } break;
  2448. case VIEW_ALIGN_TRANSFORM_WITH_VIEW: {
  2449. if (!get_selected_count()) {
  2450. break;
  2451. }
  2452. Transform3D camera_transform = camera->get_global_transform();
  2453. List<Node *> &selection = editor_selection->get_selected_node_list();
  2454. undo_redo->create_action(TTR("Align Transform with View"));
  2455. for (Node *E : selection) {
  2456. Node3D *sp = Object::cast_to<Node3D>(E);
  2457. if (!sp) {
  2458. continue;
  2459. }
  2460. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  2461. if (!se) {
  2462. continue;
  2463. }
  2464. Transform3D xform;
  2465. if (orthogonal) {
  2466. xform = sp->get_global_transform();
  2467. xform.basis.set_euler(camera_transform.basis.get_euler());
  2468. } else {
  2469. xform = camera_transform;
  2470. xform.scale_basis(sp->get_scale());
  2471. }
  2472. undo_redo->add_do_method(sp, "set_global_transform", xform);
  2473. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  2474. }
  2475. undo_redo->commit_action();
  2476. } break;
  2477. case VIEW_ALIGN_ROTATION_WITH_VIEW: {
  2478. if (!get_selected_count()) {
  2479. break;
  2480. }
  2481. Transform3D camera_transform = camera->get_global_transform();
  2482. List<Node *> &selection = editor_selection->get_selected_node_list();
  2483. undo_redo->create_action(TTR("Align Rotation with View"));
  2484. for (Node *E : selection) {
  2485. Node3D *sp = Object::cast_to<Node3D>(E);
  2486. if (!sp) {
  2487. continue;
  2488. }
  2489. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  2490. if (!se) {
  2491. continue;
  2492. }
  2493. undo_redo->add_do_method(sp, "set_rotation", camera_transform.basis.get_euler_normalized());
  2494. undo_redo->add_undo_method(sp, "set_rotation", sp->get_rotation());
  2495. }
  2496. undo_redo->commit_action();
  2497. } break;
  2498. case VIEW_ENVIRONMENT: {
  2499. int idx = view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT);
  2500. bool current = view_menu->get_popup()->is_item_checked(idx);
  2501. current = !current;
  2502. if (current) {
  2503. camera->set_environment(Ref<Resource>());
  2504. } else {
  2505. camera->set_environment(Node3DEditor::get_singleton()->get_viewport_environment());
  2506. }
  2507. view_menu->get_popup()->set_item_checked(idx, current);
  2508. } break;
  2509. case VIEW_PERSPECTIVE: {
  2510. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  2511. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), false);
  2512. orthogonal = false;
  2513. auto_orthogonal = false;
  2514. call_deferred(SNAME("update_transform_gizmo_view"));
  2515. _update_name();
  2516. } break;
  2517. case VIEW_ORTHOGONAL: {
  2518. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), false);
  2519. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), true);
  2520. orthogonal = true;
  2521. auto_orthogonal = false;
  2522. call_deferred(SNAME("update_transform_gizmo_view"));
  2523. _update_name();
  2524. } break;
  2525. case VIEW_AUTO_ORTHOGONAL: {
  2526. int idx = view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL);
  2527. bool current = view_menu->get_popup()->is_item_checked(idx);
  2528. current = !current;
  2529. view_menu->get_popup()->set_item_checked(idx, current);
  2530. if (auto_orthogonal) {
  2531. auto_orthogonal = false;
  2532. _update_name();
  2533. }
  2534. } break;
  2535. case VIEW_LOCK_ROTATION: {
  2536. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  2537. bool current = view_menu->get_popup()->is_item_checked(idx);
  2538. lock_rotation = !current;
  2539. view_menu->get_popup()->set_item_checked(idx, !current);
  2540. if (lock_rotation) {
  2541. locked_label->show();
  2542. } else {
  2543. locked_label->hide();
  2544. }
  2545. } break;
  2546. case VIEW_AUDIO_LISTENER: {
  2547. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  2548. bool current = view_menu->get_popup()->is_item_checked(idx);
  2549. current = !current;
  2550. viewport->set_as_audio_listener_3d(current);
  2551. view_menu->get_popup()->set_item_checked(idx, current);
  2552. } break;
  2553. case VIEW_AUDIO_DOPPLER: {
  2554. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  2555. bool current = view_menu->get_popup()->is_item_checked(idx);
  2556. current = !current;
  2557. camera->set_doppler_tracking(current ? Camera3D::DOPPLER_TRACKING_IDLE_STEP : Camera3D::DOPPLER_TRACKING_DISABLED);
  2558. view_menu->get_popup()->set_item_checked(idx, current);
  2559. } break;
  2560. case VIEW_CINEMATIC_PREVIEW: {
  2561. int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW);
  2562. bool current = view_menu->get_popup()->is_item_checked(idx);
  2563. current = !current;
  2564. view_menu->get_popup()->set_item_checked(idx, current);
  2565. previewing_cinema = true;
  2566. _toggle_cinema_preview(current);
  2567. if (current) {
  2568. preview_camera->hide();
  2569. } else {
  2570. if (previewing != nullptr) {
  2571. preview_camera->show();
  2572. }
  2573. }
  2574. } break;
  2575. case VIEW_GIZMOS: {
  2576. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  2577. bool current = view_menu->get_popup()->is_item_checked(idx);
  2578. current = !current;
  2579. uint32_t layers = ((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + index)) | (1 << GIZMO_GRID_LAYER) | (1 << MISC_TOOL_LAYER);
  2580. if (current) {
  2581. layers |= (1 << GIZMO_EDIT_LAYER);
  2582. }
  2583. camera->set_cull_mask(layers);
  2584. view_menu->get_popup()->set_item_checked(idx, current);
  2585. } break;
  2586. case VIEW_HALF_RESOLUTION: {
  2587. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  2588. bool current = view_menu->get_popup()->is_item_checked(idx);
  2589. view_menu->get_popup()->set_item_checked(idx, !current);
  2590. _update_shrink();
  2591. } break;
  2592. case VIEW_INFORMATION: {
  2593. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  2594. bool current = view_menu->get_popup()->is_item_checked(idx);
  2595. view_menu->get_popup()->set_item_checked(idx, !current);
  2596. } break;
  2597. case VIEW_FRAME_TIME: {
  2598. int idx = view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME);
  2599. bool current = view_menu->get_popup()->is_item_checked(idx);
  2600. view_menu->get_popup()->set_item_checked(idx, !current);
  2601. } break;
  2602. case VIEW_DISPLAY_NORMAL:
  2603. case VIEW_DISPLAY_WIREFRAME:
  2604. case VIEW_DISPLAY_OVERDRAW:
  2605. case VIEW_DISPLAY_SHADELESS:
  2606. case VIEW_DISPLAY_LIGHTING:
  2607. case VIEW_DISPLAY_NORMAL_BUFFER:
  2608. case VIEW_DISPLAY_DEBUG_SHADOW_ATLAS:
  2609. case VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS:
  2610. case VIEW_DISPLAY_DEBUG_VOXEL_GI_ALBEDO:
  2611. case VIEW_DISPLAY_DEBUG_VOXEL_GI_LIGHTING:
  2612. case VIEW_DISPLAY_DEBUG_VOXEL_GI_EMISSION:
  2613. case VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE:
  2614. case VIEW_DISPLAY_DEBUG_SSAO:
  2615. case VIEW_DISPLAY_DEBUG_SSIL:
  2616. case VIEW_DISPLAY_DEBUG_PSSM_SPLITS:
  2617. case VIEW_DISPLAY_DEBUG_DECAL_ATLAS:
  2618. case VIEW_DISPLAY_DEBUG_SDFGI:
  2619. case VIEW_DISPLAY_DEBUG_SDFGI_PROBES:
  2620. case VIEW_DISPLAY_DEBUG_GI_BUFFER:
  2621. case VIEW_DISPLAY_DEBUG_DISABLE_LOD:
  2622. case VIEW_DISPLAY_DEBUG_CLUSTER_OMNI_LIGHTS:
  2623. case VIEW_DISPLAY_DEBUG_CLUSTER_SPOT_LIGHTS:
  2624. case VIEW_DISPLAY_DEBUG_CLUSTER_DECALS:
  2625. case VIEW_DISPLAY_DEBUG_CLUSTER_REFLECTION_PROBES:
  2626. case VIEW_DISPLAY_DEBUG_OCCLUDERS:
  2627. case VIEW_DISPLAY_MOTION_VECTORS: {
  2628. static const int display_options[] = {
  2629. VIEW_DISPLAY_NORMAL,
  2630. VIEW_DISPLAY_WIREFRAME,
  2631. VIEW_DISPLAY_OVERDRAW,
  2632. VIEW_DISPLAY_SHADELESS,
  2633. VIEW_DISPLAY_LIGHTING,
  2634. VIEW_DISPLAY_NORMAL_BUFFER,
  2635. VIEW_DISPLAY_WIREFRAME,
  2636. VIEW_DISPLAY_DEBUG_SHADOW_ATLAS,
  2637. VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS,
  2638. VIEW_DISPLAY_DEBUG_VOXEL_GI_ALBEDO,
  2639. VIEW_DISPLAY_DEBUG_VOXEL_GI_LIGHTING,
  2640. VIEW_DISPLAY_DEBUG_VOXEL_GI_EMISSION,
  2641. VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE,
  2642. VIEW_DISPLAY_DEBUG_SSAO,
  2643. VIEW_DISPLAY_DEBUG_SSIL,
  2644. VIEW_DISPLAY_DEBUG_GI_BUFFER,
  2645. VIEW_DISPLAY_DEBUG_DISABLE_LOD,
  2646. VIEW_DISPLAY_DEBUG_PSSM_SPLITS,
  2647. VIEW_DISPLAY_DEBUG_DECAL_ATLAS,
  2648. VIEW_DISPLAY_DEBUG_SDFGI,
  2649. VIEW_DISPLAY_DEBUG_SDFGI_PROBES,
  2650. VIEW_DISPLAY_DEBUG_CLUSTER_OMNI_LIGHTS,
  2651. VIEW_DISPLAY_DEBUG_CLUSTER_SPOT_LIGHTS,
  2652. VIEW_DISPLAY_DEBUG_CLUSTER_DECALS,
  2653. VIEW_DISPLAY_DEBUG_CLUSTER_REFLECTION_PROBES,
  2654. VIEW_DISPLAY_DEBUG_OCCLUDERS,
  2655. VIEW_DISPLAY_MOTION_VECTORS,
  2656. VIEW_MAX
  2657. };
  2658. static const Viewport::DebugDraw debug_draw_modes[] = {
  2659. Viewport::DEBUG_DRAW_DISABLED,
  2660. Viewport::DEBUG_DRAW_WIREFRAME,
  2661. Viewport::DEBUG_DRAW_OVERDRAW,
  2662. Viewport::DEBUG_DRAW_UNSHADED,
  2663. Viewport::DEBUG_DRAW_LIGHTING,
  2664. Viewport::DEBUG_DRAW_NORMAL_BUFFER,
  2665. Viewport::DEBUG_DRAW_WIREFRAME,
  2666. Viewport::DEBUG_DRAW_SHADOW_ATLAS,
  2667. Viewport::DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS,
  2668. Viewport::DEBUG_DRAW_VOXEL_GI_ALBEDO,
  2669. Viewport::DEBUG_DRAW_VOXEL_GI_LIGHTING,
  2670. Viewport::DEBUG_DRAW_VOXEL_GI_EMISSION,
  2671. Viewport::DEBUG_DRAW_SCENE_LUMINANCE,
  2672. Viewport::DEBUG_DRAW_SSAO,
  2673. Viewport::DEBUG_DRAW_SSIL,
  2674. Viewport::DEBUG_DRAW_GI_BUFFER,
  2675. Viewport::DEBUG_DRAW_DISABLE_LOD,
  2676. Viewport::DEBUG_DRAW_PSSM_SPLITS,
  2677. Viewport::DEBUG_DRAW_DECAL_ATLAS,
  2678. Viewport::DEBUG_DRAW_SDFGI,
  2679. Viewport::DEBUG_DRAW_SDFGI_PROBES,
  2680. Viewport::DEBUG_DRAW_CLUSTER_OMNI_LIGHTS,
  2681. Viewport::DEBUG_DRAW_CLUSTER_SPOT_LIGHTS,
  2682. Viewport::DEBUG_DRAW_CLUSTER_DECALS,
  2683. Viewport::DEBUG_DRAW_CLUSTER_REFLECTION_PROBES,
  2684. Viewport::DEBUG_DRAW_OCCLUDERS,
  2685. Viewport::DEBUG_DRAW_MOTION_VECTORS,
  2686. };
  2687. int idx = 0;
  2688. while (display_options[idx] != VIEW_MAX) {
  2689. int id = display_options[idx];
  2690. int item_idx = view_menu->get_popup()->get_item_index(id);
  2691. if (item_idx != -1) {
  2692. view_menu->get_popup()->set_item_checked(item_idx, id == p_option);
  2693. }
  2694. item_idx = display_submenu->get_item_index(id);
  2695. if (item_idx != -1) {
  2696. display_submenu->set_item_checked(item_idx, id == p_option);
  2697. }
  2698. if (id == p_option) {
  2699. viewport->set_debug_draw(debug_draw_modes[idx]);
  2700. }
  2701. idx++;
  2702. }
  2703. } break;
  2704. }
  2705. }
  2706. void Node3DEditorViewport::_set_auto_orthogonal() {
  2707. if (!orthogonal && view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL))) {
  2708. _menu_option(VIEW_ORTHOGONAL);
  2709. auto_orthogonal = true;
  2710. }
  2711. }
  2712. void Node3DEditorViewport::_preview_exited_scene() {
  2713. preview_camera->disconnect("toggled", callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview));
  2714. preview_camera->set_pressed(false);
  2715. _toggle_camera_preview(false);
  2716. preview_camera->connect("toggled", callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview));
  2717. view_menu->show();
  2718. }
  2719. void Node3DEditorViewport::_init_gizmo_instance(int p_idx) {
  2720. uint32_t layer = 1 << (GIZMO_BASE_LAYER + p_idx);
  2721. for (int i = 0; i < 3; i++) {
  2722. move_gizmo_instance[i] = RS::get_singleton()->instance_create();
  2723. RS::get_singleton()->instance_set_base(move_gizmo_instance[i], spatial_editor->get_move_gizmo(i)->get_rid());
  2724. RS::get_singleton()->instance_set_scenario(move_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  2725. RS::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  2726. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  2727. RS::get_singleton()->instance_set_layer_mask(move_gizmo_instance[i], layer);
  2728. RS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  2729. move_plane_gizmo_instance[i] = RS::get_singleton()->instance_create();
  2730. RS::get_singleton()->instance_set_base(move_plane_gizmo_instance[i], spatial_editor->get_move_plane_gizmo(i)->get_rid());
  2731. RS::get_singleton()->instance_set_scenario(move_plane_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  2732. RS::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  2733. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_plane_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  2734. RS::get_singleton()->instance_set_layer_mask(move_plane_gizmo_instance[i], layer);
  2735. RS::get_singleton()->instance_geometry_set_flag(move_plane_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  2736. rotate_gizmo_instance[i] = RS::get_singleton()->instance_create();
  2737. RS::get_singleton()->instance_set_base(rotate_gizmo_instance[i], spatial_editor->get_rotate_gizmo(i)->get_rid());
  2738. RS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  2739. RS::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  2740. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  2741. RS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[i], layer);
  2742. RS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  2743. scale_gizmo_instance[i] = RS::get_singleton()->instance_create();
  2744. RS::get_singleton()->instance_set_base(scale_gizmo_instance[i], spatial_editor->get_scale_gizmo(i)->get_rid());
  2745. RS::get_singleton()->instance_set_scenario(scale_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  2746. RS::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  2747. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  2748. RS::get_singleton()->instance_set_layer_mask(scale_gizmo_instance[i], layer);
  2749. RS::get_singleton()->instance_geometry_set_flag(scale_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  2750. scale_plane_gizmo_instance[i] = RS::get_singleton()->instance_create();
  2751. RS::get_singleton()->instance_set_base(scale_plane_gizmo_instance[i], spatial_editor->get_scale_plane_gizmo(i)->get_rid());
  2752. RS::get_singleton()->instance_set_scenario(scale_plane_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  2753. RS::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  2754. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_plane_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  2755. RS::get_singleton()->instance_set_layer_mask(scale_plane_gizmo_instance[i], layer);
  2756. RS::get_singleton()->instance_geometry_set_flag(scale_plane_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  2757. axis_gizmo_instance[i] = RS::get_singleton()->instance_create();
  2758. RS::get_singleton()->instance_set_base(axis_gizmo_instance[i], spatial_editor->get_axis_gizmo(i)->get_rid());
  2759. RS::get_singleton()->instance_set_scenario(axis_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  2760. RS::get_singleton()->instance_set_visible(axis_gizmo_instance[i], true);
  2761. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(axis_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  2762. RS::get_singleton()->instance_set_layer_mask(axis_gizmo_instance[i], layer);
  2763. }
  2764. // Rotation white outline
  2765. rotate_gizmo_instance[3] = RS::get_singleton()->instance_create();
  2766. RS::get_singleton()->instance_set_base(rotate_gizmo_instance[3], spatial_editor->get_rotate_gizmo(3)->get_rid());
  2767. RS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[3], get_tree()->get_root()->get_world_3d()->get_scenario());
  2768. RS::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false);
  2769. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[3], RS::SHADOW_CASTING_SETTING_OFF);
  2770. RS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[3], layer);
  2771. RS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[3], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  2772. }
  2773. void Node3DEditorViewport::_finish_gizmo_instances() {
  2774. for (int i = 0; i < 3; i++) {
  2775. RS::get_singleton()->free(move_gizmo_instance[i]);
  2776. RS::get_singleton()->free(move_plane_gizmo_instance[i]);
  2777. RS::get_singleton()->free(rotate_gizmo_instance[i]);
  2778. RS::get_singleton()->free(scale_gizmo_instance[i]);
  2779. RS::get_singleton()->free(scale_plane_gizmo_instance[i]);
  2780. RS::get_singleton()->free(axis_gizmo_instance[i]);
  2781. }
  2782. // Rotation white outline
  2783. RS::get_singleton()->free(rotate_gizmo_instance[3]);
  2784. }
  2785. void Node3DEditorViewport::_toggle_camera_preview(bool p_activate) {
  2786. ERR_FAIL_COND(p_activate && !preview);
  2787. ERR_FAIL_COND(!p_activate && !previewing);
  2788. rotation_control->set_visible(!p_activate);
  2789. if (!p_activate) {
  2790. previewing->disconnect("tree_exiting", callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  2791. previewing = nullptr;
  2792. RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  2793. if (!preview) {
  2794. preview_camera->hide();
  2795. }
  2796. surface->update();
  2797. } else {
  2798. previewing = preview;
  2799. previewing->connect("tree_exiting", callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  2800. RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), preview->get_camera()); //replace
  2801. surface->update();
  2802. }
  2803. }
  2804. void Node3DEditorViewport::_toggle_cinema_preview(bool p_activate) {
  2805. previewing_cinema = p_activate;
  2806. rotation_control->set_visible(!p_activate);
  2807. if (!previewing_cinema) {
  2808. if (previewing != nullptr) {
  2809. previewing->disconnect("tree_exited", callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  2810. }
  2811. previewing = nullptr;
  2812. RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  2813. preview_camera->set_pressed(false);
  2814. if (!preview) {
  2815. preview_camera->hide();
  2816. } else {
  2817. preview_camera->show();
  2818. }
  2819. view_menu->show();
  2820. surface->update();
  2821. }
  2822. }
  2823. void Node3DEditorViewport::_selection_result_pressed(int p_result) {
  2824. if (selection_results.size() <= p_result) {
  2825. return;
  2826. }
  2827. clicked = selection_results[p_result].item->get_instance_id();
  2828. if (clicked.is_valid()) {
  2829. _select_clicked(spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT);
  2830. }
  2831. }
  2832. void Node3DEditorViewport::_selection_menu_hide() {
  2833. selection_results.clear();
  2834. selection_menu->clear();
  2835. selection_menu->reset_size();
  2836. }
  2837. void Node3DEditorViewport::set_can_preview(Camera3D *p_preview) {
  2838. preview = p_preview;
  2839. if (!preview_camera->is_pressed() && !previewing_cinema) {
  2840. preview_camera->set_visible(p_preview);
  2841. }
  2842. }
  2843. void Node3DEditorViewport::update_transform_gizmo_view() {
  2844. if (!is_visible_in_tree()) {
  2845. return;
  2846. }
  2847. Transform3D xform = spatial_editor->get_gizmo_transform();
  2848. Transform3D camera_xform = camera->get_transform();
  2849. if (xform.origin.is_equal_approx(camera_xform.origin)) {
  2850. for (int i = 0; i < 3; i++) {
  2851. RenderingServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  2852. RenderingServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  2853. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  2854. RenderingServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  2855. RenderingServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  2856. RenderingServer::get_singleton()->instance_set_visible(axis_gizmo_instance[i], false);
  2857. }
  2858. // Rotation white outline
  2859. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false);
  2860. return;
  2861. }
  2862. const Vector3 camz = -camera_xform.get_basis().get_column(2).normalized();
  2863. const Vector3 camy = -camera_xform.get_basis().get_column(1).normalized();
  2864. const Plane p = Plane(camz, camera_xform.origin);
  2865. const real_t gizmo_d = MAX(Math::abs(p.distance_to(xform.origin)), CMP_EPSILON);
  2866. const real_t d0 = camera->unproject_position(camera_xform.origin + camz * gizmo_d).y;
  2867. const real_t d1 = camera->unproject_position(camera_xform.origin + camz * gizmo_d + camy).y;
  2868. const real_t dd = MAX(Math::abs(d0 - d1), CMP_EPSILON);
  2869. const real_t gizmo_size = EditorSettings::get_singleton()->get("editors/3d/manipulator_gizmo_size");
  2870. // At low viewport heights, multiply the gizmo scale based on the viewport height.
  2871. // This prevents the gizmo from growing very large and going outside the viewport.
  2872. const int viewport_base_height = 400 * MAX(1, EDSCALE);
  2873. gizmo_scale =
  2874. (gizmo_size / Math::abs(dd)) * MAX(1, EDSCALE) *
  2875. MIN(viewport_base_height, subviewport_container->get_size().height) / viewport_base_height /
  2876. subviewport_container->get_stretch_shrink();
  2877. Vector3 scale = Vector3(1, 1, 1) * gizmo_scale;
  2878. // if the determinant is zero, we should disable the gizmo from being rendered
  2879. // this prevents supplying bad values to the renderer and then having to filter it out again
  2880. if (xform.basis.determinant() == 0) {
  2881. for (int i = 0; i < 3; i++) {
  2882. RenderingServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  2883. RenderingServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  2884. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  2885. RenderingServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  2886. RenderingServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  2887. }
  2888. // Rotation white outline
  2889. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false);
  2890. return;
  2891. }
  2892. for (int i = 0; i < 3; i++) {
  2893. Transform3D axis_angle = Transform3D();
  2894. if (xform.basis.get_column(i).normalized().dot(xform.basis.get_column((i + 1) % 3).normalized()) < 1.0) {
  2895. axis_angle = axis_angle.looking_at(xform.basis.get_column(i).normalized(), xform.basis.get_column((i + 1) % 3).normalized());
  2896. }
  2897. axis_angle.basis.scale(scale);
  2898. axis_angle.origin = xform.origin;
  2899. RenderingServer::get_singleton()->instance_set_transform(move_gizmo_instance[i], axis_angle);
  2900. RenderingServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE));
  2901. RenderingServer::get_singleton()->instance_set_transform(move_plane_gizmo_instance[i], axis_angle);
  2902. RenderingServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE));
  2903. RenderingServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[i], axis_angle);
  2904. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE));
  2905. RenderingServer::get_singleton()->instance_set_transform(scale_gizmo_instance[i], axis_angle);
  2906. RenderingServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE));
  2907. RenderingServer::get_singleton()->instance_set_transform(scale_plane_gizmo_instance[i], axis_angle);
  2908. RenderingServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE));
  2909. RenderingServer::get_singleton()->instance_set_transform(axis_gizmo_instance[i], xform);
  2910. }
  2911. bool show_axes = spatial_editor->is_gizmo_visible() && _edit.mode != TRANSFORM_NONE;
  2912. RenderingServer *rs = RenderingServer::get_singleton();
  2913. rs->instance_set_visible(axis_gizmo_instance[0], show_axes && (_edit.plane == TRANSFORM_X_AXIS || _edit.plane == TRANSFORM_XY || _edit.plane == TRANSFORM_XZ));
  2914. rs->instance_set_visible(axis_gizmo_instance[1], show_axes && (_edit.plane == TRANSFORM_Y_AXIS || _edit.plane == TRANSFORM_XY || _edit.plane == TRANSFORM_YZ));
  2915. rs->instance_set_visible(axis_gizmo_instance[2], show_axes && (_edit.plane == TRANSFORM_Z_AXIS || _edit.plane == TRANSFORM_XZ || _edit.plane == TRANSFORM_YZ));
  2916. // Rotation white outline
  2917. xform.orthonormalize();
  2918. xform.basis.scale(scale);
  2919. RenderingServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[3], xform);
  2920. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE));
  2921. }
  2922. void Node3DEditorViewport::set_state(const Dictionary &p_state) {
  2923. if (p_state.has("position")) {
  2924. cursor.pos = p_state["position"];
  2925. }
  2926. if (p_state.has("x_rotation")) {
  2927. cursor.x_rot = p_state["x_rotation"];
  2928. }
  2929. if (p_state.has("y_rotation")) {
  2930. cursor.y_rot = p_state["y_rotation"];
  2931. }
  2932. if (p_state.has("distance")) {
  2933. cursor.distance = p_state["distance"];
  2934. }
  2935. if (p_state.has("use_orthogonal")) {
  2936. bool orth = p_state["use_orthogonal"];
  2937. if (orth) {
  2938. _menu_option(VIEW_ORTHOGONAL);
  2939. } else {
  2940. _menu_option(VIEW_PERSPECTIVE);
  2941. }
  2942. }
  2943. if (p_state.has("view_type")) {
  2944. view_type = ViewType(p_state["view_type"].operator int());
  2945. _update_name();
  2946. }
  2947. if (p_state.has("auto_orthogonal")) {
  2948. auto_orthogonal = p_state["auto_orthogonal"];
  2949. _update_name();
  2950. }
  2951. if (p_state.has("auto_orthogonal_enabled")) {
  2952. bool enabled = p_state["auto_orthogonal_enabled"];
  2953. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL), enabled);
  2954. }
  2955. if (p_state.has("display_mode")) {
  2956. int display = p_state["display_mode"];
  2957. int idx = view_menu->get_popup()->get_item_index(display);
  2958. if (!view_menu->get_popup()->is_item_checked(idx)) {
  2959. _menu_option(display);
  2960. }
  2961. }
  2962. if (p_state.has("lock_rotation")) {
  2963. lock_rotation = p_state["lock_rotation"];
  2964. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  2965. view_menu->get_popup()->set_item_checked(idx, lock_rotation);
  2966. }
  2967. if (p_state.has("use_environment")) {
  2968. bool env = p_state["use_environment"];
  2969. if (env != camera->get_environment().is_valid()) {
  2970. _menu_option(VIEW_ENVIRONMENT);
  2971. }
  2972. }
  2973. if (p_state.has("listener")) {
  2974. bool listener = p_state["listener"];
  2975. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  2976. viewport->set_as_audio_listener_3d(listener);
  2977. view_menu->get_popup()->set_item_checked(idx, listener);
  2978. }
  2979. if (p_state.has("doppler")) {
  2980. bool doppler = p_state["doppler"];
  2981. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  2982. camera->set_doppler_tracking(doppler ? Camera3D::DOPPLER_TRACKING_IDLE_STEP : Camera3D::DOPPLER_TRACKING_DISABLED);
  2983. view_menu->get_popup()->set_item_checked(idx, doppler);
  2984. }
  2985. if (p_state.has("gizmos")) {
  2986. bool gizmos = p_state["gizmos"];
  2987. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  2988. if (view_menu->get_popup()->is_item_checked(idx) != gizmos) {
  2989. _menu_option(VIEW_GIZMOS);
  2990. }
  2991. }
  2992. if (p_state.has("information")) {
  2993. bool information = p_state["information"];
  2994. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  2995. if (view_menu->get_popup()->is_item_checked(idx) != information) {
  2996. _menu_option(VIEW_INFORMATION);
  2997. }
  2998. }
  2999. if (p_state.has("frame_time")) {
  3000. bool fps = p_state["frame_time"];
  3001. int idx = view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME);
  3002. if (view_menu->get_popup()->is_item_checked(idx) != fps) {
  3003. _menu_option(VIEW_FRAME_TIME);
  3004. }
  3005. }
  3006. if (p_state.has("half_res")) {
  3007. bool half_res = p_state["half_res"];
  3008. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  3009. view_menu->get_popup()->set_item_checked(idx, half_res);
  3010. }
  3011. if (p_state.has("cinematic_preview")) {
  3012. previewing_cinema = p_state["cinematic_preview"];
  3013. int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW);
  3014. view_menu->get_popup()->set_item_checked(idx, previewing_cinema);
  3015. }
  3016. if (preview_camera->is_connected("toggled", callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview))) {
  3017. preview_camera->disconnect("toggled", callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview));
  3018. }
  3019. if (p_state.has("previewing")) {
  3020. Node *pv = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["previewing"]);
  3021. if (Object::cast_to<Camera3D>(pv)) {
  3022. previewing = Object::cast_to<Camera3D>(pv);
  3023. previewing->connect("tree_exiting", callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  3024. RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), previewing->get_camera()); //replace
  3025. surface->update();
  3026. preview_camera->set_pressed(true);
  3027. preview_camera->show();
  3028. }
  3029. }
  3030. preview_camera->connect("toggled", callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview));
  3031. }
  3032. Dictionary Node3DEditorViewport::get_state() const {
  3033. Dictionary d;
  3034. d["position"] = cursor.pos;
  3035. d["x_rotation"] = cursor.x_rot;
  3036. d["y_rotation"] = cursor.y_rot;
  3037. d["distance"] = cursor.distance;
  3038. d["use_environment"] = camera->get_environment().is_valid();
  3039. d["use_orthogonal"] = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL;
  3040. d["view_type"] = view_type;
  3041. d["auto_orthogonal"] = auto_orthogonal;
  3042. d["auto_orthogonal_enabled"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL));
  3043. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL))) {
  3044. d["display_mode"] = VIEW_DISPLAY_NORMAL;
  3045. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME))) {
  3046. d["display_mode"] = VIEW_DISPLAY_WIREFRAME;
  3047. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW))) {
  3048. d["display_mode"] = VIEW_DISPLAY_OVERDRAW;
  3049. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS))) {
  3050. d["display_mode"] = VIEW_DISPLAY_SHADELESS;
  3051. }
  3052. d["listener"] = viewport->is_audio_listener_3d();
  3053. d["doppler"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER));
  3054. d["gizmos"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS));
  3055. d["information"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  3056. d["frame_time"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME));
  3057. d["half_res"] = subviewport_container->get_stretch_shrink() > 1;
  3058. d["cinematic_preview"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW));
  3059. if (previewing) {
  3060. d["previewing"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(previewing);
  3061. }
  3062. if (lock_rotation) {
  3063. d["lock_rotation"] = lock_rotation;
  3064. }
  3065. return d;
  3066. }
  3067. void Node3DEditorViewport::_bind_methods() {
  3068. ClassDB::bind_method(D_METHOD("update_transform_gizmo_view"), &Node3DEditorViewport::update_transform_gizmo_view); // Used by call_deferred.
  3069. ClassDB::bind_method(D_METHOD("_can_drop_data_fw"), &Node3DEditorViewport::can_drop_data_fw);
  3070. ClassDB::bind_method(D_METHOD("_drop_data_fw"), &Node3DEditorViewport::drop_data_fw);
  3071. ADD_SIGNAL(MethodInfo("toggle_maximize_view", PropertyInfo(Variant::OBJECT, "viewport")));
  3072. ADD_SIGNAL(MethodInfo("clicked", PropertyInfo(Variant::OBJECT, "viewport")));
  3073. }
  3074. void Node3DEditorViewport::reset() {
  3075. orthogonal = false;
  3076. auto_orthogonal = false;
  3077. lock_rotation = false;
  3078. message_time = 0;
  3079. message = "";
  3080. last_message = "";
  3081. view_type = VIEW_TYPE_USER;
  3082. cursor = Cursor();
  3083. _update_name();
  3084. }
  3085. void Node3DEditorViewport::focus_selection() {
  3086. Vector3 center;
  3087. int count = 0;
  3088. const List<Node *> &selection = editor_selection->get_selected_node_list();
  3089. for (Node *E : selection) {
  3090. Node3D *sp = Object::cast_to<Node3D>(E);
  3091. if (!sp) {
  3092. continue;
  3093. }
  3094. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  3095. if (!se) {
  3096. continue;
  3097. }
  3098. if (se->gizmo.is_valid()) {
  3099. for (const KeyValue<int, Transform3D> &GE : se->subgizmos) {
  3100. center += se->gizmo->get_subgizmo_transform(GE.key).origin;
  3101. count++;
  3102. }
  3103. }
  3104. center += sp->get_global_gizmo_transform().origin;
  3105. count++;
  3106. }
  3107. if (count != 0) {
  3108. center /= count;
  3109. }
  3110. cursor.pos = center;
  3111. }
  3112. void Node3DEditorViewport::assign_pending_data_pointers(Node3D *p_preview_node, AABB *p_preview_bounds, AcceptDialog *p_accept) {
  3113. preview_node = p_preview_node;
  3114. preview_bounds = p_preview_bounds;
  3115. accept = p_accept;
  3116. }
  3117. Vector3 Node3DEditorViewport::_get_instance_position(const Point2 &p_pos) const {
  3118. const float MAX_DISTANCE = 50.0;
  3119. Vector3 world_ray = _get_ray(p_pos);
  3120. Vector3 world_pos = _get_ray_pos(p_pos);
  3121. Vector3 point = world_pos + world_ray * MAX_DISTANCE;
  3122. PhysicsDirectSpaceState3D *ss = get_tree()->get_root()->get_world_3d()->get_direct_space_state();
  3123. PhysicsDirectSpaceState3D::RayParameters ray_params;
  3124. ray_params.from = world_pos;
  3125. ray_params.to = world_pos + world_ray * MAX_DISTANCE;
  3126. PhysicsDirectSpaceState3D::RayResult result;
  3127. if (ss->intersect_ray(ray_params, result)) {
  3128. point = result.position;
  3129. }
  3130. return point;
  3131. }
  3132. AABB Node3DEditorViewport::_calculate_spatial_bounds(const Node3D *p_parent, bool p_exclude_top_level_transform) {
  3133. AABB bounds;
  3134. const VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(p_parent);
  3135. if (visual_instance) {
  3136. bounds = visual_instance->get_aabb();
  3137. }
  3138. for (int i = 0; i < p_parent->get_child_count(); i++) {
  3139. Node3D *child = Object::cast_to<Node3D>(p_parent->get_child(i));
  3140. if (child) {
  3141. AABB child_bounds = _calculate_spatial_bounds(child, false);
  3142. if (bounds.size == Vector3() && Object::cast_to<Node3D>(p_parent)) {
  3143. bounds = child_bounds;
  3144. } else {
  3145. bounds.merge_with(child_bounds);
  3146. }
  3147. }
  3148. }
  3149. if (bounds.size == Vector3() && !Object::cast_to<Node3D>(p_parent)) {
  3150. bounds = AABB(Vector3(-0.2, -0.2, -0.2), Vector3(0.4, 0.4, 0.4));
  3151. }
  3152. if (!p_exclude_top_level_transform) {
  3153. bounds = p_parent->get_transform().xform(bounds);
  3154. }
  3155. return bounds;
  3156. }
  3157. Node *Node3DEditorViewport::_sanitize_preview_node(Node *p_node) const {
  3158. Node3D *node_3d = Object::cast_to<Node3D>(p_node);
  3159. if (node_3d == nullptr) {
  3160. Node3D *replacement_node = memnew(Node3D);
  3161. replacement_node->set_name(p_node->get_name());
  3162. p_node->replace_by(replacement_node);
  3163. memdelete(p_node);
  3164. p_node = replacement_node;
  3165. } else {
  3166. VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(node_3d);
  3167. if (visual_instance == nullptr) {
  3168. Node3D *replacement_node = memnew(Node3D);
  3169. replacement_node->set_name(node_3d->get_name());
  3170. replacement_node->set_visible(node_3d->is_visible());
  3171. replacement_node->set_transform(node_3d->get_transform());
  3172. replacement_node->set_rotation_edit_mode(node_3d->get_rotation_edit_mode());
  3173. replacement_node->set_rotation_order(node_3d->get_rotation_order());
  3174. replacement_node->set_as_top_level(node_3d->is_set_as_top_level());
  3175. p_node->replace_by(replacement_node);
  3176. memdelete(p_node);
  3177. p_node = replacement_node;
  3178. }
  3179. }
  3180. for (int i = 0; i < p_node->get_child_count(); i++) {
  3181. _sanitize_preview_node(p_node->get_child(i));
  3182. }
  3183. return p_node;
  3184. }
  3185. void Node3DEditorViewport::_create_preview(const Vector<String> &files) const {
  3186. for (int i = 0; i < files.size(); i++) {
  3187. String path = files[i];
  3188. Ref<Resource> res = ResourceLoader::load(path);
  3189. ERR_CONTINUE(res.is_null());
  3190. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  3191. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  3192. if (mesh != nullptr || scene != nullptr) {
  3193. if (mesh != nullptr) {
  3194. MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
  3195. mesh_instance->set_mesh(mesh);
  3196. preview_node->add_child(mesh_instance);
  3197. } else {
  3198. if (scene.is_valid()) {
  3199. Node *instance = scene->instantiate();
  3200. if (instance) {
  3201. instance = _sanitize_preview_node(instance);
  3202. preview_node->add_child(instance);
  3203. }
  3204. }
  3205. }
  3206. EditorNode::get_singleton()->get_scene_root()->add_child(preview_node);
  3207. }
  3208. }
  3209. *preview_bounds = _calculate_spatial_bounds(preview_node);
  3210. }
  3211. void Node3DEditorViewport::_remove_preview() {
  3212. if (preview_node->get_parent()) {
  3213. for (int i = preview_node->get_child_count() - 1; i >= 0; i--) {
  3214. Node *node = preview_node->get_child(i);
  3215. node->queue_delete();
  3216. preview_node->remove_child(node);
  3217. }
  3218. EditorNode::get_singleton()->get_scene_root()->remove_child(preview_node);
  3219. }
  3220. }
  3221. bool Node3DEditorViewport::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
  3222. if (p_desired_node->get_scene_file_path() == p_target_scene_path) {
  3223. return true;
  3224. }
  3225. int childCount = p_desired_node->get_child_count();
  3226. for (int i = 0; i < childCount; i++) {
  3227. Node *child = p_desired_node->get_child(i);
  3228. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  3229. return true;
  3230. }
  3231. }
  3232. return false;
  3233. }
  3234. bool Node3DEditorViewport::_create_instance(Node *parent, String &path, const Point2 &p_point) {
  3235. Ref<Resource> res = ResourceLoader::load(path);
  3236. ERR_FAIL_COND_V(res.is_null(), false);
  3237. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  3238. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  3239. Node *instantiated_scene = nullptr;
  3240. if (mesh != nullptr || scene != nullptr) {
  3241. if (mesh != nullptr) {
  3242. MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
  3243. mesh_instance->set_mesh(mesh);
  3244. // Adjust casing according to project setting. The file name is expected to be in snake_case, but will work for others.
  3245. String name = path.get_file().get_basename();
  3246. switch (ProjectSettings::get_singleton()->get("editor/node_naming/name_casing").operator int()) {
  3247. case NAME_CASING_PASCAL_CASE:
  3248. name = name.capitalize().replace(" ", "");
  3249. break;
  3250. case NAME_CASING_CAMEL_CASE:
  3251. name = name.capitalize().replace(" ", "");
  3252. name[0] = name.to_lower()[0];
  3253. break;
  3254. case NAME_CASING_SNAKE_CASE:
  3255. name = name.capitalize().replace(" ", "_").to_lower();
  3256. break;
  3257. }
  3258. mesh_instance->set_name(name);
  3259. instantiated_scene = mesh_instance;
  3260. } else {
  3261. if (!scene.is_valid()) { // invalid scene
  3262. return false;
  3263. } else {
  3264. instantiated_scene = scene->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
  3265. }
  3266. }
  3267. }
  3268. if (instantiated_scene == nullptr) {
  3269. return false;
  3270. }
  3271. if (!EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path().is_empty()) { // cyclical instancing
  3272. if (_cyclical_dependency_exists(EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path(), instantiated_scene)) {
  3273. memdelete(instantiated_scene);
  3274. return false;
  3275. }
  3276. }
  3277. if (scene != nullptr) {
  3278. instantiated_scene->set_scene_file_path(ProjectSettings::get_singleton()->localize_path(path));
  3279. }
  3280. editor_data->get_undo_redo().add_do_method(parent, "add_child", instantiated_scene, true);
  3281. editor_data->get_undo_redo().add_do_method(instantiated_scene, "set_owner", EditorNode::get_singleton()->get_edited_scene());
  3282. editor_data->get_undo_redo().add_do_reference(instantiated_scene);
  3283. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instantiated_scene);
  3284. String new_name = parent->validate_child_name(instantiated_scene);
  3285. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  3286. editor_data->get_undo_redo().add_do_method(ed, "live_debug_instance_node", EditorNode::get_singleton()->get_edited_scene()->get_path_to(parent), path, new_name);
  3287. editor_data->get_undo_redo().add_undo_method(ed, "live_debug_remove_node", NodePath(String(EditorNode::get_singleton()->get_edited_scene()->get_path_to(parent)) + "/" + new_name));
  3288. Node3D *node3d = Object::cast_to<Node3D>(instantiated_scene);
  3289. if (node3d) {
  3290. Transform3D global_transform;
  3291. Node3D *parent_node3d = Object::cast_to<Node3D>(parent);
  3292. if (parent_node3d) {
  3293. global_transform = parent_node3d->get_global_gizmo_transform();
  3294. }
  3295. global_transform.origin = spatial_editor->snap_point(_get_instance_position(p_point));
  3296. global_transform.basis *= node3d->get_transform().basis;
  3297. editor_data->get_undo_redo().add_do_method(instantiated_scene, "set_global_transform", global_transform);
  3298. }
  3299. return true;
  3300. }
  3301. void Node3DEditorViewport::_perform_drop_data() {
  3302. _remove_preview();
  3303. Vector<String> error_files;
  3304. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  3305. for (int i = 0; i < selected_files.size(); i++) {
  3306. String path = selected_files[i];
  3307. Ref<Resource> res = ResourceLoader::load(path);
  3308. if (res.is_null()) {
  3309. continue;
  3310. }
  3311. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  3312. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  3313. if (mesh != nullptr || scene != nullptr) {
  3314. bool success = _create_instance(target_node, path, drop_pos);
  3315. if (!success) {
  3316. error_files.push_back(path);
  3317. }
  3318. }
  3319. }
  3320. editor_data->get_undo_redo().commit_action();
  3321. if (error_files.size() > 0) {
  3322. String files_str;
  3323. for (int i = 0; i < error_files.size(); i++) {
  3324. files_str += error_files[i].get_file().get_basename() + ",";
  3325. }
  3326. files_str = files_str.substr(0, files_str.length() - 1);
  3327. accept->set_text(vformat(TTR("Error instancing scene from %s"), files_str.get_data()));
  3328. accept->popup_centered();
  3329. }
  3330. }
  3331. bool Node3DEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  3332. bool can_instantiate = false;
  3333. if (!preview_node->is_inside_tree()) {
  3334. Dictionary d = p_data;
  3335. if (d.has("type") && (String(d["type"]) == "files")) {
  3336. Vector<String> files = d["files"];
  3337. List<String> scene_extensions;
  3338. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  3339. List<String> mesh_extensions;
  3340. ResourceLoader::get_recognized_extensions_for_type("Mesh", &mesh_extensions);
  3341. for (int i = 0; i < files.size(); i++) {
  3342. // Check if dragged files with mesh or scene extension can be created at least once.
  3343. if (mesh_extensions.find(files[i].get_extension()) || scene_extensions.find(files[i].get_extension())) {
  3344. Ref<Resource> res = ResourceLoader::load(files[i]);
  3345. if (res.is_null()) {
  3346. continue;
  3347. }
  3348. Ref<PackedScene> scn = res;
  3349. if (scn.is_valid()) {
  3350. Node *instantiated_scene = scn->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
  3351. if (!instantiated_scene) {
  3352. continue;
  3353. }
  3354. memdelete(instantiated_scene);
  3355. }
  3356. can_instantiate = true;
  3357. break;
  3358. }
  3359. }
  3360. if (can_instantiate) {
  3361. _create_preview(files);
  3362. }
  3363. }
  3364. } else {
  3365. can_instantiate = true;
  3366. }
  3367. if (can_instantiate) {
  3368. Transform3D global_transform = Transform3D(Basis(), _get_instance_position(p_point));
  3369. preview_node->set_global_transform(global_transform);
  3370. }
  3371. return can_instantiate;
  3372. }
  3373. void Node3DEditorViewport::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  3374. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  3375. return;
  3376. }
  3377. bool is_shift = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  3378. bool is_ctrl = Input::get_singleton()->is_key_pressed(Key::CTRL);
  3379. selected_files.clear();
  3380. Dictionary d = p_data;
  3381. if (d.has("type") && String(d["type"]) == "files") {
  3382. selected_files = d["files"];
  3383. }
  3384. List<Node *> selected_nodes = EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list();
  3385. Node *root_node = EditorNode::get_singleton()->get_edited_scene();
  3386. if (selected_nodes.size() == 1) {
  3387. Node *selected_node = selected_nodes[0];
  3388. target_node = root_node;
  3389. if (is_ctrl) {
  3390. target_node = selected_node;
  3391. } else if (is_shift && selected_node != root_node) {
  3392. target_node = selected_node->get_parent();
  3393. }
  3394. } else if (selected_nodes.size() == 0) {
  3395. if (root_node) {
  3396. target_node = root_node;
  3397. } else {
  3398. // Create a root node so we can add child nodes to it.
  3399. SceneTreeDock::get_singleton()->add_root_node(memnew(Node3D));
  3400. target_node = get_tree()->get_edited_scene_root();
  3401. }
  3402. } else {
  3403. accept->set_text(TTR("Cannot drag and drop into multiple selected nodes."));
  3404. accept->popup_centered();
  3405. _remove_preview();
  3406. return;
  3407. }
  3408. drop_pos = p_point;
  3409. _perform_drop_data();
  3410. }
  3411. void Node3DEditorViewport::begin_transform(TransformMode p_mode, bool instant) {
  3412. if (get_selected_count() > 0) {
  3413. _edit.mode = p_mode;
  3414. _compute_edit(_edit.mouse_pos);
  3415. _edit.instant = instant;
  3416. _edit.snap = spatial_editor->is_snap_enabled();
  3417. }
  3418. }
  3419. void Node3DEditorViewport::commit_transform() {
  3420. ERR_FAIL_COND(_edit.mode == TRANSFORM_NONE);
  3421. static const char *_transform_name[4] = {
  3422. TTRC("None"),
  3423. TTRC("Rotate"),
  3424. // TRANSLATORS: This refers to the movement that changes the position of an object.
  3425. TTRC("Translate"),
  3426. TTRC("Scale"),
  3427. };
  3428. undo_redo->create_action(_transform_name[_edit.mode]);
  3429. List<Node *> &selection = editor_selection->get_selected_node_list();
  3430. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3431. Node3D *sp = Object::cast_to<Node3D>(E->get());
  3432. if (!sp) {
  3433. continue;
  3434. }
  3435. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  3436. if (!se) {
  3437. continue;
  3438. }
  3439. undo_redo->add_do_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  3440. undo_redo->add_undo_method(sp, "set_global_transform", se->original);
  3441. }
  3442. undo_redo->commit_action();
  3443. finish_transform();
  3444. set_message("");
  3445. }
  3446. void Node3DEditorViewport::update_transform(Point2 p_mousepos, bool p_shift) {
  3447. Vector3 ray_pos = _get_ray_pos(p_mousepos);
  3448. Vector3 ray = _get_ray(p_mousepos);
  3449. double snap = EDITOR_GET("interface/inspector/default_float_step");
  3450. int snap_step_decimals = Math::range_step_decimals(snap);
  3451. switch (_edit.mode) {
  3452. case TRANSFORM_SCALE: {
  3453. Vector3 motion_mask;
  3454. Plane plane;
  3455. bool plane_mv = false;
  3456. switch (_edit.plane) {
  3457. case TRANSFORM_VIEW:
  3458. motion_mask = Vector3(0, 0, 0);
  3459. plane = Plane(_get_camera_normal(), _edit.center);
  3460. break;
  3461. case TRANSFORM_X_AXIS:
  3462. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(0).normalized();
  3463. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  3464. break;
  3465. case TRANSFORM_Y_AXIS:
  3466. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(1).normalized();
  3467. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  3468. break;
  3469. case TRANSFORM_Z_AXIS:
  3470. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized();
  3471. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  3472. break;
  3473. case TRANSFORM_YZ:
  3474. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized() + spatial_editor->get_gizmo_transform().basis.get_column(1).normalized();
  3475. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(0).normalized(), _edit.center);
  3476. plane_mv = true;
  3477. break;
  3478. case TRANSFORM_XZ:
  3479. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized() + spatial_editor->get_gizmo_transform().basis.get_column(0).normalized();
  3480. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(1).normalized(), _edit.center);
  3481. plane_mv = true;
  3482. break;
  3483. case TRANSFORM_XY:
  3484. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(0).normalized() + spatial_editor->get_gizmo_transform().basis.get_column(1).normalized();
  3485. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(2).normalized(), _edit.center);
  3486. plane_mv = true;
  3487. break;
  3488. }
  3489. Vector3 intersection;
  3490. if (!plane.intersects_ray(ray_pos, ray, &intersection)) {
  3491. break;
  3492. }
  3493. Vector3 click;
  3494. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) {
  3495. break;
  3496. }
  3497. Vector3 motion = intersection - click;
  3498. if (_edit.plane != TRANSFORM_VIEW) {
  3499. if (!plane_mv) {
  3500. motion = motion_mask.dot(motion) * motion_mask;
  3501. } else {
  3502. // Alternative planar scaling mode
  3503. if (p_shift) {
  3504. motion = motion_mask.dot(motion) * motion_mask;
  3505. }
  3506. }
  3507. } else {
  3508. const real_t center_click_dist = click.distance_to(_edit.center);
  3509. const real_t center_inters_dist = intersection.distance_to(_edit.center);
  3510. if (center_click_dist == 0) {
  3511. break;
  3512. }
  3513. const real_t scale = center_inters_dist - center_click_dist;
  3514. motion = Vector3(scale, scale, scale);
  3515. }
  3516. motion /= click.distance_to(_edit.center);
  3517. // Disable local transformation for TRANSFORM_VIEW
  3518. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  3519. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  3520. snap = spatial_editor->get_scale_snap() / 100;
  3521. }
  3522. Vector3 motion_snapped = motion;
  3523. motion_snapped.snap(Vector3(snap, snap, snap));
  3524. // This might not be necessary anymore after issue #288 is solved (in 4.0?).
  3525. // TRANSLATORS: Refers to changing the scale of a node in the 3D editor.
  3526. set_message(TTR("Scaling:") + " (" + String::num(motion_snapped.x, snap_step_decimals) + ", " +
  3527. String::num(motion_snapped.y, snap_step_decimals) + ", " + String::num(motion_snapped.z, snap_step_decimals) + ")");
  3528. motion = _edit.original.basis.inverse().xform(motion);
  3529. List<Node *> &selection = editor_selection->get_selected_node_list();
  3530. for (Node *E : selection) {
  3531. Node3D *sp = Object::cast_to<Node3D>(E);
  3532. if (!sp) {
  3533. continue;
  3534. }
  3535. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  3536. if (!se) {
  3537. continue;
  3538. }
  3539. if (sp->has_meta("_edit_lock_")) {
  3540. continue;
  3541. }
  3542. if (se->gizmo.is_valid()) {
  3543. for (KeyValue<int, Transform3D> &GE : se->subgizmos) {
  3544. Transform3D xform = GE.value;
  3545. Transform3D new_xform = _compute_transform(TRANSFORM_SCALE, se->original * xform, xform, motion, snap, local_coords, true); // Force orthogonal with subgizmo.
  3546. if (!local_coords) {
  3547. new_xform = se->original.affine_inverse() * new_xform;
  3548. }
  3549. se->gizmo->set_subgizmo_transform(GE.key, new_xform);
  3550. }
  3551. } else {
  3552. Transform3D new_xform = _compute_transform(TRANSFORM_SCALE, se->original, se->original_local, motion, snap, local_coords, sp->get_rotation_edit_mode() != Node3D::ROTATION_EDIT_MODE_BASIS);
  3553. _transform_gizmo_apply(se->sp, new_xform, local_coords);
  3554. }
  3555. }
  3556. spatial_editor->update_transform_gizmo();
  3557. surface->update();
  3558. } break;
  3559. case TRANSFORM_TRANSLATE: {
  3560. Vector3 motion_mask;
  3561. Plane plane;
  3562. bool plane_mv = false;
  3563. switch (_edit.plane) {
  3564. case TRANSFORM_VIEW:
  3565. plane = Plane(_get_camera_normal(), _edit.center);
  3566. break;
  3567. case TRANSFORM_X_AXIS:
  3568. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(0).normalized();
  3569. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  3570. break;
  3571. case TRANSFORM_Y_AXIS:
  3572. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(1).normalized();
  3573. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  3574. break;
  3575. case TRANSFORM_Z_AXIS:
  3576. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized();
  3577. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  3578. break;
  3579. case TRANSFORM_YZ:
  3580. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(0).normalized(), _edit.center);
  3581. plane_mv = true;
  3582. break;
  3583. case TRANSFORM_XZ:
  3584. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(1).normalized(), _edit.center);
  3585. plane_mv = true;
  3586. break;
  3587. case TRANSFORM_XY:
  3588. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(2).normalized(), _edit.center);
  3589. plane_mv = true;
  3590. break;
  3591. }
  3592. Vector3 intersection;
  3593. if (!plane.intersects_ray(ray_pos, ray, &intersection)) {
  3594. break;
  3595. }
  3596. Vector3 click;
  3597. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) {
  3598. break;
  3599. }
  3600. Vector3 motion = intersection - click;
  3601. if (_edit.plane != TRANSFORM_VIEW) {
  3602. if (!plane_mv) {
  3603. motion = motion_mask.dot(motion) * motion_mask;
  3604. }
  3605. }
  3606. // Disable local transformation for TRANSFORM_VIEW
  3607. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  3608. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  3609. snap = spatial_editor->get_translate_snap();
  3610. }
  3611. Vector3 motion_snapped = motion;
  3612. motion_snapped.snap(Vector3(snap, snap, snap));
  3613. // TRANSLATORS: Refers to changing the position of a node in the 3D editor.
  3614. set_message(TTR("Translating:") + " (" + String::num(motion_snapped.x, snap_step_decimals) + ", " +
  3615. String::num(motion_snapped.y, snap_step_decimals) + ", " + String::num(motion_snapped.z, snap_step_decimals) + ")");
  3616. motion = spatial_editor->get_gizmo_transform().basis.inverse().xform(motion);
  3617. List<Node *> &selection = editor_selection->get_selected_node_list();
  3618. for (Node *E : selection) {
  3619. Node3D *sp = Object::cast_to<Node3D>(E);
  3620. if (!sp) {
  3621. continue;
  3622. }
  3623. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  3624. if (!se) {
  3625. continue;
  3626. }
  3627. if (sp->has_meta("_edit_lock_")) {
  3628. continue;
  3629. }
  3630. if (se->gizmo.is_valid()) {
  3631. for (KeyValue<int, Transform3D> &GE : se->subgizmos) {
  3632. Transform3D xform = GE.value;
  3633. Transform3D new_xform = _compute_transform(TRANSFORM_TRANSLATE, se->original * xform, xform, motion, snap, local_coords, true); // Force orthogonal with subgizmo.
  3634. new_xform = se->original.affine_inverse() * new_xform;
  3635. se->gizmo->set_subgizmo_transform(GE.key, new_xform);
  3636. }
  3637. } else {
  3638. Transform3D new_xform = _compute_transform(TRANSFORM_TRANSLATE, se->original, se->original_local, motion, snap, local_coords, sp->get_rotation_edit_mode() != Node3D::ROTATION_EDIT_MODE_BASIS);
  3639. _transform_gizmo_apply(se->sp, new_xform, false);
  3640. }
  3641. }
  3642. spatial_editor->update_transform_gizmo();
  3643. surface->update();
  3644. } break;
  3645. case TRANSFORM_ROTATE: {
  3646. Plane plane = Plane(_get_camera_normal(), _edit.center);
  3647. Vector3 local_axis;
  3648. Vector3 global_axis;
  3649. switch (_edit.plane) {
  3650. case TRANSFORM_VIEW:
  3651. // local_axis unused
  3652. global_axis = _get_camera_normal();
  3653. break;
  3654. case TRANSFORM_X_AXIS:
  3655. local_axis = Vector3(1, 0, 0);
  3656. break;
  3657. case TRANSFORM_Y_AXIS:
  3658. local_axis = Vector3(0, 1, 0);
  3659. break;
  3660. case TRANSFORM_Z_AXIS:
  3661. local_axis = Vector3(0, 0, 1);
  3662. break;
  3663. case TRANSFORM_YZ:
  3664. case TRANSFORM_XZ:
  3665. case TRANSFORM_XY:
  3666. break;
  3667. }
  3668. if (_edit.plane != TRANSFORM_VIEW) {
  3669. global_axis = spatial_editor->get_gizmo_transform().basis.xform(local_axis).normalized();
  3670. }
  3671. Vector3 intersection;
  3672. if (!plane.intersects_ray(ray_pos, ray, &intersection)) {
  3673. break;
  3674. }
  3675. Vector3 click;
  3676. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) {
  3677. break;
  3678. }
  3679. static const float orthogonal_threshold = Math::cos(Math::deg2rad(87.0f));
  3680. bool axis_is_orthogonal = ABS(plane.normal.dot(global_axis)) < orthogonal_threshold;
  3681. double angle = 0.0f;
  3682. if (axis_is_orthogonal) {
  3683. _edit.show_rotation_line = false;
  3684. Vector3 projection_axis = plane.normal.cross(global_axis);
  3685. Vector3 delta = intersection - click;
  3686. float projection = delta.dot(projection_axis);
  3687. angle = (projection * (Math_PI / 2.0f)) / (gizmo_scale * GIZMO_CIRCLE_SIZE);
  3688. } else {
  3689. _edit.show_rotation_line = true;
  3690. Vector3 click_axis = (click - _edit.center).normalized();
  3691. Vector3 current_axis = (intersection - _edit.center).normalized();
  3692. angle = click_axis.signed_angle_to(current_axis, global_axis);
  3693. }
  3694. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  3695. snap = spatial_editor->get_rotate_snap();
  3696. }
  3697. angle = Math::rad2deg(angle) + snap * 0.5; //else it won't reach +180
  3698. angle -= Math::fmod(angle, snap);
  3699. set_message(vformat(TTR("Rotating %s degrees."), String::num(angle, snap_step_decimals)));
  3700. angle = Math::deg2rad(angle);
  3701. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); // Disable local transformation for TRANSFORM_VIEW
  3702. List<Node *> &selection = editor_selection->get_selected_node_list();
  3703. for (Node *E : selection) {
  3704. Node3D *sp = Object::cast_to<Node3D>(E);
  3705. if (!sp) {
  3706. continue;
  3707. }
  3708. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  3709. if (!se) {
  3710. continue;
  3711. }
  3712. if (sp->has_meta("_edit_lock_")) {
  3713. continue;
  3714. }
  3715. Vector3 compute_axis = local_coords ? local_axis : global_axis;
  3716. if (se->gizmo.is_valid()) {
  3717. for (KeyValue<int, Transform3D> &GE : se->subgizmos) {
  3718. Transform3D xform = GE.value;
  3719. Transform3D new_xform = _compute_transform(TRANSFORM_ROTATE, se->original * xform, xform, compute_axis, angle, local_coords, true); // Force orthogonal with subgizmo.
  3720. if (!local_coords) {
  3721. new_xform = se->original.affine_inverse() * new_xform;
  3722. }
  3723. se->gizmo->set_subgizmo_transform(GE.key, new_xform);
  3724. }
  3725. } else {
  3726. Transform3D new_xform = _compute_transform(TRANSFORM_ROTATE, se->original, se->original_local, compute_axis, angle, local_coords, sp->get_rotation_edit_mode() != Node3D::ROTATION_EDIT_MODE_BASIS);
  3727. _transform_gizmo_apply(se->sp, new_xform, local_coords);
  3728. }
  3729. }
  3730. spatial_editor->update_transform_gizmo();
  3731. surface->update();
  3732. } break;
  3733. default: {
  3734. }
  3735. }
  3736. }
  3737. void Node3DEditorViewport::finish_transform() {
  3738. spatial_editor->set_local_coords_enabled(_edit.original_local);
  3739. spatial_editor->update_transform_gizmo();
  3740. _edit.mode = TRANSFORM_NONE;
  3741. _edit.instant = false;
  3742. surface->update();
  3743. }
  3744. // Register a shortcut and also add it as an input action with the same events.
  3745. void Node3DEditorViewport::register_shortcut_action(const String &p_path, const String &p_name, Key p_keycode) {
  3746. Ref<Shortcut> sc = ED_SHORTCUT(p_path, p_name, p_keycode);
  3747. shortcut_changed_callback(sc, p_path);
  3748. // Connect to the change event on the shortcut so the input binding can be updated.
  3749. sc->connect("changed", callable_mp(this, &Node3DEditorViewport::shortcut_changed_callback), varray(sc, p_path));
  3750. }
  3751. // Update the action in the InputMap to the provided shortcut events.
  3752. void Node3DEditorViewport::shortcut_changed_callback(const Ref<Shortcut> p_shortcut, const String &p_shortcut_path) {
  3753. InputMap *im = InputMap::get_singleton();
  3754. if (im->has_action(p_shortcut_path)) {
  3755. im->action_erase_events(p_shortcut_path);
  3756. } else {
  3757. im->add_action(p_shortcut_path);
  3758. }
  3759. for (int i = 0; i < p_shortcut->get_events().size(); i++) {
  3760. im->action_add_event(p_shortcut_path, p_shortcut->get_events()[i]);
  3761. }
  3762. }
  3763. Node3DEditorViewport::Node3DEditorViewport(Node3DEditor *p_spatial_editor, int p_index) {
  3764. cpu_time_history_index = 0;
  3765. gpu_time_history_index = 0;
  3766. _edit.mode = TRANSFORM_NONE;
  3767. _edit.plane = TRANSFORM_VIEW;
  3768. _edit.snap = true;
  3769. _edit.show_rotation_line = true;
  3770. _edit.instant = false;
  3771. _edit.gizmo_handle = -1;
  3772. _edit.gizmo_handle_secondary = false;
  3773. index = p_index;
  3774. editor_data = SceneTreeDock::get_singleton()->get_editor_data();
  3775. editor_selection = EditorNode::get_singleton()->get_editor_selection();
  3776. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  3777. orthogonal = false;
  3778. auto_orthogonal = false;
  3779. lock_rotation = false;
  3780. message_time = 0;
  3781. zoom_indicator_delay = 0.0;
  3782. spatial_editor = p_spatial_editor;
  3783. SubViewportContainer *c = memnew(SubViewportContainer);
  3784. subviewport_container = c;
  3785. c->set_stretch(true);
  3786. add_child(c);
  3787. c->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  3788. viewport = memnew(SubViewport);
  3789. viewport->set_disable_input(true);
  3790. c->add_child(viewport);
  3791. surface = memnew(Control);
  3792. surface->set_drag_forwarding(this);
  3793. add_child(surface);
  3794. surface->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  3795. surface->set_clip_contents(true);
  3796. camera = memnew(Camera3D);
  3797. camera->set_disable_gizmos(true);
  3798. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + p_index)) | (1 << GIZMO_EDIT_LAYER) | (1 << GIZMO_GRID_LAYER) | (1 << MISC_TOOL_LAYER));
  3799. viewport->add_child(camera);
  3800. camera->make_current();
  3801. surface->set_focus_mode(FOCUS_ALL);
  3802. VBoxContainer *vbox = memnew(VBoxContainer);
  3803. surface->add_child(vbox);
  3804. vbox->set_offset(SIDE_LEFT, 10 * EDSCALE);
  3805. vbox->set_offset(SIDE_TOP, 10 * EDSCALE);
  3806. view_menu = memnew(MenuButton);
  3807. view_menu->set_flat(false);
  3808. view_menu->set_h_size_flags(0);
  3809. view_menu->set_shortcut_context(this);
  3810. vbox->add_child(view_menu);
  3811. display_submenu = memnew(PopupMenu);
  3812. view_menu->get_popup()->add_child(display_submenu);
  3813. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/top_view"), VIEW_TOP);
  3814. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/bottom_view"), VIEW_BOTTOM);
  3815. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/left_view"), VIEW_LEFT);
  3816. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/right_view"), VIEW_RIGHT);
  3817. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/front_view"), VIEW_FRONT);
  3818. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/rear_view"), VIEW_REAR);
  3819. view_menu->get_popup()->add_separator();
  3820. view_menu->get_popup()->add_radio_check_item(TTR("Perspective") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_PERSPECTIVE);
  3821. view_menu->get_popup()->add_radio_check_item(TTR("Orthogonal") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_ORTHOGONAL);
  3822. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  3823. view_menu->get_popup()->add_check_item(TTR("Auto Orthogonal Enabled"), VIEW_AUTO_ORTHOGONAL);
  3824. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL), true);
  3825. view_menu->get_popup()->add_separator();
  3826. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_lock_rotation", TTR("Lock View Rotation")), VIEW_LOCK_ROTATION);
  3827. view_menu->get_popup()->add_separator();
  3828. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_normal", TTR("Display Normal")), VIEW_DISPLAY_NORMAL);
  3829. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_wireframe", TTR("Display Wireframe")), VIEW_DISPLAY_WIREFRAME);
  3830. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_overdraw", TTR("Display Overdraw")), VIEW_DISPLAY_OVERDRAW);
  3831. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_lighting", TTR("Display Lighting")), VIEW_DISPLAY_LIGHTING);
  3832. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_unshaded", TTR("Display Unshaded")), VIEW_DISPLAY_SHADELESS);
  3833. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true);
  3834. display_submenu->add_radio_check_item(TTR("Directional Shadow Splits"), VIEW_DISPLAY_DEBUG_PSSM_SPLITS);
  3835. display_submenu->add_separator();
  3836. display_submenu->add_radio_check_item(TTR("Normal Buffer"), VIEW_DISPLAY_NORMAL_BUFFER);
  3837. display_submenu->add_separator();
  3838. display_submenu->add_radio_check_item(TTR("Shadow Atlas"), VIEW_DISPLAY_DEBUG_SHADOW_ATLAS);
  3839. display_submenu->add_radio_check_item(TTR("Directional Shadow Map"), VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS);
  3840. display_submenu->add_separator();
  3841. display_submenu->add_radio_check_item(TTR("Decal Atlas"), VIEW_DISPLAY_DEBUG_DECAL_ATLAS);
  3842. display_submenu->add_separator();
  3843. display_submenu->add_radio_check_item(TTR("VoxelGI Lighting"), VIEW_DISPLAY_DEBUG_VOXEL_GI_LIGHTING);
  3844. display_submenu->add_radio_check_item(TTR("VoxelGI Albedo"), VIEW_DISPLAY_DEBUG_VOXEL_GI_ALBEDO);
  3845. display_submenu->add_radio_check_item(TTR("VoxelGI Emission"), VIEW_DISPLAY_DEBUG_VOXEL_GI_EMISSION);
  3846. display_submenu->add_separator();
  3847. display_submenu->add_radio_check_item(TTR("SDFGI Cascades"), VIEW_DISPLAY_DEBUG_SDFGI);
  3848. display_submenu->add_radio_check_item(TTR("SDFGI Probes"), VIEW_DISPLAY_DEBUG_SDFGI_PROBES);
  3849. display_submenu->add_separator();
  3850. display_submenu->add_radio_check_item(TTR("Scene Luminance"), VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE);
  3851. display_submenu->add_separator();
  3852. display_submenu->add_radio_check_item(TTR("SSAO"), VIEW_DISPLAY_DEBUG_SSAO);
  3853. display_submenu->add_radio_check_item(TTR("SSIL"), VIEW_DISPLAY_DEBUG_SSIL);
  3854. display_submenu->add_separator();
  3855. display_submenu->add_radio_check_item(TTR("VoxelGI/SDFGI Buffer"), VIEW_DISPLAY_DEBUG_GI_BUFFER);
  3856. display_submenu->add_separator();
  3857. display_submenu->add_radio_check_item(TTR("Disable Mesh LOD"), VIEW_DISPLAY_DEBUG_DISABLE_LOD);
  3858. display_submenu->add_separator();
  3859. display_submenu->add_radio_check_item(TTR("OmniLight3D Cluster"), VIEW_DISPLAY_DEBUG_CLUSTER_OMNI_LIGHTS);
  3860. display_submenu->add_radio_check_item(TTR("SpotLight3D Cluster"), VIEW_DISPLAY_DEBUG_CLUSTER_SPOT_LIGHTS);
  3861. display_submenu->add_radio_check_item(TTR("Decal Cluster"), VIEW_DISPLAY_DEBUG_CLUSTER_DECALS);
  3862. display_submenu->add_radio_check_item(TTR("ReflectionProbe Cluster"), VIEW_DISPLAY_DEBUG_CLUSTER_REFLECTION_PROBES);
  3863. display_submenu->add_radio_check_item(TTR("Occlusion Culling Buffer"), VIEW_DISPLAY_DEBUG_OCCLUDERS);
  3864. display_submenu->add_radio_check_item(TTR("Motion Vectors"), VIEW_DISPLAY_MOTION_VECTORS);
  3865. display_submenu->set_name("display_advanced");
  3866. view_menu->get_popup()->add_submenu_item(TTR("Display Advanced..."), "display_advanced", VIEW_DISPLAY_ADVANCED);
  3867. view_menu->get_popup()->add_separator();
  3868. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment", TTR("View Environment")), VIEW_ENVIRONMENT);
  3869. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos", TTR("View Gizmos")), VIEW_GIZMOS);
  3870. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_information", TTR("View Information")), VIEW_INFORMATION);
  3871. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_fps", TTR("View Frame Time")), VIEW_FRAME_TIME);
  3872. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT), true);
  3873. view_menu->get_popup()->add_separator();
  3874. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_half_resolution", TTR("Half Resolution")), VIEW_HALF_RESOLUTION);
  3875. view_menu->get_popup()->add_separator();
  3876. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_listener", TTR("Audio Listener")), VIEW_AUDIO_LISTENER);
  3877. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_doppler", TTR("Enable Doppler")), VIEW_AUDIO_DOPPLER);
  3878. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS), true);
  3879. view_menu->get_popup()->add_separator();
  3880. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_cinematic_preview", TTR("Cinematic Preview")), VIEW_CINEMATIC_PREVIEW);
  3881. view_menu->get_popup()->add_separator();
  3882. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_origin"), VIEW_CENTER_TO_ORIGIN);
  3883. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_selection"), VIEW_CENTER_TO_SELECTION);
  3884. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_transform_with_view"), VIEW_ALIGN_TRANSFORM_WITH_VIEW);
  3885. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_rotation_with_view"), VIEW_ALIGN_ROTATION_WITH_VIEW);
  3886. view_menu->get_popup()->connect("id_pressed", callable_mp(this, &Node3DEditorViewport::_menu_option));
  3887. display_submenu->connect("id_pressed", callable_mp(this, &Node3DEditorViewport::_menu_option));
  3888. view_menu->set_disable_shortcuts(true);
  3889. #ifndef _MSC_VER
  3890. #warning this needs to be fixed
  3891. #endif
  3892. //if (OS::get_singleton()->get_current_video_driver() == OS::VIDEO_DRIVER_GLES2) {
  3893. if (false) {
  3894. // Alternate display modes only work when using the Vulkan renderer; make this explicit.
  3895. const int normal_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL);
  3896. const int wireframe_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME);
  3897. const int overdraw_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW);
  3898. const int shadeless_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS);
  3899. const String unsupported_tooltip = TTR("Not available when using the OpenGL renderer.");
  3900. view_menu->get_popup()->set_item_disabled(normal_idx, true);
  3901. view_menu->get_popup()->set_item_tooltip(normal_idx, unsupported_tooltip);
  3902. view_menu->get_popup()->set_item_disabled(wireframe_idx, true);
  3903. view_menu->get_popup()->set_item_tooltip(wireframe_idx, unsupported_tooltip);
  3904. view_menu->get_popup()->set_item_disabled(overdraw_idx, true);
  3905. view_menu->get_popup()->set_item_tooltip(overdraw_idx, unsupported_tooltip);
  3906. view_menu->get_popup()->set_item_disabled(shadeless_idx, true);
  3907. view_menu->get_popup()->set_item_tooltip(shadeless_idx, unsupported_tooltip);
  3908. }
  3909. register_shortcut_action("spatial_editor/freelook_left", TTR("Freelook Left"), Key::A);
  3910. register_shortcut_action("spatial_editor/freelook_right", TTR("Freelook Right"), Key::D);
  3911. register_shortcut_action("spatial_editor/freelook_forward", TTR("Freelook Forward"), Key::W);
  3912. register_shortcut_action("spatial_editor/freelook_backwards", TTR("Freelook Backwards"), Key::S);
  3913. register_shortcut_action("spatial_editor/freelook_up", TTR("Freelook Up"), Key::E);
  3914. register_shortcut_action("spatial_editor/freelook_down", TTR("Freelook Down"), Key::Q);
  3915. register_shortcut_action("spatial_editor/freelook_speed_modifier", TTR("Freelook Speed Modifier"), Key::SHIFT);
  3916. register_shortcut_action("spatial_editor/freelook_slow_modifier", TTR("Freelook Slow Modifier"), Key::ALT);
  3917. ED_SHORTCUT("spatial_editor/lock_transform_x", TTR("Lock Transformation to X axis"), Key::X);
  3918. ED_SHORTCUT("spatial_editor/lock_transform_y", TTR("Lock Transformation to Y axis"), Key::Y);
  3919. ED_SHORTCUT("spatial_editor/lock_transform_z", TTR("Lock Transformation to Z axis"), Key::Z);
  3920. ED_SHORTCUT("spatial_editor/lock_transform_yz", TTR("Lock Transformation to YZ plane"), KeyModifierMask::SHIFT | Key::X);
  3921. ED_SHORTCUT("spatial_editor/lock_transform_xz", TTR("Lock Transformation to XZ plane"), KeyModifierMask::SHIFT | Key::Y);
  3922. ED_SHORTCUT("spatial_editor/lock_transform_xy", TTR("Lock Transformation to XY plane"), KeyModifierMask::SHIFT | Key::Z);
  3923. ED_SHORTCUT("spatial_editor/cancel_transform", TTR("Cancel Transformation"), Key::ESCAPE);
  3924. ED_SHORTCUT("spatial_editor/instant_translate", TTR("Begin Translate Transformation"));
  3925. ED_SHORTCUT("spatial_editor/instant_rotate", TTR("Begin Rotate Transformation"));
  3926. ED_SHORTCUT("spatial_editor/instant_scale", TTR("Begin Scale Transformation"));
  3927. preview_camera = memnew(CheckBox);
  3928. preview_camera->set_text(TTR("Preview"));
  3929. preview_camera->set_shortcut(ED_SHORTCUT("spatial_editor/toggle_camera_preview", TTR("Toggle Camera Preview"), KeyModifierMask::CMD | Key::P));
  3930. vbox->add_child(preview_camera);
  3931. preview_camera->set_h_size_flags(0);
  3932. preview_camera->hide();
  3933. preview_camera->connect("toggled", callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview));
  3934. previewing = nullptr;
  3935. gizmo_scale = 1.0;
  3936. preview_node = nullptr;
  3937. info_label = memnew(Label);
  3938. info_label->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -90 * EDSCALE);
  3939. info_label->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -90 * EDSCALE);
  3940. info_label->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -10 * EDSCALE);
  3941. info_label->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  3942. info_label->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  3943. info_label->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  3944. surface->add_child(info_label);
  3945. info_label->hide();
  3946. cinema_label = memnew(Label);
  3947. cinema_label->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  3948. cinema_label->set_h_grow_direction(GROW_DIRECTION_END);
  3949. cinema_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  3950. surface->add_child(cinema_label);
  3951. cinema_label->set_text(TTR("Cinematic Preview"));
  3952. cinema_label->hide();
  3953. previewing_cinema = false;
  3954. locked_label = memnew(Label);
  3955. locked_label->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -20 * EDSCALE);
  3956. locked_label->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  3957. locked_label->set_h_grow_direction(GROW_DIRECTION_END);
  3958. locked_label->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  3959. locked_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  3960. surface->add_child(locked_label);
  3961. locked_label->set_text(TTR("View Rotation Locked"));
  3962. locked_label->hide();
  3963. zoom_limit_label = memnew(Label);
  3964. zoom_limit_label->set_anchors_and_offsets_preset(LayoutPreset::PRESET_BOTTOM_LEFT);
  3965. zoom_limit_label->set_offset(Side::SIDE_TOP, -28 * EDSCALE);
  3966. zoom_limit_label->set_text(TTR("To zoom further, change the camera's clipping planes (View -> Settings...)"));
  3967. zoom_limit_label->set_name("ZoomLimitMessageLabel");
  3968. zoom_limit_label->add_theme_color_override("font_color", Color(1, 1, 1, 1));
  3969. zoom_limit_label->hide();
  3970. surface->add_child(zoom_limit_label);
  3971. frame_time_gradient = memnew(Gradient);
  3972. // The color is set when the theme changes.
  3973. frame_time_gradient->add_point(0.5, Color());
  3974. top_right_vbox = memnew(VBoxContainer);
  3975. top_right_vbox->set_anchors_and_offsets_preset(PRESET_TOP_RIGHT, PRESET_MODE_MINSIZE, 2.0 * EDSCALE);
  3976. top_right_vbox->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  3977. // Make sure frame time labels don't touch the viewport's edge.
  3978. top_right_vbox->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  3979. // Prevent visible spacing between frame time labels.
  3980. top_right_vbox->add_theme_constant_override("separation", 0);
  3981. rotation_control = memnew(ViewportRotationControl);
  3982. rotation_control->set_custom_minimum_size(Size2(80, 80) * EDSCALE);
  3983. rotation_control->set_h_size_flags(SIZE_SHRINK_END);
  3984. rotation_control->set_viewport(this);
  3985. top_right_vbox->add_child(rotation_control);
  3986. // Individual Labels are used to allow coloring each label with its own color.
  3987. cpu_time_label = memnew(Label);
  3988. top_right_vbox->add_child(cpu_time_label);
  3989. cpu_time_label->hide();
  3990. gpu_time_label = memnew(Label);
  3991. top_right_vbox->add_child(gpu_time_label);
  3992. gpu_time_label->hide();
  3993. fps_label = memnew(Label);
  3994. top_right_vbox->add_child(fps_label);
  3995. fps_label->hide();
  3996. surface->add_child(top_right_vbox);
  3997. accept = nullptr;
  3998. freelook_active = false;
  3999. freelook_speed = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_base_speed");
  4000. selection_menu = memnew(PopupMenu);
  4001. add_child(selection_menu);
  4002. selection_menu->set_min_size(Size2(100, 0) * EDSCALE);
  4003. selection_menu->connect("id_pressed", callable_mp(this, &Node3DEditorViewport::_selection_result_pressed));
  4004. selection_menu->connect("popup_hide", callable_mp(this, &Node3DEditorViewport::_selection_menu_hide));
  4005. if (p_index == 0) {
  4006. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER), true);
  4007. viewport->set_as_audio_listener_3d(true);
  4008. }
  4009. view_type = VIEW_TYPE_USER;
  4010. _update_name();
  4011. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Node3DEditorViewport::update_transform_gizmo_view));
  4012. }
  4013. Node3DEditorViewport::~Node3DEditorViewport() {
  4014. memdelete(frame_time_gradient);
  4015. }
  4016. //////////////////////////////////////////////////////////////
  4017. void Node3DEditorViewportContainer::gui_input(const Ref<InputEvent> &p_event) {
  4018. ERR_FAIL_COND(p_event.is_null());
  4019. Ref<InputEventMouseButton> mb = p_event;
  4020. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  4021. if (mb->is_pressed()) {
  4022. Vector2 size = get_size();
  4023. int h_sep = get_theme_constant(SNAME("separation"), SNAME("HSplitContainer"));
  4024. int v_sep = get_theme_constant(SNAME("separation"), SNAME("VSplitContainer"));
  4025. int mid_w = size.width * ratio_h;
  4026. int mid_h = size.height * ratio_v;
  4027. dragging_h = mb->get_position().x > (mid_w - h_sep / 2) && mb->get_position().x < (mid_w + h_sep / 2);
  4028. dragging_v = mb->get_position().y > (mid_h - v_sep / 2) && mb->get_position().y < (mid_h + v_sep / 2);
  4029. drag_begin_pos = mb->get_position();
  4030. drag_begin_ratio.x = ratio_h;
  4031. drag_begin_ratio.y = ratio_v;
  4032. switch (view) {
  4033. case VIEW_USE_1_VIEWPORT: {
  4034. dragging_h = false;
  4035. dragging_v = false;
  4036. } break;
  4037. case VIEW_USE_2_VIEWPORTS: {
  4038. dragging_h = false;
  4039. } break;
  4040. case VIEW_USE_2_VIEWPORTS_ALT: {
  4041. dragging_v = false;
  4042. } break;
  4043. case VIEW_USE_3_VIEWPORTS:
  4044. case VIEW_USE_3_VIEWPORTS_ALT:
  4045. case VIEW_USE_4_VIEWPORTS: {
  4046. // Do nothing.
  4047. } break;
  4048. }
  4049. } else {
  4050. dragging_h = false;
  4051. dragging_v = false;
  4052. }
  4053. }
  4054. Ref<InputEventMouseMotion> mm = p_event;
  4055. if (mm.is_valid()) {
  4056. if (view == VIEW_USE_3_VIEWPORTS || view == VIEW_USE_3_VIEWPORTS_ALT || view == VIEW_USE_4_VIEWPORTS) {
  4057. Vector2 size = get_size();
  4058. int h_sep = get_theme_constant(SNAME("separation"), SNAME("HSplitContainer"));
  4059. int v_sep = get_theme_constant(SNAME("separation"), SNAME("VSplitContainer"));
  4060. int mid_w = size.width * ratio_h;
  4061. int mid_h = size.height * ratio_v;
  4062. bool was_hovering_h = hovering_h;
  4063. bool was_hovering_v = hovering_v;
  4064. hovering_h = mm->get_position().x > (mid_w - h_sep / 2) && mm->get_position().x < (mid_w + h_sep / 2);
  4065. hovering_v = mm->get_position().y > (mid_h - v_sep / 2) && mm->get_position().y < (mid_h + v_sep / 2);
  4066. if (was_hovering_h != hovering_h || was_hovering_v != hovering_v) {
  4067. update();
  4068. }
  4069. }
  4070. if (dragging_h) {
  4071. real_t new_ratio = drag_begin_ratio.x + (mm->get_position().x - drag_begin_pos.x) / get_size().width;
  4072. new_ratio = CLAMP(new_ratio, 40 / get_size().width, (get_size().width - 40) / get_size().width);
  4073. ratio_h = new_ratio;
  4074. queue_sort();
  4075. update();
  4076. }
  4077. if (dragging_v) {
  4078. real_t new_ratio = drag_begin_ratio.y + (mm->get_position().y - drag_begin_pos.y) / get_size().height;
  4079. new_ratio = CLAMP(new_ratio, 40 / get_size().height, (get_size().height - 40) / get_size().height);
  4080. ratio_v = new_ratio;
  4081. queue_sort();
  4082. update();
  4083. }
  4084. }
  4085. }
  4086. void Node3DEditorViewportContainer::_notification(int p_what) {
  4087. switch (p_what) {
  4088. case NOTIFICATION_MOUSE_ENTER:
  4089. case NOTIFICATION_MOUSE_EXIT: {
  4090. mouseover = (p_what == NOTIFICATION_MOUSE_ENTER);
  4091. update();
  4092. } break;
  4093. case NOTIFICATION_DRAW: {
  4094. if (mouseover) {
  4095. Ref<Texture2D> h_grabber = get_theme_icon(SNAME("grabber"), SNAME("HSplitContainer"));
  4096. Ref<Texture2D> v_grabber = get_theme_icon(SNAME("grabber"), SNAME("VSplitContainer"));
  4097. Ref<Texture2D> hdiag_grabber = get_theme_icon(SNAME("GuiViewportHdiagsplitter"), SNAME("EditorIcons"));
  4098. Ref<Texture2D> vdiag_grabber = get_theme_icon(SNAME("GuiViewportVdiagsplitter"), SNAME("EditorIcons"));
  4099. Ref<Texture2D> vh_grabber = get_theme_icon(SNAME("GuiViewportVhsplitter"), SNAME("EditorIcons"));
  4100. Vector2 size = get_size();
  4101. int h_sep = get_theme_constant(SNAME("separation"), SNAME("HSplitContainer"));
  4102. int v_sep = get_theme_constant(SNAME("separation"), SNAME("VSplitContainer"));
  4103. int mid_w = size.width * ratio_h;
  4104. int mid_h = size.height * ratio_v;
  4105. int size_left = mid_w - h_sep / 2;
  4106. int size_bottom = size.height - mid_h - v_sep / 2;
  4107. switch (view) {
  4108. case VIEW_USE_1_VIEWPORT: {
  4109. // Nothing to show.
  4110. } break;
  4111. case VIEW_USE_2_VIEWPORTS: {
  4112. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  4113. set_default_cursor_shape(CURSOR_VSPLIT);
  4114. } break;
  4115. case VIEW_USE_2_VIEWPORTS_ALT: {
  4116. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  4117. set_default_cursor_shape(CURSOR_HSPLIT);
  4118. } break;
  4119. case VIEW_USE_3_VIEWPORTS: {
  4120. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  4121. draw_texture(hdiag_grabber, Vector2(mid_w - hdiag_grabber->get_width() / 2, mid_h - v_grabber->get_height() / 4));
  4122. set_default_cursor_shape(CURSOR_DRAG);
  4123. } else if ((hovering_v && !dragging_h) || dragging_v) {
  4124. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  4125. set_default_cursor_shape(CURSOR_VSPLIT);
  4126. } else if (hovering_h || dragging_h) {
  4127. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, mid_h + v_grabber->get_height() / 2 + (size_bottom - h_grabber->get_height()) / 2));
  4128. set_default_cursor_shape(CURSOR_HSPLIT);
  4129. }
  4130. } break;
  4131. case VIEW_USE_3_VIEWPORTS_ALT: {
  4132. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  4133. draw_texture(vdiag_grabber, Vector2(mid_w - vdiag_grabber->get_width() + v_grabber->get_height() / 4, mid_h - vdiag_grabber->get_height() / 2));
  4134. set_default_cursor_shape(CURSOR_DRAG);
  4135. } else if ((hovering_v && !dragging_h) || dragging_v) {
  4136. draw_texture(v_grabber, Vector2((size_left - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  4137. set_default_cursor_shape(CURSOR_VSPLIT);
  4138. } else if (hovering_h || dragging_h) {
  4139. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  4140. set_default_cursor_shape(CURSOR_HSPLIT);
  4141. }
  4142. } break;
  4143. case VIEW_USE_4_VIEWPORTS: {
  4144. Vector2 half(mid_w, mid_h);
  4145. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  4146. draw_texture(vh_grabber, half - vh_grabber->get_size() / 2.0);
  4147. set_default_cursor_shape(CURSOR_DRAG);
  4148. } else if ((hovering_v && !dragging_h) || dragging_v) {
  4149. draw_texture(v_grabber, half - v_grabber->get_size() / 2.0);
  4150. set_default_cursor_shape(CURSOR_VSPLIT);
  4151. } else if (hovering_h || dragging_h) {
  4152. draw_texture(h_grabber, half - h_grabber->get_size() / 2.0);
  4153. set_default_cursor_shape(CURSOR_HSPLIT);
  4154. }
  4155. } break;
  4156. }
  4157. }
  4158. } break;
  4159. case NOTIFICATION_SORT_CHILDREN: {
  4160. Node3DEditorViewport *viewports[4];
  4161. int vc = 0;
  4162. for (int i = 0; i < get_child_count(); i++) {
  4163. viewports[vc] = Object::cast_to<Node3DEditorViewport>(get_child(i));
  4164. if (viewports[vc]) {
  4165. vc++;
  4166. }
  4167. }
  4168. ERR_FAIL_COND(vc != 4);
  4169. Size2 size = get_size();
  4170. if (size.x < 10 || size.y < 10) {
  4171. for (int i = 0; i < 4; i++) {
  4172. viewports[i]->hide();
  4173. }
  4174. return;
  4175. }
  4176. int h_sep = get_theme_constant(SNAME("separation"), SNAME("HSplitContainer"));
  4177. int v_sep = get_theme_constant(SNAME("separation"), SNAME("VSplitContainer"));
  4178. int mid_w = size.width * ratio_h;
  4179. int mid_h = size.height * ratio_v;
  4180. int size_left = mid_w - h_sep / 2;
  4181. int size_right = size.width - mid_w - h_sep / 2;
  4182. int size_top = mid_h - v_sep / 2;
  4183. int size_bottom = size.height - mid_h - v_sep / 2;
  4184. switch (view) {
  4185. case VIEW_USE_1_VIEWPORT: {
  4186. viewports[0]->show();
  4187. for (int i = 1; i < 4; i++) {
  4188. viewports[i]->hide();
  4189. }
  4190. fit_child_in_rect(viewports[0], Rect2(Vector2(), size));
  4191. } break;
  4192. case VIEW_USE_2_VIEWPORTS: {
  4193. for (int i = 0; i < 4; i++) {
  4194. if (i == 1 || i == 3) {
  4195. viewports[i]->hide();
  4196. } else {
  4197. viewports[i]->show();
  4198. }
  4199. }
  4200. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  4201. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size.width, size_bottom)));
  4202. } break;
  4203. case VIEW_USE_2_VIEWPORTS_ALT: {
  4204. for (int i = 0; i < 4; i++) {
  4205. if (i == 1 || i == 3) {
  4206. viewports[i]->hide();
  4207. } else {
  4208. viewports[i]->show();
  4209. }
  4210. }
  4211. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size.height)));
  4212. fit_child_in_rect(viewports[2], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  4213. } break;
  4214. case VIEW_USE_3_VIEWPORTS: {
  4215. for (int i = 0; i < 4; i++) {
  4216. if (i == 1) {
  4217. viewports[i]->hide();
  4218. } else {
  4219. viewports[i]->show();
  4220. }
  4221. }
  4222. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  4223. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  4224. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  4225. } break;
  4226. case VIEW_USE_3_VIEWPORTS_ALT: {
  4227. for (int i = 0; i < 4; i++) {
  4228. if (i == 1) {
  4229. viewports[i]->hide();
  4230. } else {
  4231. viewports[i]->show();
  4232. }
  4233. }
  4234. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  4235. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  4236. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  4237. } break;
  4238. case VIEW_USE_4_VIEWPORTS: {
  4239. for (int i = 0; i < 4; i++) {
  4240. viewports[i]->show();
  4241. }
  4242. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  4243. fit_child_in_rect(viewports[1], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size_top)));
  4244. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  4245. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  4246. } break;
  4247. }
  4248. } break;
  4249. }
  4250. }
  4251. void Node3DEditorViewportContainer::set_view(View p_view) {
  4252. view = p_view;
  4253. queue_sort();
  4254. }
  4255. Node3DEditorViewportContainer::View Node3DEditorViewportContainer::get_view() {
  4256. return view;
  4257. }
  4258. Node3DEditorViewportContainer::Node3DEditorViewportContainer() {
  4259. set_clip_contents(true);
  4260. view = VIEW_USE_1_VIEWPORT;
  4261. mouseover = false;
  4262. ratio_h = 0.5;
  4263. ratio_v = 0.5;
  4264. hovering_v = false;
  4265. hovering_h = false;
  4266. dragging_v = false;
  4267. dragging_h = false;
  4268. }
  4269. ///////////////////////////////////////////////////////////////////
  4270. Node3DEditor *Node3DEditor::singleton = nullptr;
  4271. Node3DEditorSelectedItem::~Node3DEditorSelectedItem() {
  4272. if (sbox_instance.is_valid()) {
  4273. RenderingServer::get_singleton()->free(sbox_instance);
  4274. }
  4275. if (sbox_instance_offset.is_valid()) {
  4276. RenderingServer::get_singleton()->free(sbox_instance_offset);
  4277. }
  4278. if (sbox_instance_xray.is_valid()) {
  4279. RenderingServer::get_singleton()->free(sbox_instance_xray);
  4280. }
  4281. if (sbox_instance_xray_offset.is_valid()) {
  4282. RenderingServer::get_singleton()->free(sbox_instance_xray_offset);
  4283. }
  4284. }
  4285. void Node3DEditor::select_gizmo_highlight_axis(int p_axis) {
  4286. for (int i = 0; i < 3; i++) {
  4287. move_gizmo[i]->surface_set_material(0, i == p_axis ? gizmo_color_hl[i] : gizmo_color[i]);
  4288. move_plane_gizmo[i]->surface_set_material(0, (i + 6) == p_axis ? plane_gizmo_color_hl[i] : plane_gizmo_color[i]);
  4289. rotate_gizmo[i]->surface_set_material(0, (i + 3) == p_axis ? rotate_gizmo_color_hl[i] : rotate_gizmo_color[i]);
  4290. scale_gizmo[i]->surface_set_material(0, (i + 9) == p_axis ? gizmo_color_hl[i] : gizmo_color[i]);
  4291. scale_plane_gizmo[i]->surface_set_material(0, (i + 12) == p_axis ? plane_gizmo_color_hl[i] : plane_gizmo_color[i]);
  4292. }
  4293. }
  4294. void Node3DEditor::update_transform_gizmo() {
  4295. int count = 0;
  4296. bool local_gizmo_coords = are_local_coords_enabled();
  4297. Vector3 gizmo_center;
  4298. Basis gizmo_basis;
  4299. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  4300. if (se && se->gizmo.is_valid()) {
  4301. for (const KeyValue<int, Transform3D> &E : se->subgizmos) {
  4302. Transform3D xf = se->sp->get_global_transform() * se->gizmo->get_subgizmo_transform(E.key);
  4303. gizmo_center += xf.origin;
  4304. if (count == 0 && local_gizmo_coords) {
  4305. gizmo_basis = xf.basis;
  4306. }
  4307. count++;
  4308. }
  4309. } else {
  4310. List<Node *> &selection = editor_selection->get_selected_node_list();
  4311. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  4312. Node3D *sp = Object::cast_to<Node3D>(E->get());
  4313. if (!sp) {
  4314. continue;
  4315. }
  4316. if (sp->has_meta("_edit_lock_")) {
  4317. continue;
  4318. }
  4319. Node3DEditorSelectedItem *sel_item = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  4320. if (!sel_item) {
  4321. continue;
  4322. }
  4323. Transform3D xf = sel_item->sp->get_global_transform();
  4324. gizmo_center += xf.origin;
  4325. if (count == 0 && local_gizmo_coords) {
  4326. gizmo_basis = xf.basis;
  4327. }
  4328. count++;
  4329. }
  4330. }
  4331. gizmo.visible = count > 0;
  4332. gizmo.transform.origin = (count > 0) ? gizmo_center / count : Vector3();
  4333. gizmo.transform.basis = (count == 1) ? gizmo_basis : Basis();
  4334. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  4335. viewports[i]->update_transform_gizmo_view();
  4336. }
  4337. }
  4338. void _update_all_gizmos(Node *p_node) {
  4339. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  4340. Node3D *spatial_node = Object::cast_to<Node3D>(p_node->get_child(i));
  4341. if (spatial_node) {
  4342. spatial_node->update_gizmos();
  4343. }
  4344. _update_all_gizmos(p_node->get_child(i));
  4345. }
  4346. }
  4347. void Node3DEditor::update_all_gizmos(Node *p_node) {
  4348. if (!p_node && is_inside_tree()) {
  4349. p_node = get_tree()->get_edited_scene_root();
  4350. }
  4351. if (!p_node) {
  4352. // No edited scene, so nothing to update.
  4353. return;
  4354. }
  4355. _update_all_gizmos(p_node);
  4356. }
  4357. Object *Node3DEditor::_get_editor_data(Object *p_what) {
  4358. Node3D *sp = Object::cast_to<Node3D>(p_what);
  4359. if (!sp) {
  4360. return nullptr;
  4361. }
  4362. Node3DEditorSelectedItem *si = memnew(Node3DEditorSelectedItem);
  4363. si->sp = sp;
  4364. si->sbox_instance = RenderingServer::get_singleton()->instance_create2(
  4365. selection_box->get_rid(),
  4366. sp->get_world_3d()->get_scenario());
  4367. si->sbox_instance_offset = RenderingServer::get_singleton()->instance_create2(
  4368. selection_box->get_rid(),
  4369. sp->get_world_3d()->get_scenario());
  4370. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(
  4371. si->sbox_instance,
  4372. RS::SHADOW_CASTING_SETTING_OFF);
  4373. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(
  4374. si->sbox_instance_offset,
  4375. RS::SHADOW_CASTING_SETTING_OFF);
  4376. // Use the Edit layer to hide the selection box when View Gizmos is disabled, since it is a bit distracting.
  4377. // It's still possible to approximately guess what is selected by looking at the manipulation gizmo position.
  4378. RS::get_singleton()->instance_set_layer_mask(si->sbox_instance, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER);
  4379. RS::get_singleton()->instance_set_layer_mask(si->sbox_instance_offset, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER);
  4380. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  4381. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_offset, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  4382. si->sbox_instance_xray = RenderingServer::get_singleton()->instance_create2(
  4383. selection_box_xray->get_rid(),
  4384. sp->get_world_3d()->get_scenario());
  4385. si->sbox_instance_xray_offset = RenderingServer::get_singleton()->instance_create2(
  4386. selection_box_xray->get_rid(),
  4387. sp->get_world_3d()->get_scenario());
  4388. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(
  4389. si->sbox_instance_xray,
  4390. RS::SHADOW_CASTING_SETTING_OFF);
  4391. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(
  4392. si->sbox_instance_xray_offset,
  4393. RS::SHADOW_CASTING_SETTING_OFF);
  4394. // Use the Edit layer to hide the selection box when View Gizmos is disabled, since it is a bit distracting.
  4395. // It's still possible to approximately guess what is selected by looking at the manipulation gizmo position.
  4396. RS::get_singleton()->instance_set_layer_mask(si->sbox_instance_xray, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER);
  4397. RS::get_singleton()->instance_set_layer_mask(si->sbox_instance_xray_offset, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER);
  4398. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_xray, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  4399. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_xray_offset, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  4400. return si;
  4401. }
  4402. void Node3DEditor::_generate_selection_boxes() {
  4403. // Use two AABBs to create the illusion of a slightly thicker line.
  4404. AABB aabb(Vector3(), Vector3(1, 1, 1));
  4405. // Create a x-ray (visible through solid surfaces) and standard version of the selection box.
  4406. // Both will be drawn at the same position, but with different opacity.
  4407. // This lets the user see where the selection is while still having a sense of depth.
  4408. Ref<SurfaceTool> st = memnew(SurfaceTool);
  4409. Ref<SurfaceTool> st_xray = memnew(SurfaceTool);
  4410. st->begin(Mesh::PRIMITIVE_LINES);
  4411. st_xray->begin(Mesh::PRIMITIVE_LINES);
  4412. for (int i = 0; i < 12; i++) {
  4413. Vector3 a, b;
  4414. aabb.get_edge(i, a, b);
  4415. st->add_vertex(a);
  4416. st->add_vertex(b);
  4417. st_xray->add_vertex(a);
  4418. st_xray->add_vertex(b);
  4419. }
  4420. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  4421. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  4422. const Color selection_box_color = EDITOR_GET("editors/3d/selection_box_color");
  4423. mat->set_albedo(selection_box_color);
  4424. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  4425. st->set_material(mat);
  4426. selection_box = st->commit();
  4427. Ref<StandardMaterial3D> mat_xray = memnew(StandardMaterial3D);
  4428. mat_xray->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  4429. mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  4430. mat_xray->set_albedo(selection_box_color * Color(1, 1, 1, 0.15));
  4431. mat_xray->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  4432. st_xray->set_material(mat_xray);
  4433. selection_box_xray = st_xray->commit();
  4434. }
  4435. Dictionary Node3DEditor::get_state() const {
  4436. Dictionary d;
  4437. d["snap_enabled"] = snap_enabled;
  4438. d["translate_snap"] = get_translate_snap();
  4439. d["rotate_snap"] = get_rotate_snap();
  4440. d["scale_snap"] = get_scale_snap();
  4441. d["local_coords"] = tool_option_button[TOOL_OPT_LOCAL_COORDS]->is_pressed();
  4442. int vc = 0;
  4443. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT))) {
  4444. vc = 1;
  4445. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS))) {
  4446. vc = 2;
  4447. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS))) {
  4448. vc = 3;
  4449. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS))) {
  4450. vc = 4;
  4451. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT))) {
  4452. vc = 5;
  4453. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT))) {
  4454. vc = 6;
  4455. }
  4456. d["viewport_mode"] = vc;
  4457. Array vpdata;
  4458. for (int i = 0; i < 4; i++) {
  4459. vpdata.push_back(viewports[i]->get_state());
  4460. }
  4461. d["viewports"] = vpdata;
  4462. d["show_grid"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID));
  4463. d["show_origin"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN));
  4464. d["fov"] = get_fov();
  4465. d["znear"] = get_znear();
  4466. d["zfar"] = get_zfar();
  4467. Dictionary gizmos_status;
  4468. for (int i = 0; i < gizmo_plugins_by_name.size(); i++) {
  4469. if (!gizmo_plugins_by_name[i]->can_be_hidden()) {
  4470. continue;
  4471. }
  4472. int state = gizmos_menu->get_item_state(gizmos_menu->get_item_index(i));
  4473. String name = gizmo_plugins_by_name[i]->get_gizmo_name();
  4474. gizmos_status[name] = state;
  4475. }
  4476. d["gizmos_status"] = gizmos_status;
  4477. {
  4478. Dictionary pd;
  4479. pd["sun_rotation"] = sun_rotation;
  4480. pd["environ_sky_color"] = environ_sky_color->get_pick_color();
  4481. pd["environ_ground_color"] = environ_ground_color->get_pick_color();
  4482. pd["environ_energy"] = environ_energy->get_value();
  4483. pd["environ_glow_enabled"] = environ_glow_button->is_pressed();
  4484. pd["environ_tonemap_enabled"] = environ_tonemap_button->is_pressed();
  4485. pd["environ_ao_enabled"] = environ_ao_button->is_pressed();
  4486. pd["environ_gi_enabled"] = environ_gi_button->is_pressed();
  4487. pd["sun_max_distance"] = sun_max_distance->get_value();
  4488. pd["sun_color"] = sun_color->get_pick_color();
  4489. pd["sun_energy"] = sun_energy->get_value();
  4490. pd["sun_disabled"] = sun_button->is_pressed();
  4491. pd["environ_disabled"] = environ_button->is_pressed();
  4492. d["preview_sun_env"] = pd;
  4493. }
  4494. return d;
  4495. }
  4496. void Node3DEditor::set_state(const Dictionary &p_state) {
  4497. Dictionary d = p_state;
  4498. if (d.has("snap_enabled")) {
  4499. snap_enabled = d["snap_enabled"];
  4500. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(d["snap_enabled"]);
  4501. }
  4502. if (d.has("translate_snap")) {
  4503. snap_translate_value = d["translate_snap"];
  4504. }
  4505. if (d.has("rotate_snap")) {
  4506. snap_rotate_value = d["rotate_snap"];
  4507. }
  4508. if (d.has("scale_snap")) {
  4509. snap_scale_value = d["scale_snap"];
  4510. }
  4511. _snap_update();
  4512. if (d.has("local_coords")) {
  4513. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(d["local_coords"]);
  4514. update_transform_gizmo();
  4515. }
  4516. if (d.has("viewport_mode")) {
  4517. int vc = d["viewport_mode"];
  4518. if (vc == 1) {
  4519. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  4520. } else if (vc == 2) {
  4521. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  4522. } else if (vc == 3) {
  4523. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  4524. } else if (vc == 4) {
  4525. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  4526. } else if (vc == 5) {
  4527. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  4528. } else if (vc == 6) {
  4529. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  4530. }
  4531. }
  4532. if (d.has("viewports")) {
  4533. Array vp = d["viewports"];
  4534. uint32_t vp_size = static_cast<uint32_t>(vp.size());
  4535. if (vp_size > VIEWPORTS_COUNT) {
  4536. WARN_PRINT("Ignoring superfluous viewport settings from spatial editor state.");
  4537. vp_size = VIEWPORTS_COUNT;
  4538. }
  4539. for (uint32_t i = 0; i < vp_size; i++) {
  4540. viewports[i]->set_state(vp[i]);
  4541. }
  4542. }
  4543. if (d.has("zfar")) {
  4544. settings_zfar->set_value(double(d["zfar"]));
  4545. }
  4546. if (d.has("znear")) {
  4547. settings_znear->set_value(double(d["znear"]));
  4548. }
  4549. if (d.has("fov")) {
  4550. settings_fov->set_value(double(d["fov"]));
  4551. }
  4552. if (d.has("show_grid")) {
  4553. bool use = d["show_grid"];
  4554. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID))) {
  4555. _menu_item_pressed(MENU_VIEW_GRID);
  4556. }
  4557. }
  4558. if (d.has("show_origin")) {
  4559. bool use = d["show_origin"];
  4560. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN))) {
  4561. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), use);
  4562. RenderingServer::get_singleton()->instance_set_visible(origin_instance, use);
  4563. }
  4564. }
  4565. if (d.has("gizmos_status")) {
  4566. Dictionary gizmos_status = d["gizmos_status"];
  4567. List<Variant> keys;
  4568. gizmos_status.get_key_list(&keys);
  4569. for (int j = 0; j < gizmo_plugins_by_name.size(); ++j) {
  4570. if (!gizmo_plugins_by_name[j]->can_be_hidden()) {
  4571. continue;
  4572. }
  4573. int state = EditorNode3DGizmoPlugin::VISIBLE;
  4574. for (int i = 0; i < keys.size(); i++) {
  4575. if (gizmo_plugins_by_name.write[j]->get_gizmo_name() == String(keys[i])) {
  4576. state = gizmos_status[keys[i]];
  4577. break;
  4578. }
  4579. }
  4580. gizmo_plugins_by_name.write[j]->set_state(state);
  4581. }
  4582. _update_gizmos_menu();
  4583. }
  4584. if (d.has("preview_sun_env")) {
  4585. sun_environ_updating = true;
  4586. Dictionary pd = d["preview_sun_env"];
  4587. sun_rotation = pd["sun_rotation"];
  4588. environ_sky_color->set_pick_color(pd["environ_sky_color"]);
  4589. environ_ground_color->set_pick_color(pd["environ_ground_color"]);
  4590. environ_energy->set_value(pd["environ_energy"]);
  4591. environ_glow_button->set_pressed(pd["environ_glow_enabled"]);
  4592. environ_tonemap_button->set_pressed(pd["environ_tonemap_enabled"]);
  4593. environ_ao_button->set_pressed(pd["environ_ao_enabled"]);
  4594. environ_gi_button->set_pressed(pd["environ_gi_enabled"]);
  4595. sun_max_distance->set_value(pd["sun_max_distance"]);
  4596. sun_color->set_pick_color(pd["sun_color"]);
  4597. sun_energy->set_value(pd["sun_energy"]);
  4598. sun_button->set_pressed(pd["sun_disabled"]);
  4599. environ_button->set_pressed(pd["environ_disabled"]);
  4600. sun_environ_updating = false;
  4601. _preview_settings_changed();
  4602. _update_preview_environment();
  4603. } else {
  4604. _load_default_preview_settings();
  4605. sun_button->set_pressed(false);
  4606. environ_button->set_pressed(false);
  4607. _preview_settings_changed();
  4608. _update_preview_environment();
  4609. }
  4610. }
  4611. void Node3DEditor::edit(Node3D *p_spatial) {
  4612. if (p_spatial != selected) {
  4613. if (selected) {
  4614. Vector<Ref<Node3DGizmo>> gizmos = selected->get_gizmos();
  4615. for (int i = 0; i < gizmos.size(); i++) {
  4616. Ref<EditorNode3DGizmo> seg = gizmos[i];
  4617. if (!seg.is_valid()) {
  4618. continue;
  4619. }
  4620. seg->set_selected(false);
  4621. }
  4622. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected);
  4623. if (se) {
  4624. se->gizmo.unref();
  4625. se->subgizmos.clear();
  4626. }
  4627. selected->update_gizmos();
  4628. }
  4629. selected = p_spatial;
  4630. current_hover_gizmo = Ref<EditorNode3DGizmo>();
  4631. current_hover_gizmo_handle = -1;
  4632. current_hover_gizmo_handle_secondary = false;
  4633. if (selected) {
  4634. Vector<Ref<Node3DGizmo>> gizmos = selected->get_gizmos();
  4635. for (int i = 0; i < gizmos.size(); i++) {
  4636. Ref<EditorNode3DGizmo> seg = gizmos[i];
  4637. if (!seg.is_valid()) {
  4638. continue;
  4639. }
  4640. seg->set_selected(true);
  4641. }
  4642. selected->update_gizmos();
  4643. }
  4644. }
  4645. }
  4646. void Node3DEditor::_snap_changed() {
  4647. snap_translate_value = snap_translate->get_text().to_float();
  4648. snap_rotate_value = snap_rotate->get_text().to_float();
  4649. snap_scale_value = snap_scale->get_text().to_float();
  4650. }
  4651. void Node3DEditor::_snap_update() {
  4652. snap_translate->set_text(String::num(snap_translate_value));
  4653. snap_rotate->set_text(String::num(snap_rotate_value));
  4654. snap_scale->set_text(String::num(snap_scale_value));
  4655. }
  4656. void Node3DEditor::_xform_dialog_action() {
  4657. Transform3D t;
  4658. //translation
  4659. Vector3 scale;
  4660. Vector3 rotate;
  4661. Vector3 translate;
  4662. for (int i = 0; i < 3; i++) {
  4663. translate[i] = xform_translate[i]->get_text().to_float();
  4664. rotate[i] = Math::deg2rad(xform_rotate[i]->get_text().to_float());
  4665. scale[i] = xform_scale[i]->get_text().to_float();
  4666. }
  4667. t.basis.scale(scale);
  4668. t.basis.rotate(rotate);
  4669. t.origin = translate;
  4670. undo_redo->create_action(TTR("XForm Dialog"));
  4671. const List<Node *> &selection = editor_selection->get_selected_node_list();
  4672. for (Node *E : selection) {
  4673. Node3D *sp = Object::cast_to<Node3D>(E);
  4674. if (!sp) {
  4675. continue;
  4676. }
  4677. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  4678. if (!se) {
  4679. continue;
  4680. }
  4681. bool post = xform_type->get_selected() > 0;
  4682. Transform3D tr = sp->get_global_gizmo_transform();
  4683. if (post) {
  4684. tr = tr * t;
  4685. } else {
  4686. tr.basis = t.basis * tr.basis;
  4687. tr.origin += t.origin;
  4688. }
  4689. undo_redo->add_do_method(sp, "set_global_transform", tr);
  4690. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  4691. }
  4692. undo_redo->commit_action();
  4693. }
  4694. void Node3DEditor::_menu_item_toggled(bool pressed, int p_option) {
  4695. switch (p_option) {
  4696. case MENU_TOOL_LOCAL_COORDS: {
  4697. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(pressed);
  4698. update_transform_gizmo();
  4699. } break;
  4700. case MENU_TOOL_USE_SNAP: {
  4701. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(pressed);
  4702. snap_enabled = pressed;
  4703. } break;
  4704. case MENU_TOOL_OVERRIDE_CAMERA: {
  4705. EditorDebuggerNode *const debugger = EditorDebuggerNode::get_singleton();
  4706. using Override = EditorDebuggerNode::CameraOverride;
  4707. if (pressed) {
  4708. debugger->set_camera_override((Override)(Override::OVERRIDE_3D_1 + camera_override_viewport_id));
  4709. } else {
  4710. debugger->set_camera_override(Override::OVERRIDE_NONE);
  4711. }
  4712. } break;
  4713. }
  4714. }
  4715. void Node3DEditor::_menu_gizmo_toggled(int p_option) {
  4716. const int idx = gizmos_menu->get_item_index(p_option);
  4717. gizmos_menu->toggle_item_multistate(idx);
  4718. // Change icon
  4719. const int state = gizmos_menu->get_item_state(idx);
  4720. switch (state) {
  4721. case EditorNode3DGizmoPlugin::VISIBLE:
  4722. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_theme_icon(SNAME("visibility_visible")));
  4723. break;
  4724. case EditorNode3DGizmoPlugin::ON_TOP:
  4725. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_theme_icon(SNAME("visibility_xray")));
  4726. break;
  4727. case EditorNode3DGizmoPlugin::HIDDEN:
  4728. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_theme_icon(SNAME("visibility_hidden")));
  4729. break;
  4730. }
  4731. gizmo_plugins_by_name.write[p_option]->set_state(state);
  4732. update_all_gizmos();
  4733. }
  4734. void Node3DEditor::_update_camera_override_button(bool p_game_running) {
  4735. Button *const button = tool_option_button[TOOL_OPT_OVERRIDE_CAMERA];
  4736. if (p_game_running) {
  4737. button->set_disabled(false);
  4738. button->set_tooltip(TTR("Project Camera Override\nOverrides the running project's camera with the editor viewport camera."));
  4739. } else {
  4740. button->set_disabled(true);
  4741. button->set_pressed(false);
  4742. button->set_tooltip(TTR("Project Camera Override\nNo project instance running. Run the project from the editor to use this feature."));
  4743. }
  4744. }
  4745. void Node3DEditor::_update_camera_override_viewport(Object *p_viewport) {
  4746. Node3DEditorViewport *current_viewport = Object::cast_to<Node3DEditorViewport>(p_viewport);
  4747. if (!current_viewport) {
  4748. return;
  4749. }
  4750. EditorDebuggerNode *const debugger = EditorDebuggerNode::get_singleton();
  4751. camera_override_viewport_id = current_viewport->index;
  4752. if (debugger->get_camera_override() >= EditorDebuggerNode::OVERRIDE_3D_1) {
  4753. using Override = EditorDebuggerNode::CameraOverride;
  4754. debugger->set_camera_override((Override)(Override::OVERRIDE_3D_1 + camera_override_viewport_id));
  4755. }
  4756. }
  4757. void Node3DEditor::_menu_item_pressed(int p_option) {
  4758. switch (p_option) {
  4759. case MENU_TOOL_SELECT:
  4760. case MENU_TOOL_MOVE:
  4761. case MENU_TOOL_ROTATE:
  4762. case MENU_TOOL_SCALE:
  4763. case MENU_TOOL_LIST_SELECT: {
  4764. for (int i = 0; i < TOOL_MAX; i++) {
  4765. tool_button[i]->set_pressed(i == p_option);
  4766. }
  4767. tool_mode = (ToolMode)p_option;
  4768. update_transform_gizmo();
  4769. } break;
  4770. case MENU_TRANSFORM_CONFIGURE_SNAP: {
  4771. snap_dialog->popup_centered(Size2(200, 180));
  4772. } break;
  4773. case MENU_TRANSFORM_DIALOG: {
  4774. for (int i = 0; i < 3; i++) {
  4775. xform_translate[i]->set_text("0");
  4776. xform_rotate[i]->set_text("0");
  4777. xform_scale[i]->set_text("1");
  4778. }
  4779. xform_dialog->popup_centered(Size2(320, 240) * EDSCALE);
  4780. } break;
  4781. case MENU_VIEW_USE_1_VIEWPORT: {
  4782. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_1_VIEWPORT);
  4783. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true);
  4784. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  4785. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  4786. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  4787. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  4788. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  4789. } break;
  4790. case MENU_VIEW_USE_2_VIEWPORTS: {
  4791. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_2_VIEWPORTS);
  4792. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  4793. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true);
  4794. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  4795. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  4796. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  4797. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  4798. } break;
  4799. case MENU_VIEW_USE_2_VIEWPORTS_ALT: {
  4800. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_2_VIEWPORTS_ALT);
  4801. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  4802. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  4803. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  4804. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  4805. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), true);
  4806. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  4807. } break;
  4808. case MENU_VIEW_USE_3_VIEWPORTS: {
  4809. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_3_VIEWPORTS);
  4810. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  4811. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  4812. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), true);
  4813. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  4814. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  4815. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  4816. } break;
  4817. case MENU_VIEW_USE_3_VIEWPORTS_ALT: {
  4818. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_3_VIEWPORTS_ALT);
  4819. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  4820. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  4821. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  4822. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  4823. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  4824. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), true);
  4825. } break;
  4826. case MENU_VIEW_USE_4_VIEWPORTS: {
  4827. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_4_VIEWPORTS);
  4828. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  4829. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  4830. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  4831. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), true);
  4832. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  4833. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  4834. } break;
  4835. case MENU_VIEW_ORIGIN: {
  4836. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  4837. origin_enabled = !is_checked;
  4838. RenderingServer::get_singleton()->instance_set_visible(origin_instance, origin_enabled);
  4839. // Update the grid since its appearance depends on whether the origin is enabled
  4840. _finish_grid();
  4841. _init_grid();
  4842. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), origin_enabled);
  4843. } break;
  4844. case MENU_VIEW_GRID: {
  4845. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  4846. grid_enabled = !is_checked;
  4847. for (int i = 0; i < 3; ++i) {
  4848. if (grid_enable[i]) {
  4849. grid_visible[i] = grid_enabled;
  4850. }
  4851. }
  4852. _finish_grid();
  4853. _init_grid();
  4854. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), grid_enabled);
  4855. } break;
  4856. case MENU_VIEW_CAMERA_SETTINGS: {
  4857. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50));
  4858. } break;
  4859. case MENU_SNAP_TO_FLOOR: {
  4860. snap_selected_nodes_to_floor();
  4861. } break;
  4862. case MENU_LOCK_SELECTED: {
  4863. undo_redo->create_action(TTR("Lock Selected"));
  4864. List<Node *> &selection = editor_selection->get_selected_node_list();
  4865. for (Node *E : selection) {
  4866. Node3D *spatial = Object::cast_to<Node3D>(E);
  4867. if (!spatial || !spatial->is_inside_tree()) {
  4868. continue;
  4869. }
  4870. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  4871. continue;
  4872. }
  4873. undo_redo->add_do_method(spatial, "set_meta", "_edit_lock_", true);
  4874. undo_redo->add_undo_method(spatial, "remove_meta", "_edit_lock_");
  4875. undo_redo->add_do_method(this, "emit_signal", "item_lock_status_changed");
  4876. undo_redo->add_undo_method(this, "emit_signal", "item_lock_status_changed");
  4877. }
  4878. undo_redo->add_do_method(this, "_refresh_menu_icons");
  4879. undo_redo->add_undo_method(this, "_refresh_menu_icons");
  4880. undo_redo->commit_action();
  4881. } break;
  4882. case MENU_UNLOCK_SELECTED: {
  4883. undo_redo->create_action(TTR("Unlock Selected"));
  4884. List<Node *> &selection = editor_selection->get_selected_node_list();
  4885. for (Node *E : selection) {
  4886. Node3D *spatial = Object::cast_to<Node3D>(E);
  4887. if (!spatial || !spatial->is_inside_tree()) {
  4888. continue;
  4889. }
  4890. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  4891. continue;
  4892. }
  4893. undo_redo->add_do_method(spatial, "remove_meta", "_edit_lock_");
  4894. undo_redo->add_undo_method(spatial, "set_meta", "_edit_lock_", true);
  4895. undo_redo->add_do_method(this, "emit_signal", "item_lock_status_changed");
  4896. undo_redo->add_undo_method(this, "emit_signal", "item_lock_status_changed");
  4897. }
  4898. undo_redo->add_do_method(this, "_refresh_menu_icons");
  4899. undo_redo->add_undo_method(this, "_refresh_menu_icons");
  4900. undo_redo->commit_action();
  4901. } break;
  4902. case MENU_GROUP_SELECTED: {
  4903. undo_redo->create_action(TTR("Group Selected"));
  4904. List<Node *> &selection = editor_selection->get_selected_node_list();
  4905. for (Node *E : selection) {
  4906. Node3D *spatial = Object::cast_to<Node3D>(E);
  4907. if (!spatial || !spatial->is_inside_tree()) {
  4908. continue;
  4909. }
  4910. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  4911. continue;
  4912. }
  4913. undo_redo->add_do_method(spatial, "set_meta", "_edit_group_", true);
  4914. undo_redo->add_undo_method(spatial, "remove_meta", "_edit_group_");
  4915. undo_redo->add_do_method(this, "emit_signal", "item_group_status_changed");
  4916. undo_redo->add_undo_method(this, "emit_signal", "item_group_status_changed");
  4917. }
  4918. undo_redo->add_do_method(this, "_refresh_menu_icons");
  4919. undo_redo->add_undo_method(this, "_refresh_menu_icons");
  4920. undo_redo->commit_action();
  4921. } break;
  4922. case MENU_UNGROUP_SELECTED: {
  4923. undo_redo->create_action(TTR("Ungroup Selected"));
  4924. List<Node *> &selection = editor_selection->get_selected_node_list();
  4925. for (Node *E : selection) {
  4926. Node3D *spatial = Object::cast_to<Node3D>(E);
  4927. if (!spatial || !spatial->is_inside_tree()) {
  4928. continue;
  4929. }
  4930. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  4931. continue;
  4932. }
  4933. undo_redo->add_do_method(spatial, "remove_meta", "_edit_group_");
  4934. undo_redo->add_undo_method(spatial, "set_meta", "_edit_group_", true);
  4935. undo_redo->add_do_method(this, "emit_signal", "item_group_status_changed");
  4936. undo_redo->add_undo_method(this, "emit_signal", "item_group_status_changed");
  4937. }
  4938. undo_redo->add_do_method(this, "_refresh_menu_icons");
  4939. undo_redo->add_undo_method(this, "_refresh_menu_icons");
  4940. undo_redo->commit_action();
  4941. } break;
  4942. }
  4943. }
  4944. void Node3DEditor::_init_indicators() {
  4945. {
  4946. origin_enabled = true;
  4947. grid_enabled = true;
  4948. indicator_mat.instantiate();
  4949. indicator_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  4950. indicator_mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  4951. indicator_mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  4952. indicator_mat->set_transparency(StandardMaterial3D::Transparency::TRANSPARENCY_ALPHA_DEPTH_PRE_PASS);
  4953. Vector<Color> origin_colors;
  4954. Vector<Vector3> origin_points;
  4955. const int count_of_elements = 3 * 6;
  4956. origin_colors.resize(count_of_elements);
  4957. origin_points.resize(count_of_elements);
  4958. int x = 0;
  4959. for (int i = 0; i < 3; i++) {
  4960. Vector3 axis;
  4961. axis[i] = 1;
  4962. Color origin_color;
  4963. switch (i) {
  4964. case 0:
  4965. origin_color = get_theme_color(SNAME("axis_x_color"), SNAME("Editor"));
  4966. break;
  4967. case 1:
  4968. origin_color = get_theme_color(SNAME("axis_y_color"), SNAME("Editor"));
  4969. break;
  4970. case 2:
  4971. origin_color = get_theme_color(SNAME("axis_z_color"), SNAME("Editor"));
  4972. break;
  4973. default:
  4974. origin_color = Color();
  4975. break;
  4976. }
  4977. grid_enable[i] = false;
  4978. grid_visible[i] = false;
  4979. origin_colors.set(x, origin_color);
  4980. origin_colors.set(x + 1, origin_color);
  4981. origin_colors.set(x + 2, origin_color);
  4982. origin_colors.set(x + 3, origin_color);
  4983. origin_colors.set(x + 4, origin_color);
  4984. origin_colors.set(x + 5, origin_color);
  4985. // To both allow having a large origin size and avoid jitter
  4986. // at small scales, we should segment the line into pieces.
  4987. // 3 pieces seems to do the trick, and let's use powers of 2.
  4988. origin_points.set(x, axis * 1048576);
  4989. origin_points.set(x + 1, axis * 1024);
  4990. origin_points.set(x + 2, axis * 1024);
  4991. origin_points.set(x + 3, axis * -1024);
  4992. origin_points.set(x + 4, axis * -1024);
  4993. origin_points.set(x + 5, axis * -1048576);
  4994. x += 6;
  4995. }
  4996. Ref<Shader> grid_shader = memnew(Shader);
  4997. grid_shader->set_code(R"(
  4998. // 3D editor grid shader.
  4999. shader_type spatial;
  5000. render_mode unshaded;
  5001. uniform bool orthogonal;
  5002. uniform float grid_size;
  5003. void vertex() {
  5004. // From FLAG_SRGB_VERTEX_COLOR.
  5005. if (!OUTPUT_IS_SRGB) {
  5006. COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb, vec3(0.04045)));
  5007. }
  5008. }
  5009. void fragment() {
  5010. ALBEDO = COLOR.rgb;
  5011. vec3 dir = orthogonal ? -vec3(0, 0, 1) : VIEW;
  5012. float angle_fade = abs(dot(dir, NORMAL));
  5013. angle_fade = smoothstep(0.05, 0.2, angle_fade);
  5014. vec3 world_pos = (INV_VIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
  5015. vec3 world_normal = (INV_VIEW_MATRIX * vec4(NORMAL, 0.0)).xyz;
  5016. vec3 camera_world_pos = INV_VIEW_MATRIX[3].xyz;
  5017. vec3 camera_world_pos_on_plane = camera_world_pos * (1.0 - world_normal);
  5018. float dist_fade = 1.0 - (distance(world_pos, camera_world_pos_on_plane) / grid_size);
  5019. dist_fade = smoothstep(0.02, 0.3, dist_fade);
  5020. ALPHA = COLOR.a * dist_fade * angle_fade;
  5021. }
  5022. )");
  5023. for (int i = 0; i < 3; i++) {
  5024. grid_mat[i].instantiate();
  5025. grid_mat[i]->set_shader(grid_shader);
  5026. }
  5027. grid_enable[0] = EditorSettings::get_singleton()->get("editors/3d/grid_xy_plane");
  5028. grid_enable[1] = EditorSettings::get_singleton()->get("editors/3d/grid_yz_plane");
  5029. grid_enable[2] = EditorSettings::get_singleton()->get("editors/3d/grid_xz_plane");
  5030. grid_visible[0] = grid_enable[0];
  5031. grid_visible[1] = grid_enable[1];
  5032. grid_visible[2] = grid_enable[2];
  5033. _init_grid();
  5034. origin = RenderingServer::get_singleton()->mesh_create();
  5035. Array d;
  5036. d.resize(RS::ARRAY_MAX);
  5037. d[RenderingServer::ARRAY_VERTEX] = origin_points;
  5038. d[RenderingServer::ARRAY_COLOR] = origin_colors;
  5039. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(origin, RenderingServer::PRIMITIVE_LINES, d);
  5040. RenderingServer::get_singleton()->mesh_surface_set_material(origin, 0, indicator_mat->get_rid());
  5041. origin_instance = RenderingServer::get_singleton()->instance_create2(origin, get_tree()->get_root()->get_world_3d()->get_scenario());
  5042. RS::get_singleton()->instance_set_layer_mask(origin_instance, 1 << Node3DEditorViewport::GIZMO_GRID_LAYER);
  5043. RS::get_singleton()->instance_geometry_set_flag(origin_instance, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  5044. RenderingServer::get_singleton()->instance_geometry_set_cast_shadows_setting(origin_instance, RS::SHADOW_CASTING_SETTING_OFF);
  5045. }
  5046. {
  5047. //move gizmo
  5048. // Inverted zxy.
  5049. Vector3 ivec = Vector3(0, 0, -1);
  5050. Vector3 nivec = Vector3(-1, -1, 0);
  5051. Vector3 ivec2 = Vector3(-1, 0, 0);
  5052. Vector3 ivec3 = Vector3(0, -1, 0);
  5053. for (int i = 0; i < 3; i++) {
  5054. Color col;
  5055. switch (i) {
  5056. case 0:
  5057. col = get_theme_color(SNAME("axis_x_color"), SNAME("Editor"));
  5058. break;
  5059. case 1:
  5060. col = get_theme_color(SNAME("axis_y_color"), SNAME("Editor"));
  5061. break;
  5062. case 2:
  5063. col = get_theme_color(SNAME("axis_z_color"), SNAME("Editor"));
  5064. break;
  5065. default:
  5066. col = Color();
  5067. break;
  5068. }
  5069. col.a = EditorSettings::get_singleton()->get("editors/3d/manipulator_gizmo_opacity");
  5070. move_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  5071. move_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  5072. rotate_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  5073. scale_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  5074. scale_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  5075. axis_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  5076. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  5077. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  5078. mat->set_on_top_of_alpha();
  5079. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  5080. mat->set_albedo(col);
  5081. gizmo_color[i] = mat;
  5082. Ref<StandardMaterial3D> mat_hl = mat->duplicate();
  5083. const Color albedo = col.from_hsv(col.get_h(), 0.25, 1.0, 1);
  5084. mat_hl->set_albedo(albedo);
  5085. gizmo_color_hl[i] = mat_hl;
  5086. //translate
  5087. {
  5088. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  5089. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  5090. // Arrow profile
  5091. const int arrow_points = 5;
  5092. Vector3 arrow[5] = {
  5093. nivec * 0.0 + ivec * 0.0,
  5094. nivec * 0.01 + ivec * 0.0,
  5095. nivec * 0.01 + ivec * GIZMO_ARROW_OFFSET,
  5096. nivec * 0.065 + ivec * GIZMO_ARROW_OFFSET,
  5097. nivec * 0.0 + ivec * (GIZMO_ARROW_OFFSET + GIZMO_ARROW_SIZE),
  5098. };
  5099. int arrow_sides = 16;
  5100. const real_t arrow_sides_step = Math_TAU / arrow_sides;
  5101. for (int k = 0; k < arrow_sides; k++) {
  5102. Basis ma(ivec, k * arrow_sides_step);
  5103. Basis mb(ivec, (k + 1) * arrow_sides_step);
  5104. for (int j = 0; j < arrow_points - 1; j++) {
  5105. Vector3 points[4] = {
  5106. ma.xform(arrow[j]),
  5107. mb.xform(arrow[j]),
  5108. mb.xform(arrow[j + 1]),
  5109. ma.xform(arrow[j + 1]),
  5110. };
  5111. surftool->add_vertex(points[0]);
  5112. surftool->add_vertex(points[1]);
  5113. surftool->add_vertex(points[2]);
  5114. surftool->add_vertex(points[0]);
  5115. surftool->add_vertex(points[2]);
  5116. surftool->add_vertex(points[3]);
  5117. }
  5118. }
  5119. surftool->set_material(mat);
  5120. surftool->commit(move_gizmo[i]);
  5121. }
  5122. // Plane Translation
  5123. {
  5124. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  5125. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  5126. Vector3 vec = ivec2 - ivec3;
  5127. Vector3 plane[4] = {
  5128. vec * GIZMO_PLANE_DST,
  5129. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  5130. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  5131. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  5132. };
  5133. Basis ma(ivec, Math_PI / 2);
  5134. Vector3 points[4] = {
  5135. ma.xform(plane[0]),
  5136. ma.xform(plane[1]),
  5137. ma.xform(plane[2]),
  5138. ma.xform(plane[3]),
  5139. };
  5140. surftool->add_vertex(points[0]);
  5141. surftool->add_vertex(points[1]);
  5142. surftool->add_vertex(points[2]);
  5143. surftool->add_vertex(points[0]);
  5144. surftool->add_vertex(points[2]);
  5145. surftool->add_vertex(points[3]);
  5146. Ref<StandardMaterial3D> plane_mat = memnew(StandardMaterial3D);
  5147. plane_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  5148. plane_mat->set_on_top_of_alpha();
  5149. plane_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  5150. plane_mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
  5151. plane_mat->set_albedo(col);
  5152. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  5153. surftool->set_material(plane_mat);
  5154. surftool->commit(move_plane_gizmo[i]);
  5155. Ref<StandardMaterial3D> plane_mat_hl = plane_mat->duplicate();
  5156. plane_mat_hl->set_albedo(albedo);
  5157. plane_gizmo_color_hl[i] = plane_mat_hl; // needed, so we can draw planes from both sides
  5158. }
  5159. // Rotate
  5160. {
  5161. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  5162. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  5163. int n = 128; // number of circle segments
  5164. int m = 3; // number of thickness segments
  5165. real_t step = Math_TAU / n;
  5166. for (int j = 0; j < n; ++j) {
  5167. Basis basis = Basis(ivec, j * step);
  5168. Vector3 vertex = basis.xform(ivec2 * GIZMO_CIRCLE_SIZE);
  5169. for (int k = 0; k < m; ++k) {
  5170. Vector2 ofs = Vector2(Math::cos((Math_TAU * k) / m), Math::sin((Math_TAU * k) / m));
  5171. Vector3 normal = ivec * ofs.x + ivec2 * ofs.y;
  5172. surftool->set_normal(basis.xform(normal));
  5173. surftool->add_vertex(vertex);
  5174. }
  5175. }
  5176. for (int j = 0; j < n; ++j) {
  5177. for (int k = 0; k < m; ++k) {
  5178. int current_ring = j * m;
  5179. int next_ring = ((j + 1) % n) * m;
  5180. int current_segment = k;
  5181. int next_segment = (k + 1) % m;
  5182. surftool->add_index(current_ring + next_segment);
  5183. surftool->add_index(current_ring + current_segment);
  5184. surftool->add_index(next_ring + current_segment);
  5185. surftool->add_index(next_ring + current_segment);
  5186. surftool->add_index(next_ring + next_segment);
  5187. surftool->add_index(current_ring + next_segment);
  5188. }
  5189. }
  5190. Ref<Shader> rotate_shader = memnew(Shader);
  5191. rotate_shader->set_code(R"(
  5192. // 3D editor rotation manipulator gizmo shader.
  5193. shader_type spatial;
  5194. render_mode unshaded, depth_test_disabled;
  5195. uniform vec4 albedo;
  5196. mat3 orthonormalize(mat3 m) {
  5197. vec3 x = normalize(m[0]);
  5198. vec3 y = normalize(m[1] - x * dot(x, m[1]));
  5199. vec3 z = m[2] - x * dot(x, m[2]);
  5200. z = normalize(z - y * (dot(y,m[2])));
  5201. return mat3(x,y,z);
  5202. }
  5203. void vertex() {
  5204. mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX));
  5205. vec3 n = mv * VERTEX;
  5206. float orientation = dot(vec3(0, 0, -1), n);
  5207. if (orientation <= 0.005) {
  5208. VERTEX += NORMAL * 0.02;
  5209. }
  5210. }
  5211. void fragment() {
  5212. ALBEDO = albedo.rgb;
  5213. ALPHA = albedo.a;
  5214. }
  5215. )");
  5216. Ref<ShaderMaterial> rotate_mat = memnew(ShaderMaterial);
  5217. rotate_mat->set_render_priority(Material::RENDER_PRIORITY_MAX);
  5218. rotate_mat->set_shader(rotate_shader);
  5219. rotate_mat->set_shader_param("albedo", col);
  5220. rotate_gizmo_color[i] = rotate_mat;
  5221. Array arrays = surftool->commit_to_arrays();
  5222. rotate_gizmo[i]->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
  5223. rotate_gizmo[i]->surface_set_material(0, rotate_mat);
  5224. Ref<ShaderMaterial> rotate_mat_hl = rotate_mat->duplicate();
  5225. rotate_mat_hl->set_shader_param("albedo", albedo);
  5226. rotate_gizmo_color_hl[i] = rotate_mat_hl;
  5227. if (i == 2) { // Rotation white outline
  5228. Ref<ShaderMaterial> border_mat = rotate_mat->duplicate();
  5229. Ref<Shader> border_shader = memnew(Shader);
  5230. border_shader->set_code(R"(
  5231. // 3D editor rotation manipulator gizmo shader (white outline).
  5232. shader_type spatial;
  5233. render_mode unshaded, depth_test_disabled;
  5234. uniform vec4 albedo;
  5235. mat3 orthonormalize(mat3 m) {
  5236. vec3 x = normalize(m[0]);
  5237. vec3 y = normalize(m[1] - x * dot(x, m[1]));
  5238. vec3 z = m[2] - x * dot(x, m[2]);
  5239. z = normalize(z - y * (dot(y,m[2])));
  5240. return mat3(x,y,z);
  5241. }
  5242. void vertex() {
  5243. mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX));
  5244. mv = inverse(mv);
  5245. VERTEX += NORMAL*0.008;
  5246. vec3 camera_dir_local = mv * vec3(0,0,1);
  5247. vec3 camera_up_local = mv * vec3(0,1,0);
  5248. mat3 rotation_matrix = mat3(cross(camera_dir_local, camera_up_local), camera_up_local, camera_dir_local);
  5249. VERTEX = rotation_matrix * VERTEX;
  5250. }
  5251. void fragment() {
  5252. ALBEDO = albedo.rgb;
  5253. ALPHA = albedo.a;
  5254. }
  5255. )");
  5256. border_mat->set_shader(border_shader);
  5257. border_mat->set_shader_param("albedo", Color(0.75, 0.75, 0.75, col.a / 3.0));
  5258. rotate_gizmo[3] = Ref<ArrayMesh>(memnew(ArrayMesh));
  5259. rotate_gizmo[3]->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
  5260. rotate_gizmo[3]->surface_set_material(0, border_mat);
  5261. }
  5262. }
  5263. // Scale
  5264. {
  5265. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  5266. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  5267. // Cube arrow profile
  5268. const int arrow_points = 6;
  5269. Vector3 arrow[6] = {
  5270. nivec * 0.0 + ivec * 0.0,
  5271. nivec * 0.01 + ivec * 0.0,
  5272. nivec * 0.01 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  5273. nivec * 0.07 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  5274. nivec * 0.07 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  5275. nivec * 0.0 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  5276. };
  5277. int arrow_sides = 4;
  5278. const real_t arrow_sides_step = Math_TAU / arrow_sides;
  5279. for (int k = 0; k < 4; k++) {
  5280. Basis ma(ivec, k * arrow_sides_step);
  5281. Basis mb(ivec, (k + 1) * arrow_sides_step);
  5282. for (int j = 0; j < arrow_points - 1; j++) {
  5283. Vector3 points[4] = {
  5284. ma.xform(arrow[j]),
  5285. mb.xform(arrow[j]),
  5286. mb.xform(arrow[j + 1]),
  5287. ma.xform(arrow[j + 1]),
  5288. };
  5289. surftool->add_vertex(points[0]);
  5290. surftool->add_vertex(points[1]);
  5291. surftool->add_vertex(points[2]);
  5292. surftool->add_vertex(points[0]);
  5293. surftool->add_vertex(points[2]);
  5294. surftool->add_vertex(points[3]);
  5295. }
  5296. }
  5297. surftool->set_material(mat);
  5298. surftool->commit(scale_gizmo[i]);
  5299. }
  5300. // Plane Scale
  5301. {
  5302. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  5303. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  5304. Vector3 vec = ivec2 - ivec3;
  5305. Vector3 plane[4] = {
  5306. vec * GIZMO_PLANE_DST,
  5307. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  5308. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  5309. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  5310. };
  5311. Basis ma(ivec, Math_PI / 2);
  5312. Vector3 points[4] = {
  5313. ma.xform(plane[0]),
  5314. ma.xform(plane[1]),
  5315. ma.xform(plane[2]),
  5316. ma.xform(plane[3]),
  5317. };
  5318. surftool->add_vertex(points[0]);
  5319. surftool->add_vertex(points[1]);
  5320. surftool->add_vertex(points[2]);
  5321. surftool->add_vertex(points[0]);
  5322. surftool->add_vertex(points[2]);
  5323. surftool->add_vertex(points[3]);
  5324. Ref<StandardMaterial3D> plane_mat = memnew(StandardMaterial3D);
  5325. plane_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  5326. plane_mat->set_on_top_of_alpha();
  5327. plane_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  5328. plane_mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
  5329. plane_mat->set_albedo(col);
  5330. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  5331. surftool->set_material(plane_mat);
  5332. surftool->commit(scale_plane_gizmo[i]);
  5333. Ref<StandardMaterial3D> plane_mat_hl = plane_mat->duplicate();
  5334. plane_mat_hl->set_albedo(col.from_hsv(col.get_h(), 0.25, 1.0, 1));
  5335. plane_gizmo_color_hl[i] = plane_mat_hl; // needed, so we can draw planes from both sides
  5336. }
  5337. // Lines to visualize transforms locked to an axis/plane
  5338. {
  5339. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  5340. surftool->begin(Mesh::PRIMITIVE_LINE_STRIP);
  5341. Vector3 vec;
  5342. vec[i] = 1;
  5343. // line extending through infinity(ish)
  5344. surftool->add_vertex(vec * -1048576);
  5345. surftool->add_vertex(Vector3());
  5346. surftool->add_vertex(vec * 1048576);
  5347. surftool->set_material(mat_hl);
  5348. surftool->commit(axis_gizmo[i]);
  5349. }
  5350. }
  5351. }
  5352. _generate_selection_boxes();
  5353. }
  5354. void Node3DEditor::_update_context_menu_stylebox() {
  5355. // This must be called when the theme changes to follow the new accent color.
  5356. Ref<StyleBoxFlat> context_menu_stylebox = memnew(StyleBoxFlat);
  5357. const Color accent_color = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("accent_color"), SNAME("Editor"));
  5358. context_menu_stylebox->set_bg_color(accent_color * Color(1, 1, 1, 0.1));
  5359. // Add an underline to the StyleBox, but prevent its minimum vertical size from changing.
  5360. context_menu_stylebox->set_border_color(accent_color);
  5361. context_menu_stylebox->set_border_width(SIDE_BOTTOM, Math::round(2 * EDSCALE));
  5362. context_menu_stylebox->set_default_margin(SIDE_BOTTOM, 0);
  5363. context_menu_container->add_theme_style_override("panel", context_menu_stylebox);
  5364. }
  5365. void Node3DEditor::_update_gizmos_menu() {
  5366. gizmos_menu->clear();
  5367. for (int i = 0; i < gizmo_plugins_by_name.size(); ++i) {
  5368. if (!gizmo_plugins_by_name[i]->can_be_hidden()) {
  5369. continue;
  5370. }
  5371. String plugin_name = gizmo_plugins_by_name[i]->get_gizmo_name();
  5372. const int plugin_state = gizmo_plugins_by_name[i]->get_state();
  5373. gizmos_menu->add_multistate_item(plugin_name, 3, plugin_state, i);
  5374. const int idx = gizmos_menu->get_item_index(i);
  5375. gizmos_menu->set_item_tooltip(
  5376. idx,
  5377. TTR("Click to toggle between visibility states.\n\nOpen eye: Gizmo is visible.\nClosed eye: Gizmo is hidden.\nHalf-open eye: Gizmo is also visible through opaque surfaces (\"x-ray\")."));
  5378. switch (plugin_state) {
  5379. case EditorNode3DGizmoPlugin::VISIBLE:
  5380. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_visible")));
  5381. break;
  5382. case EditorNode3DGizmoPlugin::ON_TOP:
  5383. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_xray")));
  5384. break;
  5385. case EditorNode3DGizmoPlugin::HIDDEN:
  5386. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_hidden")));
  5387. break;
  5388. }
  5389. }
  5390. }
  5391. void Node3DEditor::_update_gizmos_menu_theme() {
  5392. for (int i = 0; i < gizmo_plugins_by_name.size(); ++i) {
  5393. if (!gizmo_plugins_by_name[i]->can_be_hidden()) {
  5394. continue;
  5395. }
  5396. const int plugin_state = gizmo_plugins_by_name[i]->get_state();
  5397. const int idx = gizmos_menu->get_item_index(i);
  5398. switch (plugin_state) {
  5399. case EditorNode3DGizmoPlugin::VISIBLE:
  5400. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_visible")));
  5401. break;
  5402. case EditorNode3DGizmoPlugin::ON_TOP:
  5403. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_xray")));
  5404. break;
  5405. case EditorNode3DGizmoPlugin::HIDDEN:
  5406. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_hidden")));
  5407. break;
  5408. }
  5409. }
  5410. }
  5411. void Node3DEditor::_init_grid() {
  5412. if (!grid_enabled) {
  5413. return;
  5414. }
  5415. Camera3D *camera = get_editor_viewport(0)->camera;
  5416. Vector3 camera_position = camera->get_position();
  5417. if (camera_position == Vector3()) {
  5418. return; // Camera3D is invalid, don't draw the grid.
  5419. }
  5420. bool orthogonal = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL;
  5421. Vector<Color> grid_colors[3];
  5422. Vector<Vector3> grid_points[3];
  5423. Vector<Vector3> grid_normals[3];
  5424. Color primary_grid_color = EditorSettings::get_singleton()->get("editors/3d/primary_grid_color");
  5425. Color secondary_grid_color = EditorSettings::get_singleton()->get("editors/3d/secondary_grid_color");
  5426. int grid_size = EditorSettings::get_singleton()->get("editors/3d/grid_size");
  5427. int primary_grid_steps = EditorSettings::get_singleton()->get("editors/3d/primary_grid_steps");
  5428. // Which grid planes are enabled? Which should we generate?
  5429. grid_enable[0] = grid_visible[0] = EditorSettings::get_singleton()->get("editors/3d/grid_xy_plane");
  5430. grid_enable[1] = grid_visible[1] = EditorSettings::get_singleton()->get("editors/3d/grid_yz_plane");
  5431. grid_enable[2] = grid_visible[2] = EditorSettings::get_singleton()->get("editors/3d/grid_xz_plane");
  5432. // Offsets division_level for bigger or smaller grids.
  5433. // Default value is -0.2. -1.0 gives Blender-like behavior, 0.5 gives huge grids.
  5434. real_t division_level_bias = EditorSettings::get_singleton()->get("editors/3d/grid_division_level_bias");
  5435. // Default largest grid size is 8^2 when primary_grid_steps is 8 (64m apart, so primary grid lines are 512m apart).
  5436. int division_level_max = EditorSettings::get_singleton()->get("editors/3d/grid_division_level_max");
  5437. // Default smallest grid size is 1cm, 10^-2 (default value is -2).
  5438. int division_level_min = EditorSettings::get_singleton()->get("editors/3d/grid_division_level_min");
  5439. ERR_FAIL_COND_MSG(division_level_max < division_level_min, "The 3D grid's maximum division level cannot be lower than its minimum division level.");
  5440. if (primary_grid_steps != 10) { // Log10 of 10 is 1.
  5441. // Change of base rule, divide by ln(10).
  5442. real_t div = Math::log((real_t)primary_grid_steps) / (real_t)2.302585092994045901094;
  5443. // Trucation (towards zero) is intentional.
  5444. division_level_max = (int)(division_level_max / div);
  5445. division_level_min = (int)(division_level_min / div);
  5446. }
  5447. for (int a = 0; a < 3; a++) {
  5448. if (!grid_enable[a]) {
  5449. continue; // If this grid plane is disabled, skip generation.
  5450. }
  5451. int b = (a + 1) % 3;
  5452. int c = (a + 2) % 3;
  5453. Vector3 normal;
  5454. normal[c] = 1.0;
  5455. real_t camera_distance = Math::abs(camera_position[c]);
  5456. if (orthogonal) {
  5457. camera_distance = camera->get_size() / 2.0;
  5458. Vector3 camera_direction = -camera->get_global_transform().get_basis().get_column(2);
  5459. Plane grid_plane = Plane(normal);
  5460. Vector3 intersection;
  5461. if (grid_plane.intersects_ray(camera_position, camera_direction, &intersection)) {
  5462. camera_position = intersection;
  5463. }
  5464. }
  5465. real_t division_level = Math::log(Math::abs(camera_distance)) / Math::log((double)primary_grid_steps) + division_level_bias;
  5466. real_t clamped_division_level = CLAMP(division_level, division_level_min, division_level_max);
  5467. real_t division_level_floored = Math::floor(clamped_division_level);
  5468. real_t division_level_decimals = clamped_division_level - division_level_floored;
  5469. real_t small_step_size = Math::pow(primary_grid_steps, division_level_floored);
  5470. real_t large_step_size = small_step_size * primary_grid_steps;
  5471. real_t center_a = large_step_size * (int)(camera_position[a] / large_step_size);
  5472. real_t center_b = large_step_size * (int)(camera_position[b] / large_step_size);
  5473. real_t bgn_a = center_a - grid_size * small_step_size;
  5474. real_t end_a = center_a + grid_size * small_step_size;
  5475. real_t bgn_b = center_b - grid_size * small_step_size;
  5476. real_t end_b = center_b + grid_size * small_step_size;
  5477. real_t fade_size = Math::pow(primary_grid_steps, division_level - 1.0);
  5478. real_t min_fade_size = Math::pow(primary_grid_steps, float(division_level_min));
  5479. real_t max_fade_size = Math::pow(primary_grid_steps, float(division_level_max));
  5480. fade_size = CLAMP(fade_size, min_fade_size, max_fade_size);
  5481. real_t grid_fade_size = (grid_size - primary_grid_steps) * fade_size;
  5482. grid_mat[c]->set_shader_param("grid_size", grid_fade_size);
  5483. grid_mat[c]->set_shader_param("orthogonal", orthogonal);
  5484. // Cache these so we don't have to re-access memory.
  5485. Vector<Vector3> &ref_grid = grid_points[c];
  5486. Vector<Vector3> &ref_grid_normals = grid_normals[c];
  5487. Vector<Color> &ref_grid_colors = grid_colors[c];
  5488. // Count our elements same as code below it.
  5489. int expected_size = 0;
  5490. for (int i = -grid_size; i <= grid_size; i++) {
  5491. const real_t position_a = center_a + i * small_step_size;
  5492. const real_t position_b = center_b + i * small_step_size;
  5493. // Don't draw lines over the origin if it's enabled.
  5494. if (!(origin_enabled && Math::is_zero_approx(position_a))) {
  5495. expected_size += 2;
  5496. }
  5497. if (!(origin_enabled && Math::is_zero_approx(position_b))) {
  5498. expected_size += 2;
  5499. }
  5500. }
  5501. int idx = 0;
  5502. ref_grid.resize(expected_size);
  5503. ref_grid_normals.resize(expected_size);
  5504. ref_grid_colors.resize(expected_size);
  5505. // In each iteration of this loop, draw one line in each direction (so two lines per loop, in each if statement).
  5506. for (int i = -grid_size; i <= grid_size; i++) {
  5507. Color line_color;
  5508. // Is this a primary line? Set the appropriate color.
  5509. if (i % primary_grid_steps == 0) {
  5510. line_color = primary_grid_color.lerp(secondary_grid_color, division_level_decimals);
  5511. } else {
  5512. line_color = secondary_grid_color;
  5513. line_color.a = line_color.a * (1 - division_level_decimals);
  5514. }
  5515. real_t position_a = center_a + i * small_step_size;
  5516. real_t position_b = center_b + i * small_step_size;
  5517. // Don't draw lines over the origin if it's enabled.
  5518. if (!(origin_enabled && Math::is_zero_approx(position_a))) {
  5519. Vector3 line_bgn = Vector3();
  5520. Vector3 line_end = Vector3();
  5521. line_bgn[a] = position_a;
  5522. line_end[a] = position_a;
  5523. line_bgn[b] = bgn_b;
  5524. line_end[b] = end_b;
  5525. ref_grid.set(idx, line_bgn);
  5526. ref_grid.set(idx + 1, line_end);
  5527. ref_grid_colors.set(idx, line_color);
  5528. ref_grid_colors.set(idx + 1, line_color);
  5529. ref_grid_normals.set(idx, normal);
  5530. ref_grid_normals.set(idx + 1, normal);
  5531. idx += 2;
  5532. }
  5533. if (!(origin_enabled && Math::is_zero_approx(position_b))) {
  5534. Vector3 line_bgn = Vector3();
  5535. Vector3 line_end = Vector3();
  5536. line_bgn[b] = position_b;
  5537. line_end[b] = position_b;
  5538. line_bgn[a] = bgn_a;
  5539. line_end[a] = end_a;
  5540. ref_grid.set(idx, line_bgn);
  5541. ref_grid.set(idx + 1, line_end);
  5542. ref_grid_colors.set(idx, line_color);
  5543. ref_grid_colors.set(idx + 1, line_color);
  5544. ref_grid_normals.set(idx, normal);
  5545. ref_grid_normals.set(idx + 1, normal);
  5546. idx += 2;
  5547. }
  5548. }
  5549. // Create a mesh from the pushed vector points and colors.
  5550. grid[c] = RenderingServer::get_singleton()->mesh_create();
  5551. Array d;
  5552. d.resize(RS::ARRAY_MAX);
  5553. d[RenderingServer::ARRAY_VERTEX] = grid_points[c];
  5554. d[RenderingServer::ARRAY_COLOR] = grid_colors[c];
  5555. d[RenderingServer::ARRAY_NORMAL] = grid_normals[c];
  5556. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[c], RenderingServer::PRIMITIVE_LINES, d);
  5557. RenderingServer::get_singleton()->mesh_surface_set_material(grid[c], 0, grid_mat[c]->get_rid());
  5558. grid_instance[c] = RenderingServer::get_singleton()->instance_create2(grid[c], get_tree()->get_root()->get_world_3d()->get_scenario());
  5559. // Yes, the end of this line is supposed to be a.
  5560. RenderingServer::get_singleton()->instance_set_visible(grid_instance[c], grid_visible[a]);
  5561. RenderingServer::get_singleton()->instance_geometry_set_cast_shadows_setting(grid_instance[c], RS::SHADOW_CASTING_SETTING_OFF);
  5562. RS::get_singleton()->instance_set_layer_mask(grid_instance[c], 1 << Node3DEditorViewport::GIZMO_GRID_LAYER);
  5563. RS::get_singleton()->instance_geometry_set_flag(grid_instance[c], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  5564. }
  5565. }
  5566. void Node3DEditor::_finish_indicators() {
  5567. RenderingServer::get_singleton()->free(origin_instance);
  5568. RenderingServer::get_singleton()->free(origin);
  5569. _finish_grid();
  5570. }
  5571. void Node3DEditor::_finish_grid() {
  5572. for (int i = 0; i < 3; i++) {
  5573. RenderingServer::get_singleton()->free(grid_instance[i]);
  5574. RenderingServer::get_singleton()->free(grid[i]);
  5575. }
  5576. }
  5577. void Node3DEditor::update_grid() {
  5578. const Camera3D::Projection current_projection = viewports[0]->camera->get_projection();
  5579. if (current_projection != grid_camera_last_update_perspective) {
  5580. grid_init_draw = false; // redraw
  5581. grid_camera_last_update_perspective = current_projection;
  5582. }
  5583. // Gets a orthogonal or perspective position correctly (for the grid comparison)
  5584. const Vector3 camera_position = get_editor_viewport(0)->camera->get_position();
  5585. if (!grid_init_draw || grid_camera_last_update_position.distance_squared_to(camera_position) >= 100.0f) {
  5586. _finish_grid();
  5587. _init_grid();
  5588. grid_init_draw = true;
  5589. grid_camera_last_update_position = camera_position;
  5590. }
  5591. }
  5592. void Node3DEditor::_selection_changed() {
  5593. _refresh_menu_icons();
  5594. if (selected && editor_selection->get_selected_node_list().size() != 1) {
  5595. Vector<Ref<Node3DGizmo>> gizmos = selected->get_gizmos();
  5596. for (int i = 0; i < gizmos.size(); i++) {
  5597. Ref<EditorNode3DGizmo> seg = gizmos[i];
  5598. if (!seg.is_valid()) {
  5599. continue;
  5600. }
  5601. seg->set_selected(false);
  5602. }
  5603. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected);
  5604. if (se) {
  5605. se->gizmo.unref();
  5606. se->subgizmos.clear();
  5607. }
  5608. selected->update_gizmos();
  5609. selected = nullptr;
  5610. }
  5611. update_transform_gizmo();
  5612. }
  5613. void Node3DEditor::_refresh_menu_icons() {
  5614. bool all_locked = true;
  5615. bool all_grouped = true;
  5616. List<Node *> &selection = editor_selection->get_selected_node_list();
  5617. if (selection.is_empty()) {
  5618. all_locked = false;
  5619. all_grouped = false;
  5620. } else {
  5621. for (Node *E : selection) {
  5622. if (Object::cast_to<Node3D>(E) && !Object::cast_to<Node3D>(E)->has_meta("_edit_lock_")) {
  5623. all_locked = false;
  5624. break;
  5625. }
  5626. }
  5627. for (Node *E : selection) {
  5628. if (Object::cast_to<Node3D>(E) && !Object::cast_to<Node3D>(E)->has_meta("_edit_group_")) {
  5629. all_grouped = false;
  5630. break;
  5631. }
  5632. }
  5633. }
  5634. tool_button[TOOL_LOCK_SELECTED]->set_visible(!all_locked);
  5635. tool_button[TOOL_LOCK_SELECTED]->set_disabled(selection.is_empty());
  5636. tool_button[TOOL_UNLOCK_SELECTED]->set_visible(all_locked);
  5637. tool_button[TOOL_GROUP_SELECTED]->set_visible(!all_grouped);
  5638. tool_button[TOOL_GROUP_SELECTED]->set_disabled(selection.is_empty());
  5639. tool_button[TOOL_UNGROUP_SELECTED]->set_visible(all_grouped);
  5640. }
  5641. template <typename T>
  5642. HashSet<T *> _get_child_nodes(Node *parent_node) {
  5643. HashSet<T *> nodes = HashSet<T *>();
  5644. T *node = Node::cast_to<T>(parent_node);
  5645. if (node) {
  5646. nodes.insert(node);
  5647. }
  5648. for (int i = 0; i < parent_node->get_child_count(); i++) {
  5649. Node *child_node = parent_node->get_child(i);
  5650. HashSet<T *> child_nodes = _get_child_nodes<T>(child_node);
  5651. for (T *I : child_nodes) {
  5652. nodes.insert(I);
  5653. }
  5654. }
  5655. return nodes;
  5656. }
  5657. HashSet<RID> _get_physics_bodies_rid(Node *node) {
  5658. HashSet<RID> rids = HashSet<RID>();
  5659. PhysicsBody3D *pb = Node::cast_to<PhysicsBody3D>(node);
  5660. if (pb) {
  5661. rids.insert(pb->get_rid());
  5662. }
  5663. HashSet<PhysicsBody3D *> child_nodes = _get_child_nodes<PhysicsBody3D>(node);
  5664. for (const PhysicsBody3D *I : child_nodes) {
  5665. rids.insert(I->get_rid());
  5666. }
  5667. return rids;
  5668. }
  5669. void Node3DEditor::snap_selected_nodes_to_floor() {
  5670. const List<Node *> &selection = editor_selection->get_selected_node_list();
  5671. Dictionary snap_data;
  5672. for (Node *E : selection) {
  5673. Node3D *sp = Object::cast_to<Node3D>(E);
  5674. if (sp) {
  5675. Vector3 from = Vector3();
  5676. Vector3 position_offset = Vector3();
  5677. // Priorities for snapping to floor are CollisionShapes, VisualInstances and then origin
  5678. HashSet<VisualInstance3D *> vi = _get_child_nodes<VisualInstance3D>(sp);
  5679. HashSet<CollisionShape3D *> cs = _get_child_nodes<CollisionShape3D>(sp);
  5680. bool found_valid_shape = false;
  5681. if (cs.size()) {
  5682. AABB aabb;
  5683. HashSet<CollisionShape3D *>::Iterator I = cs.begin();
  5684. if ((*I)->get_shape().is_valid()) {
  5685. CollisionShape3D *collision_shape = *cs.begin();
  5686. aabb = collision_shape->get_global_transform().xform(collision_shape->get_shape()->get_debug_mesh()->get_aabb());
  5687. found_valid_shape = true;
  5688. }
  5689. for (++I; I; ++I) {
  5690. CollisionShape3D *col_shape = *I;
  5691. if (col_shape->get_shape().is_valid()) {
  5692. aabb.merge_with(col_shape->get_global_transform().xform(col_shape->get_shape()->get_debug_mesh()->get_aabb()));
  5693. found_valid_shape = true;
  5694. }
  5695. }
  5696. if (found_valid_shape) {
  5697. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  5698. from = aabb.position + size;
  5699. position_offset.y = from.y - sp->get_global_transform().origin.y;
  5700. }
  5701. }
  5702. if (!found_valid_shape && vi.size()) {
  5703. AABB aabb = (*vi.begin())->get_transformed_aabb();
  5704. for (const VisualInstance3D *I : vi) {
  5705. aabb.merge_with(I->get_transformed_aabb());
  5706. }
  5707. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  5708. from = aabb.position + size;
  5709. position_offset.y = from.y - sp->get_global_transform().origin.y;
  5710. } else if (!found_valid_shape) {
  5711. from = sp->get_global_transform().origin;
  5712. }
  5713. // We add a bit of margin to the from position to avoid it from snapping
  5714. // when the spatial is already on a floor and there's another floor under
  5715. // it
  5716. from = from + Vector3(0.0, 1, 0.0);
  5717. Dictionary d;
  5718. d["from"] = from;
  5719. d["position_offset"] = position_offset;
  5720. snap_data[sp] = d;
  5721. }
  5722. }
  5723. PhysicsDirectSpaceState3D *ss = get_tree()->get_root()->get_world_3d()->get_direct_space_state();
  5724. PhysicsDirectSpaceState3D::RayResult result;
  5725. Array keys = snap_data.keys();
  5726. // The maximum height an object can travel to be snapped
  5727. const float max_snap_height = 500.0;
  5728. // Will be set to `true` if at least one node from the selection was successfully snapped
  5729. bool snapped_to_floor = false;
  5730. if (keys.size()) {
  5731. // For snapping to be performed, there must be solid geometry under at least one of the selected nodes.
  5732. // We need to check this before snapping to register the undo/redo action only if needed.
  5733. for (int i = 0; i < keys.size(); i++) {
  5734. Node *node = Object::cast_to<Node>(keys[i]);
  5735. Node3D *sp = Object::cast_to<Node3D>(node);
  5736. Dictionary d = snap_data[node];
  5737. Vector3 from = d["from"];
  5738. Vector3 to = from - Vector3(0.0, max_snap_height, 0.0);
  5739. HashSet<RID> excluded = _get_physics_bodies_rid(sp);
  5740. PhysicsDirectSpaceState3D::RayParameters ray_params;
  5741. ray_params.from = from;
  5742. ray_params.to = to;
  5743. ray_params.exclude = excluded;
  5744. if (ss->intersect_ray(ray_params, result)) {
  5745. snapped_to_floor = true;
  5746. }
  5747. }
  5748. if (snapped_to_floor) {
  5749. undo_redo->create_action(TTR("Snap Nodes to Floor"));
  5750. // Perform snapping if at least one node can be snapped
  5751. for (int i = 0; i < keys.size(); i++) {
  5752. Node *node = Object::cast_to<Node>(keys[i]);
  5753. Node3D *sp = Object::cast_to<Node3D>(node);
  5754. Dictionary d = snap_data[node];
  5755. Vector3 from = d["from"];
  5756. Vector3 to = from - Vector3(0.0, max_snap_height, 0.0);
  5757. HashSet<RID> excluded = _get_physics_bodies_rid(sp);
  5758. PhysicsDirectSpaceState3D::RayParameters ray_params;
  5759. ray_params.from = from;
  5760. ray_params.to = to;
  5761. ray_params.exclude = excluded;
  5762. if (ss->intersect_ray(ray_params, result)) {
  5763. Vector3 position_offset = d["position_offset"];
  5764. Transform3D new_transform = sp->get_global_transform();
  5765. new_transform.origin.y = result.position.y;
  5766. new_transform.origin = new_transform.origin - position_offset;
  5767. undo_redo->add_do_method(sp, "set_global_transform", new_transform);
  5768. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_transform());
  5769. }
  5770. }
  5771. undo_redo->commit_action();
  5772. } else {
  5773. EditorNode::get_singleton()->show_warning(TTR("Couldn't find a solid floor to snap the selection to."));
  5774. }
  5775. }
  5776. }
  5777. void Node3DEditor::shortcut_input(const Ref<InputEvent> &p_event) {
  5778. ERR_FAIL_COND(p_event.is_null());
  5779. if (!is_visible_in_tree()) {
  5780. return;
  5781. }
  5782. snap_key_enabled = Input::get_singleton()->is_key_pressed(Key::CTRL);
  5783. }
  5784. void Node3DEditor::_sun_environ_settings_pressed() {
  5785. Vector2 pos = sun_environ_settings->get_screen_position() + sun_environ_settings->get_size();
  5786. sun_environ_popup->set_position(pos - Vector2(sun_environ_popup->get_contents_minimum_size().width / 2, 0));
  5787. sun_environ_popup->reset_size();
  5788. sun_environ_popup->popup();
  5789. }
  5790. void Node3DEditor::_add_sun_to_scene(bool p_already_added_environment) {
  5791. sun_environ_popup->hide();
  5792. if (!p_already_added_environment && world_env_count == 0 && Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  5793. // Prevent infinite feedback loop between the sun and environment methods.
  5794. _add_environment_to_scene(true);
  5795. }
  5796. Node *base = get_tree()->get_edited_scene_root();
  5797. if (!base) {
  5798. // Create a root node so we can add child nodes to it.
  5799. SceneTreeDock::get_singleton()->add_root_node(memnew(Node3D));
  5800. base = get_tree()->get_edited_scene_root();
  5801. }
  5802. ERR_FAIL_COND(!base);
  5803. Node *new_sun = preview_sun->duplicate();
  5804. undo_redo->create_action(TTR("Add Preview Sun to Scene"));
  5805. undo_redo->add_do_method(base, "add_child", new_sun, true);
  5806. // Move to the beginning of the scene tree since more "global" nodes
  5807. // generally look better when placed at the top.
  5808. undo_redo->add_do_method(base, "move_child", new_sun, 0);
  5809. undo_redo->add_do_method(new_sun, "set_owner", base);
  5810. undo_redo->add_undo_method(base, "remove_child", new_sun);
  5811. undo_redo->add_do_reference(new_sun);
  5812. undo_redo->commit_action();
  5813. }
  5814. void Node3DEditor::_add_environment_to_scene(bool p_already_added_sun) {
  5815. sun_environ_popup->hide();
  5816. if (!p_already_added_sun && directional_light_count == 0 && Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  5817. // Prevent infinite feedback loop between the sun and environment methods.
  5818. _add_sun_to_scene(true);
  5819. }
  5820. Node *base = get_tree()->get_edited_scene_root();
  5821. if (!base) {
  5822. // Create a root node so we can add child nodes to it.
  5823. SceneTreeDock::get_singleton()->add_root_node(memnew(Node3D));
  5824. base = get_tree()->get_edited_scene_root();
  5825. }
  5826. ERR_FAIL_COND(!base);
  5827. WorldEnvironment *new_env = memnew(WorldEnvironment);
  5828. new_env->set_environment(preview_environment->get_environment()->duplicate(true));
  5829. undo_redo->create_action(TTR("Add Preview Environment to Scene"));
  5830. undo_redo->add_do_method(base, "add_child", new_env, true);
  5831. // Move to the beginning of the scene tree since more "global" nodes
  5832. // generally look better when placed at the top.
  5833. undo_redo->add_do_method(base, "move_child", new_env, 0);
  5834. undo_redo->add_do_method(new_env, "set_owner", base);
  5835. undo_redo->add_undo_method(base, "remove_child", new_env);
  5836. undo_redo->add_do_reference(new_env);
  5837. undo_redo->commit_action();
  5838. }
  5839. void Node3DEditor::_update_theme() {
  5840. tool_button[TOOL_MODE_SELECT]->set_icon(get_theme_icon(SNAME("ToolSelect"), SNAME("EditorIcons")));
  5841. tool_button[TOOL_MODE_MOVE]->set_icon(get_theme_icon(SNAME("ToolMove"), SNAME("EditorIcons")));
  5842. tool_button[TOOL_MODE_ROTATE]->set_icon(get_theme_icon(SNAME("ToolRotate"), SNAME("EditorIcons")));
  5843. tool_button[TOOL_MODE_SCALE]->set_icon(get_theme_icon(SNAME("ToolScale"), SNAME("EditorIcons")));
  5844. tool_button[TOOL_MODE_LIST_SELECT]->set_icon(get_theme_icon(SNAME("ListSelect"), SNAME("EditorIcons")));
  5845. tool_button[TOOL_LOCK_SELECTED]->set_icon(get_theme_icon(SNAME("Lock"), SNAME("EditorIcons")));
  5846. tool_button[TOOL_UNLOCK_SELECTED]->set_icon(get_theme_icon(SNAME("Unlock"), SNAME("EditorIcons")));
  5847. tool_button[TOOL_GROUP_SELECTED]->set_icon(get_theme_icon(SNAME("Group"), SNAME("EditorIcons")));
  5848. tool_button[TOOL_UNGROUP_SELECTED]->set_icon(get_theme_icon(SNAME("Ungroup"), SNAME("EditorIcons")));
  5849. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_icon(get_theme_icon(SNAME("Object"), SNAME("EditorIcons")));
  5850. tool_option_button[TOOL_OPT_USE_SNAP]->set_icon(get_theme_icon(SNAME("Snap"), SNAME("EditorIcons")));
  5851. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_icon(get_theme_icon(SNAME("Camera3D"), SNAME("EditorIcons")));
  5852. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_theme_icon(SNAME("Panels1"), SNAME("EditorIcons")));
  5853. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_theme_icon(SNAME("Panels2"), SNAME("EditorIcons")));
  5854. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_theme_icon(SNAME("Panels2Alt"), SNAME("EditorIcons")));
  5855. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_theme_icon(SNAME("Panels3"), SNAME("EditorIcons")));
  5856. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_theme_icon(SNAME("Panels3Alt"), SNAME("EditorIcons")));
  5857. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_theme_icon(SNAME("Panels4"), SNAME("EditorIcons")));
  5858. sun_button->set_icon(get_theme_icon(SNAME("DirectionalLight3D"), SNAME("EditorIcons")));
  5859. environ_button->set_icon(get_theme_icon(SNAME("WorldEnvironment"), SNAME("EditorIcons")));
  5860. sun_environ_settings->set_icon(get_theme_icon(SNAME("GuiTabMenuHl"), SNAME("EditorIcons")));
  5861. sun_title->add_theme_font_override("font", get_theme_font(SNAME("title_font"), SNAME("Window")));
  5862. environ_title->add_theme_font_override("font", get_theme_font(SNAME("title_font"), SNAME("Window")));
  5863. sun_color->set_custom_minimum_size(Size2(0, get_theme_constant(SNAME("color_picker_button_height"), SNAME("Editor"))));
  5864. environ_sky_color->set_custom_minimum_size(Size2(0, get_theme_constant(SNAME("color_picker_button_height"), SNAME("Editor"))));
  5865. environ_ground_color->set_custom_minimum_size(Size2(0, get_theme_constant(SNAME("color_picker_button_height"), SNAME("Editor"))));
  5866. }
  5867. void Node3DEditor::_notification(int p_what) {
  5868. switch (p_what) {
  5869. case NOTIFICATION_READY: {
  5870. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  5871. _refresh_menu_icons();
  5872. get_tree()->connect("node_removed", callable_mp(this, &Node3DEditor::_node_removed));
  5873. get_tree()->connect("node_added", callable_mp(this, &Node3DEditor::_node_added));
  5874. SceneTreeDock::get_singleton()->get_tree_editor()->connect("node_changed", callable_mp(this, &Node3DEditor::_refresh_menu_icons));
  5875. editor_selection->connect("selection_changed", callable_mp(this, &Node3DEditor::_selection_changed));
  5876. EditorNode::get_singleton()->connect("stop_pressed", callable_mp(this, &Node3DEditor::_update_camera_override_button), make_binds(false));
  5877. EditorNode::get_singleton()->connect("play_pressed", callable_mp(this, &Node3DEditor::_update_camera_override_button), make_binds(true));
  5878. _update_preview_environment();
  5879. sun_state->set_custom_minimum_size(sun_vb->get_combined_minimum_size());
  5880. environ_state->set_custom_minimum_size(environ_vb->get_combined_minimum_size());
  5881. } break;
  5882. case NOTIFICATION_ENTER_TREE: {
  5883. _update_theme();
  5884. _register_all_gizmos();
  5885. _update_gizmos_menu();
  5886. _init_indicators();
  5887. update_all_gizmos();
  5888. } break;
  5889. case NOTIFICATION_EXIT_TREE: {
  5890. _finish_indicators();
  5891. } break;
  5892. case NOTIFICATION_THEME_CHANGED: {
  5893. _update_theme();
  5894. _update_gizmos_menu_theme();
  5895. _update_context_menu_stylebox();
  5896. sun_title->add_theme_font_override("font", get_theme_font(SNAME("title_font"), SNAME("Window")));
  5897. environ_title->add_theme_font_override("font", get_theme_font(SNAME("title_font"), SNAME("Window")));
  5898. } break;
  5899. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  5900. // Update grid color by rebuilding grid.
  5901. _finish_grid();
  5902. _init_grid();
  5903. } break;
  5904. case NOTIFICATION_VISIBILITY_CHANGED: {
  5905. if (!is_visible() && tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->is_pressed()) {
  5906. EditorDebuggerNode *debugger = EditorDebuggerNode::get_singleton();
  5907. debugger->set_camera_override(EditorDebuggerNode::OVERRIDE_NONE);
  5908. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_pressed(false);
  5909. }
  5910. } break;
  5911. }
  5912. }
  5913. bool Node3DEditor::is_subgizmo_selected(int p_id) {
  5914. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  5915. if (se) {
  5916. return se->subgizmos.has(p_id);
  5917. }
  5918. return false;
  5919. }
  5920. bool Node3DEditor::is_current_selected_gizmo(const EditorNode3DGizmo *p_gizmo) {
  5921. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  5922. if (se) {
  5923. return se->gizmo == p_gizmo;
  5924. }
  5925. return false;
  5926. }
  5927. Vector<int> Node3DEditor::get_subgizmo_selection() {
  5928. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  5929. Vector<int> ret;
  5930. if (se) {
  5931. for (const KeyValue<int, Transform3D> &E : se->subgizmos) {
  5932. ret.push_back(E.key);
  5933. }
  5934. }
  5935. return ret;
  5936. }
  5937. void Node3DEditor::add_control_to_menu_panel(Control *p_control) {
  5938. hbc_context_menu->add_child(p_control);
  5939. }
  5940. void Node3DEditor::remove_control_from_menu_panel(Control *p_control) {
  5941. hbc_context_menu->remove_child(p_control);
  5942. }
  5943. void Node3DEditor::set_can_preview(Camera3D *p_preview) {
  5944. for (int i = 0; i < 4; i++) {
  5945. viewports[i]->set_can_preview(p_preview);
  5946. }
  5947. }
  5948. VSplitContainer *Node3DEditor::get_shader_split() {
  5949. return shader_split;
  5950. }
  5951. void Node3DEditor::add_control_to_left_panel(Control *p_control) {
  5952. left_panel_split->add_child(p_control);
  5953. left_panel_split->move_child(p_control, 0);
  5954. }
  5955. void Node3DEditor::add_control_to_right_panel(Control *p_control) {
  5956. right_panel_split->add_child(p_control);
  5957. right_panel_split->move_child(p_control, 1);
  5958. }
  5959. void Node3DEditor::remove_control_from_left_panel(Control *p_control) {
  5960. left_panel_split->remove_child(p_control);
  5961. }
  5962. void Node3DEditor::remove_control_from_right_panel(Control *p_control) {
  5963. right_panel_split->remove_child(p_control);
  5964. }
  5965. void Node3DEditor::move_control_to_left_panel(Control *p_control) {
  5966. ERR_FAIL_NULL(p_control);
  5967. if (p_control->get_parent() == left_panel_split) {
  5968. return;
  5969. }
  5970. ERR_FAIL_COND(p_control->get_parent() != right_panel_split);
  5971. right_panel_split->remove_child(p_control);
  5972. add_control_to_left_panel(p_control);
  5973. }
  5974. void Node3DEditor::move_control_to_right_panel(Control *p_control) {
  5975. ERR_FAIL_NULL(p_control);
  5976. if (p_control->get_parent() == right_panel_split) {
  5977. return;
  5978. }
  5979. ERR_FAIL_COND(p_control->get_parent() != left_panel_split);
  5980. left_panel_split->remove_child(p_control);
  5981. add_control_to_right_panel(p_control);
  5982. }
  5983. void Node3DEditor::_request_gizmo(Object *p_obj) {
  5984. Node3D *sp = Object::cast_to<Node3D>(p_obj);
  5985. if (!sp) {
  5986. return;
  5987. }
  5988. bool is_selected = (sp == selected);
  5989. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  5990. if (edited_scene && (sp == edited_scene || (sp->get_owner() && edited_scene->is_ancestor_of(sp)))) {
  5991. for (int i = 0; i < gizmo_plugins_by_priority.size(); ++i) {
  5992. Ref<EditorNode3DGizmo> seg = gizmo_plugins_by_priority.write[i]->get_gizmo(sp);
  5993. if (seg.is_valid()) {
  5994. sp->add_gizmo(seg);
  5995. if (is_selected != seg->is_selected()) {
  5996. seg->set_selected(is_selected);
  5997. }
  5998. }
  5999. }
  6000. if (!sp->get_gizmos().is_empty()) {
  6001. sp->update_gizmos();
  6002. }
  6003. }
  6004. }
  6005. void Node3DEditor::_set_subgizmo_selection(Object *p_obj, Ref<Node3DGizmo> p_gizmo, int p_id, Transform3D p_transform) {
  6006. if (p_id == -1) {
  6007. _clear_subgizmo_selection(p_obj);
  6008. return;
  6009. }
  6010. Node3D *sp = nullptr;
  6011. if (p_obj) {
  6012. sp = Object::cast_to<Node3D>(p_obj);
  6013. } else {
  6014. sp = selected;
  6015. }
  6016. if (!sp) {
  6017. return;
  6018. }
  6019. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  6020. if (se) {
  6021. se->subgizmos.clear();
  6022. se->subgizmos.insert(p_id, p_transform);
  6023. se->gizmo = p_gizmo;
  6024. sp->update_gizmos();
  6025. update_transform_gizmo();
  6026. }
  6027. }
  6028. void Node3DEditor::_clear_subgizmo_selection(Object *p_obj) {
  6029. Node3D *sp = nullptr;
  6030. if (p_obj) {
  6031. sp = Object::cast_to<Node3D>(p_obj);
  6032. } else {
  6033. sp = selected;
  6034. }
  6035. if (!sp) {
  6036. return;
  6037. }
  6038. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  6039. if (se) {
  6040. se->subgizmos.clear();
  6041. se->gizmo.unref();
  6042. sp->update_gizmos();
  6043. update_transform_gizmo();
  6044. }
  6045. }
  6046. void Node3DEditor::_toggle_maximize_view(Object *p_viewport) {
  6047. if (!p_viewport) {
  6048. return;
  6049. }
  6050. Node3DEditorViewport *current_viewport = Object::cast_to<Node3DEditorViewport>(p_viewport);
  6051. if (!current_viewport) {
  6052. return;
  6053. }
  6054. int index = -1;
  6055. bool maximized = false;
  6056. for (int i = 0; i < 4; i++) {
  6057. if (viewports[i] == current_viewport) {
  6058. index = i;
  6059. if (current_viewport->get_global_rect() == viewport_base->get_global_rect()) {
  6060. maximized = true;
  6061. }
  6062. break;
  6063. }
  6064. }
  6065. if (index == -1) {
  6066. return;
  6067. }
  6068. if (!maximized) {
  6069. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  6070. if (i == (uint32_t)index) {
  6071. viewports[i]->set_anchors_and_offsets_preset(Control::PRESET_WIDE);
  6072. } else {
  6073. viewports[i]->hide();
  6074. }
  6075. }
  6076. } else {
  6077. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  6078. viewports[i]->show();
  6079. }
  6080. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT))) {
  6081. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  6082. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS))) {
  6083. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  6084. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT))) {
  6085. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  6086. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS))) {
  6087. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  6088. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT))) {
  6089. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  6090. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS))) {
  6091. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  6092. }
  6093. }
  6094. }
  6095. void Node3DEditor::_node_added(Node *p_node) {
  6096. if (EditorNode::get_singleton()->get_scene_root()->is_ancestor_of(p_node)) {
  6097. if (Object::cast_to<WorldEnvironment>(p_node)) {
  6098. world_env_count++;
  6099. if (world_env_count == 1) {
  6100. _update_preview_environment();
  6101. }
  6102. } else if (Object::cast_to<DirectionalLight3D>(p_node)) {
  6103. directional_light_count++;
  6104. if (directional_light_count == 1) {
  6105. _update_preview_environment();
  6106. }
  6107. }
  6108. }
  6109. }
  6110. void Node3DEditor::_node_removed(Node *p_node) {
  6111. if (EditorNode::get_singleton()->get_scene_root()->is_ancestor_of(p_node)) {
  6112. if (Object::cast_to<WorldEnvironment>(p_node)) {
  6113. world_env_count--;
  6114. if (world_env_count == 0) {
  6115. _update_preview_environment();
  6116. }
  6117. } else if (Object::cast_to<DirectionalLight3D>(p_node)) {
  6118. directional_light_count--;
  6119. if (directional_light_count == 0) {
  6120. _update_preview_environment();
  6121. }
  6122. }
  6123. }
  6124. if (p_node == selected) {
  6125. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected);
  6126. if (se) {
  6127. se->gizmo.unref();
  6128. se->subgizmos.clear();
  6129. }
  6130. selected = nullptr;
  6131. update_transform_gizmo();
  6132. }
  6133. }
  6134. void Node3DEditor::_register_all_gizmos() {
  6135. add_gizmo_plugin(Ref<Camera3DGizmoPlugin>(memnew(Camera3DGizmoPlugin)));
  6136. add_gizmo_plugin(Ref<Light3DGizmoPlugin>(memnew(Light3DGizmoPlugin)));
  6137. add_gizmo_plugin(Ref<AudioStreamPlayer3DGizmoPlugin>(memnew(AudioStreamPlayer3DGizmoPlugin)));
  6138. add_gizmo_plugin(Ref<AudioListener3DGizmoPlugin>(memnew(AudioListener3DGizmoPlugin)));
  6139. add_gizmo_plugin(Ref<MeshInstance3DGizmoPlugin>(memnew(MeshInstance3DGizmoPlugin)));
  6140. add_gizmo_plugin(Ref<OccluderInstance3DGizmoPlugin>(memnew(OccluderInstance3DGizmoPlugin)));
  6141. add_gizmo_plugin(Ref<SoftDynamicBody3DGizmoPlugin>(memnew(SoftDynamicBody3DGizmoPlugin)));
  6142. add_gizmo_plugin(Ref<Sprite3DGizmoPlugin>(memnew(Sprite3DGizmoPlugin)));
  6143. add_gizmo_plugin(Ref<Label3DGizmoPlugin>(memnew(Label3DGizmoPlugin)));
  6144. add_gizmo_plugin(Ref<Position3DGizmoPlugin>(memnew(Position3DGizmoPlugin)));
  6145. add_gizmo_plugin(Ref<RayCast3DGizmoPlugin>(memnew(RayCast3DGizmoPlugin)));
  6146. add_gizmo_plugin(Ref<SpringArm3DGizmoPlugin>(memnew(SpringArm3DGizmoPlugin)));
  6147. add_gizmo_plugin(Ref<VehicleWheel3DGizmoPlugin>(memnew(VehicleWheel3DGizmoPlugin)));
  6148. add_gizmo_plugin(Ref<VisibleOnScreenNotifier3DGizmoPlugin>(memnew(VisibleOnScreenNotifier3DGizmoPlugin)));
  6149. add_gizmo_plugin(Ref<GPUParticles3DGizmoPlugin>(memnew(GPUParticles3DGizmoPlugin)));
  6150. add_gizmo_plugin(Ref<GPUParticlesCollision3DGizmoPlugin>(memnew(GPUParticlesCollision3DGizmoPlugin)));
  6151. add_gizmo_plugin(Ref<CPUParticles3DGizmoPlugin>(memnew(CPUParticles3DGizmoPlugin)));
  6152. add_gizmo_plugin(Ref<ReflectionProbeGizmoPlugin>(memnew(ReflectionProbeGizmoPlugin)));
  6153. add_gizmo_plugin(Ref<DecalGizmoPlugin>(memnew(DecalGizmoPlugin)));
  6154. add_gizmo_plugin(Ref<VoxelGIGizmoPlugin>(memnew(VoxelGIGizmoPlugin)));
  6155. add_gizmo_plugin(Ref<LightmapGIGizmoPlugin>(memnew(LightmapGIGizmoPlugin)));
  6156. add_gizmo_plugin(Ref<LightmapProbeGizmoPlugin>(memnew(LightmapProbeGizmoPlugin)));
  6157. add_gizmo_plugin(Ref<CollisionObject3DGizmoPlugin>(memnew(CollisionObject3DGizmoPlugin)));
  6158. add_gizmo_plugin(Ref<CollisionShape3DGizmoPlugin>(memnew(CollisionShape3DGizmoPlugin)));
  6159. add_gizmo_plugin(Ref<CollisionPolygon3DGizmoPlugin>(memnew(CollisionPolygon3DGizmoPlugin)));
  6160. add_gizmo_plugin(Ref<NavigationRegion3DGizmoPlugin>(memnew(NavigationRegion3DGizmoPlugin)));
  6161. add_gizmo_plugin(Ref<Joint3DGizmoPlugin>(memnew(Joint3DGizmoPlugin)));
  6162. add_gizmo_plugin(Ref<PhysicalBone3DGizmoPlugin>(memnew(PhysicalBone3DGizmoPlugin)));
  6163. add_gizmo_plugin(Ref<FogVolumeGizmoPlugin>(memnew(FogVolumeGizmoPlugin)));
  6164. }
  6165. void Node3DEditor::_bind_methods() {
  6166. ClassDB::bind_method("_get_editor_data", &Node3DEditor::_get_editor_data);
  6167. ClassDB::bind_method("_request_gizmo", &Node3DEditor::_request_gizmo);
  6168. ClassDB::bind_method("_set_subgizmo_selection", &Node3DEditor::_set_subgizmo_selection);
  6169. ClassDB::bind_method("_clear_subgizmo_selection", &Node3DEditor::_clear_subgizmo_selection);
  6170. ClassDB::bind_method("_refresh_menu_icons", &Node3DEditor::_refresh_menu_icons);
  6171. ADD_SIGNAL(MethodInfo("transform_key_request"));
  6172. ADD_SIGNAL(MethodInfo("item_lock_status_changed"));
  6173. ADD_SIGNAL(MethodInfo("item_group_status_changed"));
  6174. }
  6175. void Node3DEditor::clear() {
  6176. settings_fov->set_value(EDITOR_GET("editors/3d/default_fov"));
  6177. settings_znear->set_value(EDITOR_GET("editors/3d/default_z_near"));
  6178. settings_zfar->set_value(EDITOR_GET("editors/3d/default_z_far"));
  6179. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  6180. viewports[i]->reset();
  6181. }
  6182. RenderingServer::get_singleton()->instance_set_visible(origin_instance, true);
  6183. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), true);
  6184. for (int i = 0; i < 3; ++i) {
  6185. if (grid_enable[i]) {
  6186. grid_visible[i] = true;
  6187. }
  6188. }
  6189. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  6190. viewports[i]->view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(Node3DEditorViewport::VIEW_AUDIO_LISTENER), i == 0);
  6191. viewports[i]->viewport->set_as_audio_listener_3d(i == 0);
  6192. }
  6193. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID), true);
  6194. }
  6195. void Node3DEditor::_sun_direction_draw() {
  6196. sun_direction->draw_rect(Rect2(Vector2(), sun_direction->get_size()), Color(1, 1, 1, 1));
  6197. Vector3 z_axis = preview_sun->get_transform().basis.get_column(Vector3::AXIS_Z);
  6198. z_axis = get_editor_viewport(0)->camera->get_camera_transform().basis.xform_inv(z_axis);
  6199. sun_direction_material->set_shader_param("sun_direction", Vector3(z_axis.x, -z_axis.y, z_axis.z));
  6200. Color color = sun_color->get_pick_color() * sun_energy->get_value();
  6201. sun_direction_material->set_shader_param("sun_color", Vector3(color.r, color.g, color.b));
  6202. }
  6203. void Node3DEditor::_preview_settings_changed() {
  6204. if (sun_environ_updating) {
  6205. return;
  6206. }
  6207. { // preview sun
  6208. Transform3D t;
  6209. t.basis = Basis(Vector3(sun_rotation.x, sun_rotation.y, 0));
  6210. preview_sun->set_transform(t);
  6211. sun_direction->update();
  6212. preview_sun->set_param(Light3D::PARAM_ENERGY, sun_energy->get_value());
  6213. preview_sun->set_param(Light3D::PARAM_SHADOW_MAX_DISTANCE, sun_max_distance->get_value());
  6214. preview_sun->set_color(sun_color->get_pick_color());
  6215. }
  6216. { //preview env
  6217. sky_material->set_sky_energy(environ_energy->get_value());
  6218. Color hz_color = environ_sky_color->get_pick_color().lerp(environ_ground_color->get_pick_color(), 0.5).lerp(Color(1, 1, 1), 0.5);
  6219. sky_material->set_sky_top_color(environ_sky_color->get_pick_color());
  6220. sky_material->set_sky_horizon_color(hz_color);
  6221. sky_material->set_ground_bottom_color(environ_ground_color->get_pick_color());
  6222. sky_material->set_ground_horizon_color(hz_color);
  6223. environment->set_ssao_enabled(environ_ao_button->is_pressed());
  6224. environment->set_glow_enabled(environ_glow_button->is_pressed());
  6225. environment->set_sdfgi_enabled(environ_gi_button->is_pressed());
  6226. environment->set_tonemapper(environ_tonemap_button->is_pressed() ? Environment::TONE_MAPPER_FILMIC : Environment::TONE_MAPPER_LINEAR);
  6227. }
  6228. }
  6229. void Node3DEditor::_load_default_preview_settings() {
  6230. sun_environ_updating = true;
  6231. // These default rotations place the preview sun at an angular altitude
  6232. // of 60 degrees (must be negative) and an azimuth of 30 degrees clockwise
  6233. // from north (or 150 CCW from south), from north east, facing south west.
  6234. // On any not-tidally-locked planet, a sun would have an angular altitude
  6235. // of 60 degrees as the average of all points on the sphere at noon.
  6236. // The azimuth choice is arbitrary, but ideally shouldn't be on an axis.
  6237. sun_rotation = Vector2(-Math::deg2rad(60.0), Math::deg2rad(150.0));
  6238. sun_angle_altitude->set_value(-Math::rad2deg(sun_rotation.x));
  6239. sun_angle_azimuth->set_value(180.0 - Math::rad2deg(sun_rotation.y));
  6240. sun_direction->update();
  6241. environ_sky_color->set_pick_color(Color(0.385, 0.454, 0.55));
  6242. environ_ground_color->set_pick_color(Color(0.2, 0.169, 0.133));
  6243. environ_energy->set_value(1.0);
  6244. environ_glow_button->set_pressed(true);
  6245. environ_tonemap_button->set_pressed(true);
  6246. environ_ao_button->set_pressed(false);
  6247. environ_gi_button->set_pressed(false);
  6248. sun_max_distance->set_value(250);
  6249. sun_color->set_pick_color(Color(1, 1, 1));
  6250. sun_energy->set_value(1.0);
  6251. sun_environ_updating = false;
  6252. }
  6253. void Node3DEditor::_update_preview_environment() {
  6254. bool disable_light = directional_light_count > 0 || sun_button->is_pressed();
  6255. sun_button->set_disabled(directional_light_count > 0);
  6256. if (disable_light) {
  6257. if (preview_sun->get_parent()) {
  6258. preview_sun->get_parent()->remove_child(preview_sun);
  6259. sun_state->show();
  6260. sun_vb->hide();
  6261. }
  6262. if (directional_light_count > 0) {
  6263. sun_state->set_text(TTR("Scene contains\nDirectionalLight3D.\nPreview disabled."));
  6264. } else {
  6265. sun_state->set_text(TTR("Preview disabled."));
  6266. }
  6267. } else {
  6268. if (!preview_sun->get_parent()) {
  6269. add_child(preview_sun, true);
  6270. sun_state->hide();
  6271. sun_vb->show();
  6272. }
  6273. }
  6274. sun_angle_altitude->set_value(-Math::rad2deg(sun_rotation.x));
  6275. sun_angle_azimuth->set_value(180.0 - Math::rad2deg(sun_rotation.y));
  6276. bool disable_env = world_env_count > 0 || environ_button->is_pressed();
  6277. environ_button->set_disabled(world_env_count > 0);
  6278. if (disable_env) {
  6279. if (preview_environment->get_parent()) {
  6280. preview_environment->get_parent()->remove_child(preview_environment);
  6281. environ_state->show();
  6282. environ_vb->hide();
  6283. }
  6284. if (world_env_count > 0) {
  6285. environ_state->set_text(TTR("Scene contains\nWorldEnvironment.\nPreview disabled."));
  6286. } else {
  6287. environ_state->set_text(TTR("Preview disabled."));
  6288. }
  6289. } else {
  6290. if (!preview_environment->get_parent()) {
  6291. add_child(preview_environment);
  6292. environ_state->hide();
  6293. environ_vb->show();
  6294. }
  6295. }
  6296. }
  6297. void Node3DEditor::_sun_direction_input(const Ref<InputEvent> &p_event) {
  6298. Ref<InputEventMouseMotion> mm = p_event;
  6299. if (mm.is_valid() && (mm->get_button_mask() & MouseButton::MASK_LEFT) != MouseButton::NONE) {
  6300. sun_rotation.x += mm->get_relative().y * (0.02 * EDSCALE);
  6301. sun_rotation.y -= mm->get_relative().x * (0.02 * EDSCALE);
  6302. sun_rotation.x = CLAMP(sun_rotation.x, -Math_TAU / 4, Math_TAU / 4);
  6303. sun_angle_altitude->set_value(-Math::rad2deg(sun_rotation.x));
  6304. sun_angle_azimuth->set_value(180.0 - Math::rad2deg(sun_rotation.y));
  6305. _preview_settings_changed();
  6306. }
  6307. }
  6308. void Node3DEditor::_sun_direction_angle_set() {
  6309. sun_rotation.x = Math::deg2rad(-sun_angle_altitude->get_value());
  6310. sun_rotation.y = Math::deg2rad(180.0 - sun_angle_azimuth->get_value());
  6311. _preview_settings_changed();
  6312. }
  6313. Node3DEditor::Node3DEditor() {
  6314. gizmo.visible = true;
  6315. gizmo.scale = 1.0;
  6316. viewport_environment = Ref<Environment>(memnew(Environment));
  6317. undo_redo = EditorNode::get_singleton()->get_undo_redo();
  6318. VBoxContainer *vbc = this;
  6319. custom_camera = nullptr;
  6320. singleton = this;
  6321. editor_selection = EditorNode::get_singleton()->get_editor_selection();
  6322. editor_selection->add_editor_plugin(this);
  6323. snap_enabled = false;
  6324. snap_key_enabled = false;
  6325. tool_mode = TOOL_MODE_SELECT;
  6326. camera_override_viewport_id = 0;
  6327. hbc_menu = memnew(HBoxContainer);
  6328. vbc->add_child(hbc_menu);
  6329. Vector<Variant> button_binds;
  6330. button_binds.resize(1);
  6331. String sct;
  6332. // Add some margin to the left for better aesthetics.
  6333. // This prevents the first button's hover/pressed effect from "touching" the panel's border,
  6334. // which looks ugly.
  6335. Control *margin_left = memnew(Control);
  6336. hbc_menu->add_child(margin_left);
  6337. margin_left->set_custom_minimum_size(Size2(2, 0) * EDSCALE);
  6338. tool_button[TOOL_MODE_SELECT] = memnew(Button);
  6339. hbc_menu->add_child(tool_button[TOOL_MODE_SELECT]);
  6340. tool_button[TOOL_MODE_SELECT]->set_toggle_mode(true);
  6341. tool_button[TOOL_MODE_SELECT]->set_flat(true);
  6342. tool_button[TOOL_MODE_SELECT]->set_pressed(true);
  6343. button_binds.write[0] = MENU_TOOL_SELECT;
  6344. tool_button[TOOL_MODE_SELECT]->connect("pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed), button_binds);
  6345. tool_button[TOOL_MODE_SELECT]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_select", TTR("Select Mode"), Key::Q));
  6346. tool_button[TOOL_MODE_SELECT]->set_shortcut_context(this);
  6347. tool_button[TOOL_MODE_SELECT]->set_tooltip(keycode_get_string((Key)KeyModifierMask::CMD) + TTR("Drag: Rotate selected node around pivot.") + "\n" + TTR("Alt+RMB: Show list of all nodes at position clicked, including locked."));
  6348. hbc_menu->add_child(memnew(VSeparator));
  6349. tool_button[TOOL_MODE_MOVE] = memnew(Button);
  6350. hbc_menu->add_child(tool_button[TOOL_MODE_MOVE]);
  6351. tool_button[TOOL_MODE_MOVE]->set_toggle_mode(true);
  6352. tool_button[TOOL_MODE_MOVE]->set_flat(true);
  6353. button_binds.write[0] = MENU_TOOL_MOVE;
  6354. tool_button[TOOL_MODE_MOVE]->connect("pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed), button_binds);
  6355. tool_button[TOOL_MODE_MOVE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_move", TTR("Move Mode"), Key::W));
  6356. tool_button[TOOL_MODE_MOVE]->set_shortcut_context(this);
  6357. tool_button[TOOL_MODE_ROTATE] = memnew(Button);
  6358. hbc_menu->add_child(tool_button[TOOL_MODE_ROTATE]);
  6359. tool_button[TOOL_MODE_ROTATE]->set_toggle_mode(true);
  6360. tool_button[TOOL_MODE_ROTATE]->set_flat(true);
  6361. button_binds.write[0] = MENU_TOOL_ROTATE;
  6362. tool_button[TOOL_MODE_ROTATE]->connect("pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed), button_binds);
  6363. tool_button[TOOL_MODE_ROTATE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_rotate", TTR("Rotate Mode"), Key::E));
  6364. tool_button[TOOL_MODE_ROTATE]->set_shortcut_context(this);
  6365. tool_button[TOOL_MODE_SCALE] = memnew(Button);
  6366. hbc_menu->add_child(tool_button[TOOL_MODE_SCALE]);
  6367. tool_button[TOOL_MODE_SCALE]->set_toggle_mode(true);
  6368. tool_button[TOOL_MODE_SCALE]->set_flat(true);
  6369. button_binds.write[0] = MENU_TOOL_SCALE;
  6370. tool_button[TOOL_MODE_SCALE]->connect("pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed), button_binds);
  6371. tool_button[TOOL_MODE_SCALE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_scale", TTR("Scale Mode"), Key::R));
  6372. tool_button[TOOL_MODE_SCALE]->set_shortcut_context(this);
  6373. hbc_menu->add_child(memnew(VSeparator));
  6374. tool_button[TOOL_MODE_LIST_SELECT] = memnew(Button);
  6375. hbc_menu->add_child(tool_button[TOOL_MODE_LIST_SELECT]);
  6376. tool_button[TOOL_MODE_LIST_SELECT]->set_toggle_mode(true);
  6377. tool_button[TOOL_MODE_LIST_SELECT]->set_flat(true);
  6378. button_binds.write[0] = MENU_TOOL_LIST_SELECT;
  6379. tool_button[TOOL_MODE_LIST_SELECT]->connect("pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed), button_binds);
  6380. tool_button[TOOL_MODE_LIST_SELECT]->set_tooltip(TTR("Show list of selectable nodes at position clicked."));
  6381. tool_button[TOOL_LOCK_SELECTED] = memnew(Button);
  6382. hbc_menu->add_child(tool_button[TOOL_LOCK_SELECTED]);
  6383. tool_button[TOOL_LOCK_SELECTED]->set_flat(true);
  6384. button_binds.write[0] = MENU_LOCK_SELECTED;
  6385. tool_button[TOOL_LOCK_SELECTED]->connect("pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed), button_binds);
  6386. tool_button[TOOL_LOCK_SELECTED]->set_tooltip(TTR("Lock selected node, preventing selection and movement."));
  6387. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  6388. tool_button[TOOL_LOCK_SELECTED]->set_shortcut(ED_SHORTCUT("editor/lock_selected_nodes", TTR("Lock Selected Node(s)"), KeyModifierMask::CMD | Key::L));
  6389. tool_button[TOOL_UNLOCK_SELECTED] = memnew(Button);
  6390. hbc_menu->add_child(tool_button[TOOL_UNLOCK_SELECTED]);
  6391. tool_button[TOOL_UNLOCK_SELECTED]->set_flat(true);
  6392. button_binds.write[0] = MENU_UNLOCK_SELECTED;
  6393. tool_button[TOOL_UNLOCK_SELECTED]->connect("pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed), button_binds);
  6394. tool_button[TOOL_UNLOCK_SELECTED]->set_tooltip(TTR("Unlock selected node, allowing selection and movement."));
  6395. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  6396. tool_button[TOOL_UNLOCK_SELECTED]->set_shortcut(ED_SHORTCUT("editor/unlock_selected_nodes", TTR("Unlock Selected Node(s)"), KeyModifierMask::CMD | KeyModifierMask::SHIFT | Key::L));
  6397. tool_button[TOOL_GROUP_SELECTED] = memnew(Button);
  6398. hbc_menu->add_child(tool_button[TOOL_GROUP_SELECTED]);
  6399. tool_button[TOOL_GROUP_SELECTED]->set_flat(true);
  6400. button_binds.write[0] = MENU_GROUP_SELECTED;
  6401. tool_button[TOOL_GROUP_SELECTED]->connect("pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed), button_binds);
  6402. tool_button[TOOL_GROUP_SELECTED]->set_tooltip(TTR("Makes sure the object's children are not selectable."));
  6403. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  6404. tool_button[TOOL_GROUP_SELECTED]->set_shortcut(ED_SHORTCUT("editor/group_selected_nodes", TTR("Group Selected Node(s)"), KeyModifierMask::CMD | Key::G));
  6405. tool_button[TOOL_UNGROUP_SELECTED] = memnew(Button);
  6406. hbc_menu->add_child(tool_button[TOOL_UNGROUP_SELECTED]);
  6407. tool_button[TOOL_UNGROUP_SELECTED]->set_flat(true);
  6408. button_binds.write[0] = MENU_UNGROUP_SELECTED;
  6409. tool_button[TOOL_UNGROUP_SELECTED]->connect("pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed), button_binds);
  6410. tool_button[TOOL_UNGROUP_SELECTED]->set_tooltip(TTR("Restores the object's children's ability to be selected."));
  6411. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  6412. tool_button[TOOL_UNGROUP_SELECTED]->set_shortcut(ED_SHORTCUT("editor/ungroup_selected_nodes", TTR("Ungroup Selected Node(s)"), KeyModifierMask::CMD | KeyModifierMask::SHIFT | Key::G));
  6413. hbc_menu->add_child(memnew(VSeparator));
  6414. tool_option_button[TOOL_OPT_LOCAL_COORDS] = memnew(Button);
  6415. hbc_menu->add_child(tool_option_button[TOOL_OPT_LOCAL_COORDS]);
  6416. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_toggle_mode(true);
  6417. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_flat(true);
  6418. button_binds.write[0] = MENU_TOOL_LOCAL_COORDS;
  6419. tool_option_button[TOOL_OPT_LOCAL_COORDS]->connect("toggled", callable_mp(this, &Node3DEditor::_menu_item_toggled), button_binds);
  6420. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_shortcut(ED_SHORTCUT("spatial_editor/local_coords", TTR("Use Local Space"), Key::T));
  6421. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_shortcut_context(this);
  6422. tool_option_button[TOOL_OPT_USE_SNAP] = memnew(Button);
  6423. hbc_menu->add_child(tool_option_button[TOOL_OPT_USE_SNAP]);
  6424. tool_option_button[TOOL_OPT_USE_SNAP]->set_toggle_mode(true);
  6425. tool_option_button[TOOL_OPT_USE_SNAP]->set_flat(true);
  6426. button_binds.write[0] = MENU_TOOL_USE_SNAP;
  6427. tool_option_button[TOOL_OPT_USE_SNAP]->connect("toggled", callable_mp(this, &Node3DEditor::_menu_item_toggled), button_binds);
  6428. tool_option_button[TOOL_OPT_USE_SNAP]->set_shortcut(ED_SHORTCUT("spatial_editor/snap", TTR("Use Snap"), Key::Y));
  6429. tool_option_button[TOOL_OPT_USE_SNAP]->set_shortcut_context(this);
  6430. hbc_menu->add_child(memnew(VSeparator));
  6431. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA] = memnew(Button);
  6432. hbc_menu->add_child(tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]);
  6433. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_toggle_mode(true);
  6434. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_flat(true);
  6435. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_disabled(true);
  6436. button_binds.write[0] = MENU_TOOL_OVERRIDE_CAMERA;
  6437. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->connect("toggled", callable_mp(this, &Node3DEditor::_menu_item_toggled), button_binds);
  6438. _update_camera_override_button(false);
  6439. hbc_menu->add_child(memnew(VSeparator));
  6440. sun_button = memnew(Button);
  6441. sun_button->set_tooltip(TTR("Toggle preview sunlight.\nIf a DirectionalLight3D node is added to the scene, preview sunlight is disabled."));
  6442. sun_button->set_toggle_mode(true);
  6443. sun_button->set_flat(true);
  6444. sun_button->connect("pressed", callable_mp(this, &Node3DEditor::_update_preview_environment), varray(), CONNECT_DEFERRED);
  6445. sun_button->set_disabled(true);
  6446. hbc_menu->add_child(sun_button);
  6447. environ_button = memnew(Button);
  6448. environ_button->set_tooltip(TTR("Toggle preview environment.\nIf a WorldEnvironment node is added to the scene, preview environment is disabled."));
  6449. environ_button->set_toggle_mode(true);
  6450. environ_button->set_flat(true);
  6451. environ_button->connect("pressed", callable_mp(this, &Node3DEditor::_update_preview_environment), varray(), CONNECT_DEFERRED);
  6452. environ_button->set_disabled(true);
  6453. hbc_menu->add_child(environ_button);
  6454. sun_environ_settings = memnew(Button);
  6455. sun_environ_settings->set_tooltip(TTR("Edit Sun and Environment settings."));
  6456. sun_environ_settings->set_flat(true);
  6457. sun_environ_settings->connect("pressed", callable_mp(this, &Node3DEditor::_sun_environ_settings_pressed));
  6458. hbc_menu->add_child(sun_environ_settings);
  6459. hbc_menu->add_child(memnew(VSeparator));
  6460. // Drag and drop support;
  6461. preview_node = memnew(Node3D);
  6462. preview_bounds = AABB();
  6463. ED_SHORTCUT("spatial_editor/bottom_view", TTR("Bottom View"), KeyModifierMask::ALT + Key::KP_7);
  6464. ED_SHORTCUT("spatial_editor/top_view", TTR("Top View"), Key::KP_7);
  6465. ED_SHORTCUT("spatial_editor/rear_view", TTR("Rear View"), KeyModifierMask::ALT + Key::KP_1);
  6466. ED_SHORTCUT("spatial_editor/front_view", TTR("Front View"), Key::KP_1);
  6467. ED_SHORTCUT("spatial_editor/left_view", TTR("Left View"), KeyModifierMask::ALT + Key::KP_3);
  6468. ED_SHORTCUT("spatial_editor/right_view", TTR("Right View"), Key::KP_3);
  6469. ED_SHORTCUT("spatial_editor/orbit_view_down", TTR("Orbit View Down"), Key::KP_2);
  6470. ED_SHORTCUT("spatial_editor/orbit_view_left", TTR("Orbit View Left"), Key::KP_4);
  6471. ED_SHORTCUT("spatial_editor/orbit_view_right", TTR("Orbit View Right"), Key::KP_6);
  6472. ED_SHORTCUT("spatial_editor/orbit_view_up", TTR("Orbit View Up"), Key::KP_8);
  6473. ED_SHORTCUT("spatial_editor/orbit_view_180", TTR("Orbit View 180"), Key::KP_9);
  6474. ED_SHORTCUT("spatial_editor/switch_perspective_orthogonal", TTR("Switch Perspective/Orthogonal View"), Key::KP_5);
  6475. ED_SHORTCUT("spatial_editor/insert_anim_key", TTR("Insert Animation Key"), Key::K);
  6476. ED_SHORTCUT("spatial_editor/focus_origin", TTR("Focus Origin"), Key::O);
  6477. ED_SHORTCUT("spatial_editor/focus_selection", TTR("Focus Selection"), Key::F);
  6478. ED_SHORTCUT("spatial_editor/align_transform_with_view", TTR("Align Transform with View"), KeyModifierMask::ALT + KeyModifierMask::CMD + Key::M);
  6479. ED_SHORTCUT("spatial_editor/align_rotation_with_view", TTR("Align Rotation with View"), KeyModifierMask::ALT + KeyModifierMask::CMD + Key::F);
  6480. ED_SHORTCUT("spatial_editor/freelook_toggle", TTR("Toggle Freelook"), KeyModifierMask::SHIFT + Key::F);
  6481. ED_SHORTCUT("spatial_editor/decrease_fov", TTR("Decrease Field of View"), KeyModifierMask::CMD + Key::EQUAL); // Usually direct access key for `KEY_PLUS`.
  6482. ED_SHORTCUT("spatial_editor/increase_fov", TTR("Increase Field of View"), KeyModifierMask::CMD + Key::MINUS);
  6483. ED_SHORTCUT("spatial_editor/reset_fov", TTR("Reset Field of View to Default"), KeyModifierMask::CMD + Key::KEY_0);
  6484. PopupMenu *p;
  6485. transform_menu = memnew(MenuButton);
  6486. transform_menu->set_text(TTR("Transform"));
  6487. transform_menu->set_switch_on_hover(true);
  6488. transform_menu->set_shortcut_context(this);
  6489. hbc_menu->add_child(transform_menu);
  6490. p = transform_menu->get_popup();
  6491. p->add_shortcut(ED_SHORTCUT("spatial_editor/snap_to_floor", TTR("Snap Object to Floor"), Key::PAGEDOWN), MENU_SNAP_TO_FLOOR);
  6492. p->add_shortcut(ED_SHORTCUT("spatial_editor/transform_dialog", TTR("Transform Dialog...")), MENU_TRANSFORM_DIALOG);
  6493. p->add_separator();
  6494. p->add_shortcut(ED_SHORTCUT("spatial_editor/configure_snap", TTR("Configure Snap...")), MENU_TRANSFORM_CONFIGURE_SNAP);
  6495. p->connect("id_pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed));
  6496. view_menu = memnew(MenuButton);
  6497. // TRANSLATORS: Noun, name of the 2D/3D View menus.
  6498. view_menu->set_text(TTR("View"));
  6499. view_menu->set_switch_on_hover(true);
  6500. view_menu->set_shortcut_context(this);
  6501. hbc_menu->add_child(view_menu);
  6502. hbc_menu->add_child(memnew(VSeparator));
  6503. context_menu_container = memnew(PanelContainer);
  6504. hbc_context_menu = memnew(HBoxContainer);
  6505. context_menu_container->add_child(hbc_context_menu);
  6506. // Use a custom stylebox to make contextual menu items stand out from the rest.
  6507. // This helps with editor usability as contextual menu items change when selecting nodes,
  6508. // even though it may not be immediately obvious at first.
  6509. hbc_menu->add_child(context_menu_container);
  6510. _update_context_menu_stylebox();
  6511. // Get the view menu popup and have it stay open when a checkable item is selected
  6512. p = view_menu->get_popup();
  6513. p->set_hide_on_checkable_item_selection(false);
  6514. accept = memnew(AcceptDialog);
  6515. EditorNode::get_singleton()->get_gui_base()->add_child(accept);
  6516. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/1_viewport", TTR("1 Viewport"), KeyModifierMask::CMD + Key::KEY_1), MENU_VIEW_USE_1_VIEWPORT);
  6517. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports", TTR("2 Viewports"), KeyModifierMask::CMD + Key::KEY_2), MENU_VIEW_USE_2_VIEWPORTS);
  6518. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports_alt", TTR("2 Viewports (Alt)"), KeyModifierMask::ALT + KeyModifierMask::CMD + Key::KEY_2), MENU_VIEW_USE_2_VIEWPORTS_ALT);
  6519. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports", TTR("3 Viewports"), KeyModifierMask::CMD + Key::KEY_3), MENU_VIEW_USE_3_VIEWPORTS);
  6520. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports_alt", TTR("3 Viewports (Alt)"), KeyModifierMask::ALT + KeyModifierMask::CMD + Key::KEY_3), MENU_VIEW_USE_3_VIEWPORTS_ALT);
  6521. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/4_viewports", TTR("4 Viewports"), KeyModifierMask::CMD + Key::KEY_4), MENU_VIEW_USE_4_VIEWPORTS);
  6522. p->add_separator();
  6523. p->add_submenu_item(TTR("Gizmos"), "GizmosMenu");
  6524. p->add_separator();
  6525. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_origin", TTR("View Origin")), MENU_VIEW_ORIGIN);
  6526. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_grid", TTR("View Grid"), Key::NUMBERSIGN), MENU_VIEW_GRID);
  6527. p->add_separator();
  6528. p->add_shortcut(ED_SHORTCUT("spatial_editor/settings", TTR("Settings...")), MENU_VIEW_CAMERA_SETTINGS);
  6529. p->set_item_checked(p->get_item_index(MENU_VIEW_ORIGIN), true);
  6530. p->set_item_checked(p->get_item_index(MENU_VIEW_GRID), true);
  6531. p->connect("id_pressed", callable_mp(this, &Node3DEditor::_menu_item_pressed));
  6532. gizmos_menu = memnew(PopupMenu);
  6533. p->add_child(gizmos_menu);
  6534. gizmos_menu->set_name("GizmosMenu");
  6535. gizmos_menu->set_hide_on_checkable_item_selection(false);
  6536. gizmos_menu->connect("id_pressed", callable_mp(this, &Node3DEditor::_menu_gizmo_toggled));
  6537. /* REST OF MENU */
  6538. left_panel_split = memnew(HSplitContainer);
  6539. left_panel_split->set_v_size_flags(SIZE_EXPAND_FILL);
  6540. vbc->add_child(left_panel_split);
  6541. right_panel_split = memnew(HSplitContainer);
  6542. right_panel_split->set_v_size_flags(SIZE_EXPAND_FILL);
  6543. left_panel_split->add_child(right_panel_split);
  6544. shader_split = memnew(VSplitContainer);
  6545. shader_split->set_h_size_flags(SIZE_EXPAND_FILL);
  6546. right_panel_split->add_child(shader_split);
  6547. viewport_base = memnew(Node3DEditorViewportContainer);
  6548. shader_split->add_child(viewport_base);
  6549. viewport_base->set_v_size_flags(SIZE_EXPAND_FILL);
  6550. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  6551. viewports[i] = memnew(Node3DEditorViewport(this, i));
  6552. viewports[i]->connect("toggle_maximize_view", callable_mp(this, &Node3DEditor::_toggle_maximize_view));
  6553. viewports[i]->connect("clicked", callable_mp(this, &Node3DEditor::_update_camera_override_viewport));
  6554. viewports[i]->assign_pending_data_pointers(preview_node, &preview_bounds, accept);
  6555. viewport_base->add_child(viewports[i]);
  6556. }
  6557. /* SNAP DIALOG */
  6558. snap_translate_value = 1;
  6559. snap_rotate_value = 15;
  6560. snap_scale_value = 10;
  6561. snap_dialog = memnew(ConfirmationDialog);
  6562. snap_dialog->set_title(TTR("Snap Settings"));
  6563. add_child(snap_dialog);
  6564. snap_dialog->connect("confirmed", callable_mp(this, &Node3DEditor::_snap_changed));
  6565. snap_dialog->get_cancel_button()->connect("pressed", callable_mp(this, &Node3DEditor::_snap_update));
  6566. VBoxContainer *snap_dialog_vbc = memnew(VBoxContainer);
  6567. snap_dialog->add_child(snap_dialog_vbc);
  6568. snap_translate = memnew(LineEdit);
  6569. snap_dialog_vbc->add_margin_child(TTR("Translate Snap:"), snap_translate);
  6570. snap_rotate = memnew(LineEdit);
  6571. snap_dialog_vbc->add_margin_child(TTR("Rotate Snap (deg.):"), snap_rotate);
  6572. snap_scale = memnew(LineEdit);
  6573. snap_dialog_vbc->add_margin_child(TTR("Scale Snap (%):"), snap_scale);
  6574. _snap_update();
  6575. /* SETTINGS DIALOG */
  6576. settings_dialog = memnew(ConfirmationDialog);
  6577. settings_dialog->set_title(TTR("Viewport Settings"));
  6578. add_child(settings_dialog);
  6579. settings_vbc = memnew(VBoxContainer);
  6580. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  6581. settings_dialog->add_child(settings_vbc);
  6582. settings_fov = memnew(SpinBox);
  6583. settings_fov->set_max(MAX_FOV);
  6584. settings_fov->set_min(MIN_FOV);
  6585. settings_fov->set_step(0.1);
  6586. settings_fov->set_value(EDITOR_GET("editors/3d/default_fov"));
  6587. settings_vbc->add_margin_child(TTR("Perspective FOV (deg.):"), settings_fov);
  6588. settings_znear = memnew(SpinBox);
  6589. settings_znear->set_max(MAX_Z);
  6590. settings_znear->set_min(MIN_Z);
  6591. settings_znear->set_step(0.01);
  6592. settings_znear->set_value(EDITOR_GET("editors/3d/default_z_near"));
  6593. settings_vbc->add_margin_child(TTR("View Z-Near:"), settings_znear);
  6594. settings_zfar = memnew(SpinBox);
  6595. settings_zfar->set_max(MAX_Z);
  6596. settings_zfar->set_min(MIN_Z);
  6597. settings_zfar->set_step(0.1);
  6598. settings_zfar->set_value(EDITOR_GET("editors/3d/default_z_far"));
  6599. settings_vbc->add_margin_child(TTR("View Z-Far:"), settings_zfar);
  6600. for (uint32_t i = 0; i < VIEWPORTS_COUNT; ++i) {
  6601. settings_dialog->connect("confirmed", callable_mp(viewports[i], &Node3DEditorViewport::_view_settings_confirmed), varray(0.0));
  6602. }
  6603. /* XFORM DIALOG */
  6604. xform_dialog = memnew(ConfirmationDialog);
  6605. xform_dialog->set_title(TTR("Transform Change"));
  6606. add_child(xform_dialog);
  6607. VBoxContainer *xform_vbc = memnew(VBoxContainer);
  6608. xform_dialog->add_child(xform_vbc);
  6609. Label *l = memnew(Label);
  6610. l->set_text(TTR("Translate:"));
  6611. xform_vbc->add_child(l);
  6612. HBoxContainer *xform_hbc = memnew(HBoxContainer);
  6613. xform_vbc->add_child(xform_hbc);
  6614. for (int i = 0; i < 3; i++) {
  6615. xform_translate[i] = memnew(LineEdit);
  6616. xform_translate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  6617. xform_hbc->add_child(xform_translate[i]);
  6618. }
  6619. l = memnew(Label);
  6620. l->set_text(TTR("Rotate (deg.):"));
  6621. xform_vbc->add_child(l);
  6622. xform_hbc = memnew(HBoxContainer);
  6623. xform_vbc->add_child(xform_hbc);
  6624. for (int i = 0; i < 3; i++) {
  6625. xform_rotate[i] = memnew(LineEdit);
  6626. xform_rotate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  6627. xform_hbc->add_child(xform_rotate[i]);
  6628. }
  6629. l = memnew(Label);
  6630. l->set_text(TTR("Scale (ratio):"));
  6631. xform_vbc->add_child(l);
  6632. xform_hbc = memnew(HBoxContainer);
  6633. xform_vbc->add_child(xform_hbc);
  6634. for (int i = 0; i < 3; i++) {
  6635. xform_scale[i] = memnew(LineEdit);
  6636. xform_scale[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  6637. xform_hbc->add_child(xform_scale[i]);
  6638. }
  6639. l = memnew(Label);
  6640. l->set_text(TTR("Transform Type"));
  6641. xform_vbc->add_child(l);
  6642. xform_type = memnew(OptionButton);
  6643. xform_type->set_h_size_flags(SIZE_EXPAND_FILL);
  6644. xform_type->add_item(TTR("Pre"));
  6645. xform_type->add_item(TTR("Post"));
  6646. xform_vbc->add_child(xform_type);
  6647. xform_dialog->connect("confirmed", callable_mp(this, &Node3DEditor::_xform_dialog_action));
  6648. selected = nullptr;
  6649. set_process_shortcut_input(true);
  6650. add_to_group("_spatial_editor_group");
  6651. EDITOR_DEF("editors/3d/manipulator_gizmo_size", 80);
  6652. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/3d/manipulator_gizmo_size", PROPERTY_HINT_RANGE, "16,160,1"));
  6653. EDITOR_DEF("editors/3d/manipulator_gizmo_opacity", 0.9);
  6654. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::FLOAT, "editors/3d/manipulator_gizmo_opacity", PROPERTY_HINT_RANGE, "0,1,0.01"));
  6655. EDITOR_DEF("editors/3d/navigation/show_viewport_rotation_gizmo", true);
  6656. current_hover_gizmo_handle = -1;
  6657. current_hover_gizmo_handle_secondary = false;
  6658. {
  6659. //sun popup
  6660. sun_environ_popup = memnew(PopupPanel);
  6661. add_child(sun_environ_popup);
  6662. HBoxContainer *sun_environ_hb = memnew(HBoxContainer);
  6663. sun_environ_popup->add_child(sun_environ_hb);
  6664. sun_vb = memnew(VBoxContainer);
  6665. sun_environ_hb->add_child(sun_vb);
  6666. sun_vb->set_custom_minimum_size(Size2(200 * EDSCALE, 0));
  6667. sun_vb->hide();
  6668. sun_title = memnew(Label);
  6669. sun_title->set_theme_type_variation("HeaderSmall");
  6670. sun_vb->add_child(sun_title);
  6671. sun_title->set_text(TTR("Preview Sun"));
  6672. sun_title->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  6673. CenterContainer *sun_direction_center = memnew(CenterContainer);
  6674. sun_direction = memnew(Control);
  6675. sun_direction->set_custom_minimum_size(Size2i(128, 128) * EDSCALE);
  6676. sun_direction_center->add_child(sun_direction);
  6677. sun_vb->add_margin_child(TTR("Sun Direction"), sun_direction_center);
  6678. sun_direction->connect("gui_input", callable_mp(this, &Node3DEditor::_sun_direction_input));
  6679. sun_direction->connect("draw", callable_mp(this, &Node3DEditor::_sun_direction_draw));
  6680. sun_direction->set_default_cursor_shape(CURSOR_MOVE);
  6681. sun_direction_shader.instantiate();
  6682. sun_direction_shader->set_code(R"(
  6683. // 3D editor Preview Sun direction shader.
  6684. shader_type canvas_item;
  6685. uniform vec3 sun_direction;
  6686. uniform vec3 sun_color;
  6687. void fragment() {
  6688. vec3 n;
  6689. n.xy = UV * 2.0 - 1.0;
  6690. n.z = sqrt(max(0.0, 1.0 - dot(n.xy, n.xy)));
  6691. COLOR.rgb = dot(n, sun_direction) * sun_color;
  6692. COLOR.a = 1.0 - smoothstep(0.99, 1.0, length(n.xy));
  6693. }
  6694. )");
  6695. sun_direction_material.instantiate();
  6696. sun_direction_material->set_shader(sun_direction_shader);
  6697. sun_direction_material->set_shader_param("sun_direction", Vector3(0, 0, 1));
  6698. sun_direction_material->set_shader_param("sun_color", Vector3(1, 1, 1));
  6699. sun_direction->set_material(sun_direction_material);
  6700. HBoxContainer *sun_angle_hbox = memnew(HBoxContainer);
  6701. VBoxContainer *sun_angle_altitude_vbox = memnew(VBoxContainer);
  6702. Label *sun_angle_altitude_label = memnew(Label);
  6703. sun_angle_altitude_label->set_text(TTR("Angular Altitude"));
  6704. sun_angle_altitude_vbox->add_child(sun_angle_altitude_label);
  6705. sun_angle_altitude = memnew(EditorSpinSlider);
  6706. sun_angle_altitude->set_max(90);
  6707. sun_angle_altitude->set_min(-90);
  6708. sun_angle_altitude->set_step(0.1);
  6709. sun_angle_altitude->connect("value_changed", callable_mp(this, &Node3DEditor::_sun_direction_angle_set).unbind(1));
  6710. sun_angle_altitude_vbox->add_child(sun_angle_altitude);
  6711. sun_angle_hbox->add_child(sun_angle_altitude_vbox);
  6712. VBoxContainer *sun_angle_azimuth_vbox = memnew(VBoxContainer);
  6713. sun_angle_azimuth_vbox->set_custom_minimum_size(Vector2(100, 0));
  6714. Label *sun_angle_azimuth_label = memnew(Label);
  6715. sun_angle_azimuth_label->set_text(TTR("Azimuth"));
  6716. sun_angle_azimuth_vbox->add_child(sun_angle_azimuth_label);
  6717. sun_angle_azimuth = memnew(EditorSpinSlider);
  6718. sun_angle_azimuth->set_max(180);
  6719. sun_angle_azimuth->set_min(-180);
  6720. sun_angle_azimuth->set_step(0.1);
  6721. sun_angle_azimuth->set_allow_greater(true);
  6722. sun_angle_azimuth->set_allow_lesser(true);
  6723. sun_angle_azimuth->connect("value_changed", callable_mp(this, &Node3DEditor::_sun_direction_angle_set).unbind(1));
  6724. sun_angle_azimuth_vbox->add_child(sun_angle_azimuth);
  6725. sun_angle_hbox->add_child(sun_angle_azimuth_vbox);
  6726. sun_angle_hbox->add_theme_constant_override("separation", 10);
  6727. sun_vb->add_child(sun_angle_hbox);
  6728. sun_color = memnew(ColorPickerButton);
  6729. sun_color->set_edit_alpha(false);
  6730. sun_vb->add_margin_child(TTR("Sun Color"), sun_color);
  6731. sun_color->connect("color_changed", callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  6732. sun_energy = memnew(EditorSpinSlider);
  6733. sun_vb->add_margin_child(TTR("Sun Energy"), sun_energy);
  6734. sun_energy->connect("value_changed", callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  6735. sun_energy->set_max(64.0);
  6736. sun_max_distance = memnew(EditorSpinSlider);
  6737. sun_vb->add_margin_child(TTR("Shadow Max Distance"), sun_max_distance);
  6738. sun_max_distance->connect("value_changed", callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  6739. sun_max_distance->set_min(1);
  6740. sun_max_distance->set_max(4096);
  6741. sun_add_to_scene = memnew(Button);
  6742. sun_add_to_scene->set_text(TTR("Add Sun to Scene"));
  6743. sun_add_to_scene->set_tooltip(TTR("Adds a DirectionalLight3D node matching the preview sun settings to the current scene.\nHold Shift while clicking to also add the preview environment to the current scene."));
  6744. sun_add_to_scene->connect("pressed", callable_mp(this, &Node3DEditor::_add_sun_to_scene), varray(false));
  6745. sun_vb->add_spacer();
  6746. sun_vb->add_child(sun_add_to_scene);
  6747. sun_state = memnew(Label);
  6748. sun_environ_hb->add_child(sun_state);
  6749. sun_state->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  6750. sun_state->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  6751. sun_state->set_h_size_flags(SIZE_EXPAND_FILL);
  6752. VSeparator *sc = memnew(VSeparator);
  6753. sc->set_custom_minimum_size(Size2(50 * EDSCALE, 0));
  6754. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  6755. sun_environ_hb->add_child(sc);
  6756. environ_vb = memnew(VBoxContainer);
  6757. sun_environ_hb->add_child(environ_vb);
  6758. environ_vb->set_custom_minimum_size(Size2(200 * EDSCALE, 0));
  6759. environ_vb->hide();
  6760. environ_title = memnew(Label);
  6761. environ_title->set_theme_type_variation("HeaderSmall");
  6762. environ_vb->add_child(environ_title);
  6763. environ_title->set_text(TTR("Preview Environment"));
  6764. environ_title->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  6765. environ_sky_color = memnew(ColorPickerButton);
  6766. environ_sky_color->set_edit_alpha(false);
  6767. environ_sky_color->connect("color_changed", callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  6768. environ_vb->add_margin_child(TTR("Sky Color"), environ_sky_color);
  6769. environ_ground_color = memnew(ColorPickerButton);
  6770. environ_ground_color->connect("color_changed", callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  6771. environ_ground_color->set_edit_alpha(false);
  6772. environ_vb->add_margin_child(TTR("Ground Color"), environ_ground_color);
  6773. environ_energy = memnew(EditorSpinSlider);
  6774. environ_energy->connect("value_changed", callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  6775. environ_energy->set_max(8.0);
  6776. environ_vb->add_margin_child(TTR("Sky Energy"), environ_energy);
  6777. HBoxContainer *fx_vb = memnew(HBoxContainer);
  6778. fx_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  6779. environ_ao_button = memnew(Button);
  6780. environ_ao_button->set_text(TTR("AO"));
  6781. environ_ao_button->set_toggle_mode(true);
  6782. environ_ao_button->connect("pressed", callable_mp(this, &Node3DEditor::_preview_settings_changed), varray(), CONNECT_DEFERRED);
  6783. fx_vb->add_child(environ_ao_button);
  6784. environ_glow_button = memnew(Button);
  6785. environ_glow_button->set_text(TTR("Glow"));
  6786. environ_glow_button->set_toggle_mode(true);
  6787. environ_glow_button->connect("pressed", callable_mp(this, &Node3DEditor::_preview_settings_changed), varray(), CONNECT_DEFERRED);
  6788. fx_vb->add_child(environ_glow_button);
  6789. environ_tonemap_button = memnew(Button);
  6790. environ_tonemap_button->set_text(TTR("Tonemap"));
  6791. environ_tonemap_button->set_toggle_mode(true);
  6792. environ_tonemap_button->connect("pressed", callable_mp(this, &Node3DEditor::_preview_settings_changed), varray(), CONNECT_DEFERRED);
  6793. fx_vb->add_child(environ_tonemap_button);
  6794. environ_gi_button = memnew(Button);
  6795. environ_gi_button->set_text(TTR("GI"));
  6796. environ_gi_button->set_toggle_mode(true);
  6797. environ_gi_button->connect("pressed", callable_mp(this, &Node3DEditor::_preview_settings_changed), varray(), CONNECT_DEFERRED);
  6798. fx_vb->add_child(environ_gi_button);
  6799. environ_vb->add_margin_child(TTR("Post Process"), fx_vb);
  6800. environ_add_to_scene = memnew(Button);
  6801. environ_add_to_scene->set_text(TTR("Add Environment to Scene"));
  6802. environ_add_to_scene->set_tooltip(TTR("Adds a WorldEnvironment node matching the preview environment settings to the current scene.\nHold Shift while clicking to also add the preview sun to the current scene."));
  6803. environ_add_to_scene->connect("pressed", callable_mp(this, &Node3DEditor::_add_environment_to_scene), varray(false));
  6804. environ_vb->add_spacer();
  6805. environ_vb->add_child(environ_add_to_scene);
  6806. environ_state = memnew(Label);
  6807. sun_environ_hb->add_child(environ_state);
  6808. environ_state->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  6809. environ_state->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  6810. environ_state->set_h_size_flags(SIZE_EXPAND_FILL);
  6811. preview_sun = memnew(DirectionalLight3D);
  6812. preview_sun->set_shadow(true);
  6813. preview_sun->set_shadow_mode(DirectionalLight3D::SHADOW_PARALLEL_4_SPLITS);
  6814. preview_environment = memnew(WorldEnvironment);
  6815. environment.instantiate();
  6816. preview_environment->set_environment(environment);
  6817. Ref<Sky> sky;
  6818. sky.instantiate();
  6819. sky_material.instantiate();
  6820. sky->set_material(sky_material);
  6821. environment->set_sky(sky);
  6822. environment->set_background(Environment::BG_SKY);
  6823. _load_default_preview_settings();
  6824. _preview_settings_changed();
  6825. }
  6826. }
  6827. Node3DEditor::~Node3DEditor() {
  6828. memdelete(preview_node);
  6829. }
  6830. void Node3DEditorPlugin::make_visible(bool p_visible) {
  6831. if (p_visible) {
  6832. spatial_editor->show();
  6833. spatial_editor->set_process(true);
  6834. } else {
  6835. spatial_editor->hide();
  6836. spatial_editor->set_process(false);
  6837. }
  6838. }
  6839. void Node3DEditorPlugin::edit(Object *p_object) {
  6840. spatial_editor->edit(Object::cast_to<Node3D>(p_object));
  6841. }
  6842. bool Node3DEditorPlugin::handles(Object *p_object) const {
  6843. if (p_object->is_class("Node3D")) {
  6844. return true;
  6845. } else {
  6846. // This ensures that gizmos are cleared when selecting a non-Node3D node.
  6847. const_cast<Node3DEditorPlugin *>(this)->edit((Object *)nullptr);
  6848. return false;
  6849. }
  6850. }
  6851. Dictionary Node3DEditorPlugin::get_state() const {
  6852. return spatial_editor->get_state();
  6853. }
  6854. void Node3DEditorPlugin::set_state(const Dictionary &p_state) {
  6855. spatial_editor->set_state(p_state);
  6856. }
  6857. Vector3 Node3DEditor::snap_point(Vector3 p_target, Vector3 p_start) const {
  6858. if (is_snap_enabled()) {
  6859. p_target.x = Math::snap_scalar(0.0, get_translate_snap(), p_target.x);
  6860. p_target.y = Math::snap_scalar(0.0, get_translate_snap(), p_target.y);
  6861. p_target.z = Math::snap_scalar(0.0, get_translate_snap(), p_target.z);
  6862. }
  6863. return p_target;
  6864. }
  6865. bool Node3DEditor::is_gizmo_visible() const {
  6866. if (selected) {
  6867. return gizmo.visible && selected->is_transform_gizmo_visible();
  6868. }
  6869. return gizmo.visible;
  6870. }
  6871. double Node3DEditor::get_translate_snap() const {
  6872. double snap_value;
  6873. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  6874. snap_value = snap_translate->get_text().to_float() / 10.0;
  6875. } else {
  6876. snap_value = snap_translate->get_text().to_float();
  6877. }
  6878. return snap_value;
  6879. }
  6880. double Node3DEditor::get_rotate_snap() const {
  6881. double snap_value;
  6882. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  6883. snap_value = snap_rotate->get_text().to_float() / 3.0;
  6884. } else {
  6885. snap_value = snap_rotate->get_text().to_float();
  6886. }
  6887. return snap_value;
  6888. }
  6889. double Node3DEditor::get_scale_snap() const {
  6890. double snap_value;
  6891. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  6892. snap_value = snap_scale->get_text().to_float() / 2.0;
  6893. } else {
  6894. snap_value = snap_scale->get_text().to_float();
  6895. }
  6896. return snap_value;
  6897. }
  6898. struct _GizmoPluginPriorityComparator {
  6899. bool operator()(const Ref<EditorNode3DGizmoPlugin> &p_a, const Ref<EditorNode3DGizmoPlugin> &p_b) const {
  6900. if (p_a->get_priority() == p_b->get_priority()) {
  6901. return p_a->get_gizmo_name() < p_b->get_gizmo_name();
  6902. }
  6903. return p_a->get_priority() > p_b->get_priority();
  6904. }
  6905. };
  6906. struct _GizmoPluginNameComparator {
  6907. bool operator()(const Ref<EditorNode3DGizmoPlugin> &p_a, const Ref<EditorNode3DGizmoPlugin> &p_b) const {
  6908. return p_a->get_gizmo_name() < p_b->get_gizmo_name();
  6909. }
  6910. };
  6911. void Node3DEditor::add_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin) {
  6912. ERR_FAIL_NULL(p_plugin.ptr());
  6913. gizmo_plugins_by_priority.push_back(p_plugin);
  6914. gizmo_plugins_by_priority.sort_custom<_GizmoPluginPriorityComparator>();
  6915. gizmo_plugins_by_name.push_back(p_plugin);
  6916. gizmo_plugins_by_name.sort_custom<_GizmoPluginNameComparator>();
  6917. _update_gizmos_menu();
  6918. }
  6919. void Node3DEditor::remove_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin) {
  6920. gizmo_plugins_by_priority.erase(p_plugin);
  6921. gizmo_plugins_by_name.erase(p_plugin);
  6922. _update_gizmos_menu();
  6923. }
  6924. Node3DEditorPlugin::Node3DEditorPlugin() {
  6925. spatial_editor = memnew(Node3DEditor);
  6926. spatial_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  6927. EditorNode::get_singleton()->get_main_control()->add_child(spatial_editor);
  6928. spatial_editor->hide();
  6929. }
  6930. Node3DEditorPlugin::~Node3DEditorPlugin() {
  6931. }