spatial_editor_plugin.cpp 254 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115
  1. /*************************************************************************/
  2. /* spatial_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 "spatial_editor_plugin.h"
  31. #include "core/math/camera_matrix.h"
  32. #include "core/os/input.h"
  33. #include "core/os/keyboard.h"
  34. #include "core/print_string.h"
  35. #include "core/project_settings.h"
  36. #include "core/sort_array.h"
  37. #include "editor/editor_node.h"
  38. #include "editor/editor_scale.h"
  39. #include "editor/editor_settings.h"
  40. #include "editor/plugins/animation_player_editor_plugin.h"
  41. #include "editor/plugins/script_editor_plugin.h"
  42. #include "editor/script_editor_debugger.h"
  43. #include "editor/spatial_editor_gizmos.h"
  44. #include "scene/3d/camera.h"
  45. #include "scene/3d/collision_shape.h"
  46. #include "scene/3d/mesh_instance.h"
  47. #include "scene/3d/physics_body.h"
  48. #include "scene/3d/room_manager.h"
  49. #include "scene/3d/visual_instance.h"
  50. #include "scene/gui/viewport_container.h"
  51. #include "scene/resources/packed_scene.h"
  52. #include "scene/resources/surface_tool.h"
  53. #define DISTANCE_DEFAULT 4
  54. #define GIZMO_ARROW_SIZE 0.35
  55. #define GIZMO_RING_HALF_WIDTH 0.1
  56. #define GIZMO_SCALE_DEFAULT 0.15
  57. #define GIZMO_PLANE_SIZE 0.2
  58. #define GIZMO_PLANE_DST 0.3
  59. #define GIZMO_CIRCLE_SIZE 1.1
  60. #define GIZMO_SCALE_OFFSET (GIZMO_CIRCLE_SIZE + 0.3)
  61. #define GIZMO_ARROW_OFFSET (GIZMO_CIRCLE_SIZE + 0.3)
  62. #define ZOOM_FREELOOK_MIN 0.01
  63. #define ZOOM_FREELOOK_MULTIPLIER 1.08
  64. #define ZOOM_FREELOOK_INDICATOR_DELAY_S 1.5
  65. #define ZOOM_FREELOOK_MAX 10'000
  66. #define MIN_Z 0.01
  67. #define MAX_Z 1000000.0
  68. #define MIN_FOV 0.01
  69. #define MAX_FOV 179
  70. void ViewportRotationControl::_notification(int p_what) {
  71. if (p_what == NOTIFICATION_ENTER_TREE) {
  72. axis_menu_options.clear();
  73. axis_menu_options.push_back(SpatialEditorViewport::VIEW_RIGHT);
  74. axis_menu_options.push_back(SpatialEditorViewport::VIEW_TOP);
  75. axis_menu_options.push_back(SpatialEditorViewport::VIEW_REAR);
  76. axis_menu_options.push_back(SpatialEditorViewport::VIEW_LEFT);
  77. axis_menu_options.push_back(SpatialEditorViewport::VIEW_BOTTOM);
  78. axis_menu_options.push_back(SpatialEditorViewport::VIEW_FRONT);
  79. axis_colors.clear();
  80. axis_colors.push_back(get_color("axis_x_color", "Editor"));
  81. axis_colors.push_back(get_color("axis_y_color", "Editor"));
  82. axis_colors.push_back(get_color("axis_z_color", "Editor"));
  83. update();
  84. if (!is_connected("mouse_exited", this, "_on_mouse_exited")) {
  85. connect("mouse_exited", this, "_on_mouse_exited");
  86. }
  87. }
  88. if (p_what == NOTIFICATION_DRAW && viewport != nullptr) {
  89. _draw();
  90. }
  91. }
  92. void ViewportRotationControl::_draw() {
  93. Vector2i center = get_size() / 2.0;
  94. float radius = get_size().x / 2.0;
  95. if (focused_axis > -2 || orbiting) {
  96. draw_circle(center, radius, Color(0.5, 0.5, 0.5, 0.25));
  97. }
  98. Vector<Axis2D> axis_to_draw;
  99. _get_sorted_axis(axis_to_draw);
  100. for (int i = 0; i < axis_to_draw.size(); ++i) {
  101. _draw_axis(axis_to_draw[i]);
  102. }
  103. }
  104. void ViewportRotationControl::_draw_axis(const Axis2D &p_axis) {
  105. const bool focused = focused_axis == p_axis.axis;
  106. const bool positive = p_axis.axis < 3;
  107. const int direction = p_axis.axis % 3;
  108. const Color axis_color = axis_colors[direction];
  109. const double alpha = focused ? 1.0 : ((p_axis.z_axis + 1.0) / 2.0) * 0.5 + 0.5;
  110. const Color c = focused ? Color(0.9, 0.9, 0.9) : Color(axis_color.r, axis_color.g, axis_color.b, alpha);
  111. if (positive) {
  112. // Draw axis lines for the positive axes.
  113. const Vector2i center = get_size() / 2.0;
  114. draw_line(center, p_axis.screen_point, c, 1.5 * EDSCALE, true);
  115. draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS, c);
  116. // Draw the axis letter for the positive axes.
  117. const String axis_name = direction == 0 ? "X" : (direction == 1 ? "Y" : "Z");
  118. draw_char(get_font("rotation_control", "EditorFonts"), p_axis.screen_point + Vector2(-4.0, 5.0) * EDSCALE, axis_name, "", Color(0.0, 0.0, 0.0, alpha));
  119. } else {
  120. // Draw an outline around the negative axes.
  121. draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS, c);
  122. draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS * 0.8, c.darkened(0.4));
  123. }
  124. }
  125. void ViewportRotationControl::_get_sorted_axis(Vector<Axis2D> &r_axis) {
  126. Vector2i center = get_size() / 2.0;
  127. float radius = get_size().x / 2.0;
  128. float axis_radius = radius - AXIS_CIRCLE_RADIUS - 2.0 * EDSCALE;
  129. Basis camera_basis = viewport->to_camera_transform(viewport->cursor).get_basis().inverse();
  130. for (int i = 0; i < 3; ++i) {
  131. Vector3 axis_3d = camera_basis.get_axis(i);
  132. Vector2i axis_vector = Vector2(axis_3d.x, -axis_3d.y) * axis_radius;
  133. if (Math::abs(axis_3d.z) < 1.0) {
  134. Axis2D pos_axis;
  135. pos_axis.axis = i;
  136. pos_axis.screen_point = center + axis_vector;
  137. pos_axis.z_axis = axis_3d.z;
  138. r_axis.push_back(pos_axis);
  139. Axis2D neg_axis;
  140. neg_axis.axis = i + 3;
  141. neg_axis.screen_point = center - axis_vector;
  142. neg_axis.z_axis = -axis_3d.z;
  143. r_axis.push_back(neg_axis);
  144. } else {
  145. // Special case when the camera is aligned with one axis
  146. Axis2D axis;
  147. axis.axis = i + (axis_3d.z < 0 ? 0 : 3);
  148. axis.screen_point = center;
  149. axis.z_axis = 1.0;
  150. r_axis.push_back(axis);
  151. }
  152. }
  153. r_axis.sort_custom<Axis2DCompare>();
  154. }
  155. void ViewportRotationControl::_gui_input(Ref<InputEvent> p_event) {
  156. const Ref<InputEventMouseButton> mb = p_event;
  157. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  158. Vector2 pos = mb->get_position();
  159. if (mb->is_pressed()) {
  160. if (pos.distance_to(get_size() / 2.0) < get_size().x / 2.0) {
  161. orbiting = true;
  162. }
  163. } else {
  164. if (focused_axis > -1) {
  165. viewport->_menu_option(axis_menu_options[focused_axis]);
  166. _update_focus();
  167. }
  168. orbiting = false;
  169. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
  170. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  171. Input::get_singleton()->warp_mouse_position(orbiting_mouse_start);
  172. }
  173. }
  174. }
  175. const Ref<InputEventMouseMotion> mm = p_event;
  176. if (mm.is_valid()) {
  177. if (orbiting) {
  178. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) {
  179. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  180. orbiting_mouse_start = mm->get_global_position();
  181. }
  182. viewport->_nav_orbit(mm, viewport->_get_warped_mouse_motion(mm));
  183. focused_axis = -1;
  184. } else {
  185. _update_focus();
  186. }
  187. }
  188. }
  189. void ViewportRotationControl::_update_focus() {
  190. int original_focus = focused_axis;
  191. focused_axis = -2;
  192. Vector2 mouse_pos = get_local_mouse_position();
  193. if (mouse_pos.distance_to(get_size() / 2.0) < get_size().x / 2.0) {
  194. focused_axis = -1;
  195. }
  196. Vector<Axis2D> axes;
  197. _get_sorted_axis(axes);
  198. for (int i = 0; i < axes.size(); i++) {
  199. const Axis2D &axis = axes[i];
  200. if (mouse_pos.distance_to(axis.screen_point) < AXIS_CIRCLE_RADIUS) {
  201. focused_axis = axis.axis;
  202. }
  203. }
  204. if (focused_axis != original_focus) {
  205. update();
  206. }
  207. }
  208. void ViewportRotationControl::_on_mouse_exited() {
  209. focused_axis = -2;
  210. update();
  211. }
  212. void ViewportRotationControl::set_viewport(SpatialEditorViewport *p_viewport) {
  213. viewport = p_viewport;
  214. }
  215. void ViewportRotationControl::_bind_methods() {
  216. ClassDB::bind_method(D_METHOD("_gui_input"), &ViewportRotationControl::_gui_input);
  217. ClassDB::bind_method(D_METHOD("_on_mouse_exited"), &ViewportRotationControl::_on_mouse_exited);
  218. }
  219. void SpatialEditorViewport::_update_camera(float p_interp_delta) {
  220. bool is_orthogonal = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL;
  221. Cursor old_camera_cursor = camera_cursor;
  222. camera_cursor = cursor;
  223. if (p_interp_delta > 0) {
  224. //-------
  225. // Perform smoothing
  226. if (is_freelook_active()) {
  227. // Higher inertia should increase "lag" (lerp with factor between 0 and 1)
  228. // 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.
  229. const real_t inertia = EDITOR_GET("editors/3d/freelook/freelook_inertia");
  230. real_t factor = (1.0 / inertia) * p_interp_delta;
  231. // We interpolate a different point here, because in freelook mode the focus point (cursor.pos) orbits around eye_pos
  232. camera_cursor.eye_pos = old_camera_cursor.eye_pos.linear_interpolate(cursor.eye_pos, CLAMP(factor, 0, 1));
  233. const float orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  234. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  235. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  236. if (Math::abs(camera_cursor.x_rot - cursor.x_rot) < 0.1) {
  237. camera_cursor.x_rot = cursor.x_rot;
  238. }
  239. if (Math::abs(camera_cursor.y_rot - cursor.y_rot) < 0.1) {
  240. camera_cursor.y_rot = cursor.y_rot;
  241. }
  242. Vector3 forward = to_camera_transform(camera_cursor).basis.xform(Vector3(0, 0, -1));
  243. camera_cursor.pos = camera_cursor.eye_pos + forward * camera_cursor.distance;
  244. } else {
  245. const float orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  246. const float translation_inertia = EDITOR_GET("editors/3d/navigation_feel/translation_inertia");
  247. const float zoom_inertia = EDITOR_GET("editors/3d/navigation_feel/zoom_inertia");
  248. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  249. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  250. if (Math::abs(camera_cursor.x_rot - cursor.x_rot) < 0.1) {
  251. camera_cursor.x_rot = cursor.x_rot;
  252. }
  253. if (Math::abs(camera_cursor.y_rot - cursor.y_rot) < 0.1) {
  254. camera_cursor.y_rot = cursor.y_rot;
  255. }
  256. camera_cursor.pos = old_camera_cursor.pos.linear_interpolate(cursor.pos, MIN(1.f, p_interp_delta * (1 / translation_inertia)));
  257. camera_cursor.distance = Math::lerp(old_camera_cursor.distance, cursor.distance, MIN(1.f, p_interp_delta * (1 / zoom_inertia)));
  258. }
  259. }
  260. //-------
  261. // Apply camera transform
  262. real_t tolerance = 0.001;
  263. bool equal = true;
  264. 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)) {
  265. equal = false;
  266. } else if (!old_camera_cursor.pos.is_equal_approx(camera_cursor.pos)) {
  267. equal = false;
  268. } else if (!Math::is_equal_approx(old_camera_cursor.distance, camera_cursor.distance, tolerance)) {
  269. equal = false;
  270. }
  271. if (!equal || p_interp_delta == 0 || is_orthogonal != orthogonal) {
  272. camera->set_global_transform(to_camera_transform(camera_cursor));
  273. if (orthogonal) {
  274. float half_fov = Math::deg2rad(get_fov()) / 2.0;
  275. float height = 2.0 * cursor.distance * Math::tan(half_fov);
  276. camera->set_orthogonal(height, get_znear(), get_zfar());
  277. } else {
  278. camera->set_perspective(get_fov(), get_znear(), get_zfar());
  279. }
  280. update_transform_gizmo_view();
  281. rotation_control->update();
  282. spatial_editor->update_grid();
  283. }
  284. }
  285. Transform SpatialEditorViewport::to_camera_transform(const Cursor &p_cursor) const {
  286. Transform camera_transform;
  287. camera_transform.translate(p_cursor.pos);
  288. camera_transform.basis.rotate(Vector3(1, 0, 0), -p_cursor.x_rot);
  289. camera_transform.basis.rotate(Vector3(0, 1, 0), -p_cursor.y_rot);
  290. if (orthogonal) {
  291. camera_transform.translate(0, 0, (get_zfar() - get_znear()) / 2.0);
  292. } else {
  293. camera_transform.translate(0, 0, p_cursor.distance);
  294. }
  295. return camera_transform;
  296. }
  297. int SpatialEditorViewport::get_selected_count() const {
  298. Map<Node *, Object *> &selection = editor_selection->get_selection();
  299. int count = 0;
  300. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  301. Spatial *sp = Object::cast_to<Spatial>(E->key());
  302. if (!sp) {
  303. continue;
  304. }
  305. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  306. if (!se) {
  307. continue;
  308. }
  309. count++;
  310. }
  311. return count;
  312. }
  313. float SpatialEditorViewport::get_znear() const {
  314. return CLAMP(spatial_editor->get_znear(), MIN_Z, MAX_Z);
  315. }
  316. float SpatialEditorViewport::get_zfar() const {
  317. return CLAMP(spatial_editor->get_zfar(), MIN_Z, MAX_Z);
  318. }
  319. float SpatialEditorViewport::get_fov() const {
  320. return CLAMP(spatial_editor->get_fov(), MIN_FOV, MAX_FOV);
  321. }
  322. Transform SpatialEditorViewport::_get_camera_transform() const {
  323. return camera->get_global_transform();
  324. }
  325. Vector3 SpatialEditorViewport::_get_camera_position() const {
  326. return _get_camera_transform().origin;
  327. }
  328. Point2 SpatialEditorViewport::_point_to_screen(const Vector3 &p_point) {
  329. return camera->unproject_position(p_point) * viewport_container->get_stretch_shrink();
  330. }
  331. Vector3 SpatialEditorViewport::_get_ray_pos(const Vector2 &p_pos) const {
  332. return camera->project_ray_origin(p_pos / viewport_container->get_stretch_shrink());
  333. }
  334. Vector3 SpatialEditorViewport::_get_camera_normal() const {
  335. return -_get_camera_transform().basis.get_axis(2);
  336. }
  337. Vector3 SpatialEditorViewport::_get_ray(const Vector2 &p_pos) const {
  338. return camera->project_ray_normal(p_pos / viewport_container->get_stretch_shrink());
  339. }
  340. void SpatialEditorViewport::_clear_selected() {
  341. editor_selection->clear();
  342. }
  343. void SpatialEditorViewport::_select_clicked(bool p_append, bool p_single, bool p_allow_locked) {
  344. if (!clicked) {
  345. return;
  346. }
  347. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(clicked));
  348. Spatial *selected = Object::cast_to<Spatial>(node);
  349. if (!selected) {
  350. return;
  351. }
  352. if (!p_allow_locked) {
  353. // Replace the node by the group if grouped.
  354. while (node && node != editor->get_edited_scene()->get_parent()) {
  355. Spatial *selected_tmp = Object::cast_to<Spatial>(node);
  356. if (selected_tmp && node->has_meta("_edit_group_")) {
  357. selected = selected_tmp;
  358. }
  359. node = node->get_parent();
  360. }
  361. }
  362. if (p_allow_locked || !_is_node_locked(selected)) {
  363. _select(selected, clicked_wants_append, true);
  364. }
  365. }
  366. void SpatialEditorViewport::_select(Node *p_node, bool p_append, bool p_single) {
  367. if (!p_append) {
  368. editor_selection->clear();
  369. }
  370. if (editor_selection->is_selected(p_node)) {
  371. //erase
  372. editor_selection->remove_node(p_node);
  373. } else {
  374. editor_selection->add_node(p_node);
  375. }
  376. if (p_single) {
  377. if (Engine::get_singleton()->is_editor_hint()) {
  378. editor->call("edit_node", p_node);
  379. }
  380. }
  381. }
  382. ObjectID SpatialEditorViewport::_select_ray(const Point2 &p_pos, bool p_append, bool &r_includes_current, int *r_gizmo_handle, bool p_alt_select) {
  383. if (r_gizmo_handle) {
  384. *r_gizmo_handle = -1;
  385. }
  386. Vector3 ray = _get_ray(p_pos);
  387. Vector3 pos = _get_ray_pos(p_pos);
  388. Vector2 shrinked_pos = p_pos / viewport_container->get_stretch_shrink();
  389. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(pos, ray, get_tree()->get_root()->get_world()->get_scenario());
  390. Set<Ref<EditorSpatialGizmo>> found_gizmos;
  391. Node *edited_scene = get_tree()->get_edited_scene_root();
  392. ObjectID closest = 0;
  393. Node *item = nullptr;
  394. float closest_dist = 1e20;
  395. int selected_handle = -1;
  396. for (int i = 0; i < instances.size(); i++) {
  397. Spatial *spat = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  398. if (!spat) {
  399. continue;
  400. }
  401. Ref<EditorSpatialGizmo> seg = spat->get_gizmo();
  402. if ((!seg.is_valid()) || found_gizmos.has(seg)) {
  403. continue;
  404. }
  405. found_gizmos.insert(seg);
  406. Vector3 point;
  407. Vector3 normal;
  408. int handle = -1;
  409. bool inters = seg->intersect_ray(camera, shrinked_pos, point, normal, &handle, p_alt_select);
  410. if (!inters) {
  411. continue;
  412. }
  413. float dist = pos.distance_to(point);
  414. if (dist < 0) {
  415. continue;
  416. }
  417. if (dist < closest_dist) {
  418. item = Object::cast_to<Node>(spat);
  419. if (item != edited_scene) {
  420. item = edited_scene->get_deepest_editable_node(item);
  421. }
  422. closest = item->get_instance_id();
  423. closest_dist = dist;
  424. selected_handle = handle;
  425. }
  426. }
  427. if (!item) {
  428. return 0;
  429. }
  430. if (!editor_selection->is_selected(item) || (r_gizmo_handle && selected_handle >= 0)) {
  431. if (r_gizmo_handle) {
  432. *r_gizmo_handle = selected_handle;
  433. }
  434. }
  435. return closest;
  436. }
  437. void SpatialEditorViewport::_find_items_at_pos(const Point2 &p_pos, bool &r_includes_current, Vector<_RayResult> &results, bool p_alt_select) {
  438. Vector3 ray = _get_ray(p_pos);
  439. Vector3 pos = _get_ray_pos(p_pos);
  440. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(pos, ray, get_tree()->get_root()->get_world()->get_scenario());
  441. Set<Ref<EditorSpatialGizmo>> found_gizmos;
  442. r_includes_current = false;
  443. for (int i = 0; i < instances.size(); i++) {
  444. Spatial *spat = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  445. if (!spat) {
  446. continue;
  447. }
  448. Ref<EditorSpatialGizmo> seg = spat->get_gizmo();
  449. if (!seg.is_valid()) {
  450. continue;
  451. }
  452. if (found_gizmos.has(seg)) {
  453. continue;
  454. }
  455. found_gizmos.insert(seg);
  456. Vector3 point;
  457. Vector3 normal;
  458. int handle = -1;
  459. bool inters = seg->intersect_ray(camera, p_pos, point, normal, nullptr, p_alt_select);
  460. if (!inters) {
  461. continue;
  462. }
  463. float dist = pos.distance_to(point);
  464. if (dist < 0) {
  465. continue;
  466. }
  467. if (editor_selection->is_selected(spat)) {
  468. r_includes_current = true;
  469. }
  470. _RayResult res;
  471. res.item = spat;
  472. res.depth = dist;
  473. res.handle = handle;
  474. results.push_back(res);
  475. }
  476. if (results.empty()) {
  477. return;
  478. }
  479. results.sort();
  480. }
  481. Vector3 SpatialEditorViewport::_get_screen_to_space(const Vector3 &p_vector3) {
  482. CameraMatrix cm;
  483. if (orthogonal) {
  484. cm.set_orthogonal(camera->get_size(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  485. } else {
  486. cm.set_perspective(get_fov(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  487. }
  488. Vector2 screen_he = cm.get_viewport_half_extents();
  489. Transform camera_transform;
  490. camera_transform.translate(cursor.pos);
  491. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  492. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  493. camera_transform.translate(0, 0, cursor.distance);
  494. 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)));
  495. }
  496. void SpatialEditorViewport::_select_region() {
  497. if (cursor.region_begin == cursor.region_end) {
  498. return; //nothing really
  499. }
  500. float z_offset = MAX(0.0, 5.0 - get_znear());
  501. Vector3 box[4] = {
  502. Vector3(
  503. MIN(cursor.region_begin.x, cursor.region_end.x),
  504. MIN(cursor.region_begin.y, cursor.region_end.y),
  505. z_offset),
  506. Vector3(
  507. MAX(cursor.region_begin.x, cursor.region_end.x),
  508. MIN(cursor.region_begin.y, cursor.region_end.y),
  509. z_offset),
  510. Vector3(
  511. MAX(cursor.region_begin.x, cursor.region_end.x),
  512. MAX(cursor.region_begin.y, cursor.region_end.y),
  513. z_offset),
  514. Vector3(
  515. MIN(cursor.region_begin.x, cursor.region_end.x),
  516. MAX(cursor.region_begin.y, cursor.region_end.y),
  517. z_offset)
  518. };
  519. Vector<Plane> frustum;
  520. Vector3 cam_pos = _get_camera_position();
  521. for (int i = 0; i < 4; i++) {
  522. Vector3 a = _get_screen_to_space(box[i]);
  523. Vector3 b = _get_screen_to_space(box[(i + 1) % 4]);
  524. if (orthogonal) {
  525. frustum.push_back(Plane(a, (a - b).normalized()));
  526. } else {
  527. frustum.push_back(Plane(a, b, cam_pos));
  528. }
  529. }
  530. Plane near(cam_pos, -_get_camera_normal());
  531. near.d -= get_znear();
  532. frustum.push_back(near);
  533. Plane far = -near;
  534. far.d += get_zfar();
  535. frustum.push_back(far);
  536. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_convex(frustum, get_tree()->get_root()->get_world()->get_scenario());
  537. Vector<Node *> selected;
  538. Node *edited_scene = get_tree()->get_edited_scene_root();
  539. for (int i = 0; i < instances.size(); i++) {
  540. Spatial *sp = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  541. if (!sp || _is_node_locked(sp)) {
  542. continue;
  543. }
  544. Node *item = Object::cast_to<Node>(sp);
  545. if (item != edited_scene) {
  546. item = edited_scene->get_deepest_editable_node(item);
  547. }
  548. // Replace the node by the group if grouped
  549. if (item->is_class("Spatial")) {
  550. Spatial *sel = Object::cast_to<Spatial>(item);
  551. while (item && item != editor->get_edited_scene()->get_parent()) {
  552. Spatial *selected_tmp = Object::cast_to<Spatial>(item);
  553. if (selected_tmp && item->has_meta("_edit_group_")) {
  554. sel = selected_tmp;
  555. }
  556. item = item->get_parent();
  557. }
  558. item = sel;
  559. }
  560. if (selected.find(item) != -1) {
  561. continue;
  562. }
  563. if (_is_node_locked(item)) {
  564. continue;
  565. }
  566. Ref<EditorSpatialGizmo> seg = sp->get_gizmo();
  567. if (!seg.is_valid()) {
  568. continue;
  569. }
  570. if (seg->intersect_frustum(camera, frustum)) {
  571. selected.push_back(item);
  572. }
  573. }
  574. bool single = selected.size() == 1;
  575. for (int i = 0; i < selected.size(); i++) {
  576. _select(selected[i], true, single);
  577. }
  578. }
  579. void SpatialEditorViewport::_update_name() {
  580. String name;
  581. switch (view_type) {
  582. case VIEW_TYPE_USER: {
  583. if (orthogonal) {
  584. name = TTR("Orthogonal");
  585. } else {
  586. name = TTR("Perspective");
  587. }
  588. } break;
  589. case VIEW_TYPE_TOP: {
  590. if (orthogonal) {
  591. name = TTR("Top Orthogonal");
  592. } else {
  593. name = TTR("Top Perspective");
  594. }
  595. } break;
  596. case VIEW_TYPE_BOTTOM: {
  597. if (orthogonal) {
  598. name = TTR("Bottom Orthogonal");
  599. } else {
  600. name = TTR("Bottom Perspective");
  601. }
  602. } break;
  603. case VIEW_TYPE_LEFT: {
  604. if (orthogonal) {
  605. name = TTR("Left Orthogonal");
  606. } else {
  607. name = TTR("Left Perspective");
  608. }
  609. } break;
  610. case VIEW_TYPE_RIGHT: {
  611. if (orthogonal) {
  612. name = TTR("Right Orthogonal");
  613. } else {
  614. name = TTR("Right Perspective");
  615. }
  616. } break;
  617. case VIEW_TYPE_FRONT: {
  618. if (orthogonal) {
  619. name = TTR("Front Orthogonal");
  620. } else {
  621. name = TTR("Front Perspective");
  622. }
  623. } break;
  624. case VIEW_TYPE_REAR: {
  625. if (orthogonal) {
  626. name = TTR("Rear Orthogonal");
  627. } else {
  628. name = TTR("Rear Perspective");
  629. }
  630. } break;
  631. }
  632. if (auto_orthogonal) {
  633. // TRANSLATORS: This will be appended to the view name when Auto Orthogonal is enabled.
  634. name += TTR(" [auto]");
  635. }
  636. if (RoomManager::static_rooms_get_active_and_loaded()) {
  637. // TRANSLATORS: This will be appended to the view name when Portal Occulusion is enabled.
  638. name += TTR(" [portals active]");
  639. }
  640. view_menu->set_text(name);
  641. view_menu->set_size(Vector2(0, 0)); // resets the button size
  642. }
  643. void SpatialEditorViewport::_compute_edit(const Point2 &p_point) {
  644. _edit.click_ray = _get_ray(Vector2(p_point.x, p_point.y));
  645. _edit.click_ray_pos = _get_ray_pos(Vector2(p_point.x, p_point.y));
  646. _edit.plane = TRANSFORM_VIEW;
  647. spatial_editor->update_transform_gizmo();
  648. _edit.center = spatial_editor->get_gizmo_transform().origin;
  649. List<Node *> &selection = editor_selection->get_selected_node_list();
  650. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  651. Spatial *sp = Object::cast_to<Spatial>(E->get());
  652. if (!sp) {
  653. continue;
  654. }
  655. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  656. if (!se) {
  657. continue;
  658. }
  659. se->original = se->sp->get_global_gizmo_transform();
  660. se->original_local = se->sp->get_local_gizmo_transform();
  661. }
  662. }
  663. static int _get_key_modifier_setting(const String &p_property) {
  664. switch (EditorSettings::get_singleton()->get(p_property).operator int()) {
  665. case 0:
  666. return 0;
  667. case 1:
  668. return KEY_SHIFT;
  669. case 2:
  670. return KEY_ALT;
  671. case 3:
  672. return KEY_META;
  673. case 4:
  674. return KEY_CONTROL;
  675. }
  676. return 0;
  677. }
  678. static int _get_key_modifier(Ref<InputEventWithModifiers> e) {
  679. if (e->get_shift()) {
  680. return KEY_SHIFT;
  681. }
  682. if (e->get_alt()) {
  683. return KEY_ALT;
  684. }
  685. if (e->get_control()) {
  686. return KEY_CONTROL;
  687. }
  688. if (e->get_metakey()) {
  689. return KEY_META;
  690. }
  691. return 0;
  692. }
  693. bool SpatialEditorViewport::_gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only) {
  694. if (!spatial_editor->is_gizmo_visible()) {
  695. return false;
  696. }
  697. if (get_selected_count() == 0) {
  698. if (p_highlight_only) {
  699. spatial_editor->select_gizmo_highlight_axis(-1);
  700. }
  701. return false;
  702. }
  703. Vector3 ray_pos = _get_ray_pos(Vector2(p_screenpos.x, p_screenpos.y));
  704. Vector3 ray = _get_ray(Vector2(p_screenpos.x, p_screenpos.y));
  705. Transform gt = spatial_editor->get_gizmo_transform();
  706. float gs = gizmo_scale;
  707. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE) {
  708. int col_axis = -1;
  709. float col_d = 1e20;
  710. for (int i = 0; i < 3; i++) {
  711. Vector3 grabber_pos = gt.origin + gt.basis.get_axis(i) * gs * (GIZMO_ARROW_OFFSET + (GIZMO_ARROW_SIZE * 0.5));
  712. float grabber_radius = gs * GIZMO_ARROW_SIZE;
  713. Vector3 r;
  714. if (Geometry::segment_intersects_sphere(ray_pos, ray_pos + ray * MAX_Z, grabber_pos, grabber_radius, &r)) {
  715. float d = r.distance_to(ray_pos);
  716. if (d < col_d) {
  717. col_d = d;
  718. col_axis = i;
  719. }
  720. }
  721. }
  722. bool is_plane_translate = false;
  723. // plane select
  724. if (col_axis == -1) {
  725. col_d = 1e20;
  726. for (int i = 0; i < 3; i++) {
  727. Vector3 ivec2 = gt.basis.get_axis((i + 1) % 3).normalized();
  728. Vector3 ivec3 = gt.basis.get_axis((i + 2) % 3).normalized();
  729. // Allow some tolerance to make the plane easier to click,
  730. // even if the click is actually slightly outside the plane.
  731. const Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gs * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST * 0.6667);
  732. Vector3 r;
  733. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  734. if (plane.intersects_ray(ray_pos, ray, &r)) {
  735. float dist = r.distance_to(grabber_pos);
  736. // Allow some tolerance to make the plane easier to click,
  737. // even if the click is actually slightly outside the plane.
  738. if (dist < (gs * GIZMO_PLANE_SIZE * 1.5)) {
  739. float d = ray_pos.distance_to(r);
  740. if (d < col_d) {
  741. col_d = d;
  742. col_axis = i;
  743. is_plane_translate = true;
  744. }
  745. }
  746. }
  747. }
  748. }
  749. if (col_axis != -1) {
  750. if (p_highlight_only) {
  751. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_translate ? 6 : 0));
  752. } else {
  753. //handle plane translate
  754. _edit.mode = TRANSFORM_TRANSLATE;
  755. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  756. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_translate ? 3 : 0));
  757. }
  758. return true;
  759. }
  760. }
  761. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE) {
  762. int col_axis = -1;
  763. float col_d = 1e20;
  764. for (int i = 0; i < 3; i++) {
  765. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  766. Vector3 r;
  767. if (!plane.intersects_ray(ray_pos, ray, &r)) {
  768. continue;
  769. }
  770. float dist = r.distance_to(gt.origin);
  771. Vector3 r_dir = (r - gt.origin).normalized();
  772. if (_get_camera_normal().dot(r_dir) <= 0.005) {
  773. if (dist > gs * (GIZMO_CIRCLE_SIZE - GIZMO_RING_HALF_WIDTH) && dist < gs * (GIZMO_CIRCLE_SIZE + GIZMO_RING_HALF_WIDTH)) {
  774. float d = ray_pos.distance_to(r);
  775. if (d < col_d) {
  776. col_d = d;
  777. col_axis = i;
  778. }
  779. }
  780. }
  781. }
  782. if (col_axis != -1) {
  783. if (p_highlight_only) {
  784. spatial_editor->select_gizmo_highlight_axis(col_axis + 3);
  785. } else {
  786. //handle rotate
  787. _edit.mode = TRANSFORM_ROTATE;
  788. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  789. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis);
  790. }
  791. return true;
  792. }
  793. }
  794. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE) {
  795. int col_axis = -1;
  796. float col_d = 1e20;
  797. for (int i = 0; i < 3; i++) {
  798. Vector3 grabber_pos = gt.origin + gt.basis.get_axis(i) * gs * GIZMO_SCALE_OFFSET;
  799. float grabber_radius = gs * GIZMO_ARROW_SIZE;
  800. Vector3 r;
  801. if (Geometry::segment_intersects_sphere(ray_pos, ray_pos + ray * MAX_Z, grabber_pos, grabber_radius, &r)) {
  802. float d = r.distance_to(ray_pos);
  803. if (d < col_d) {
  804. col_d = d;
  805. col_axis = i;
  806. }
  807. }
  808. }
  809. bool is_plane_scale = false;
  810. // plane select
  811. if (col_axis == -1) {
  812. col_d = 1e20;
  813. for (int i = 0; i < 3; i++) {
  814. Vector3 ivec2 = gt.basis.get_axis((i + 1) % 3).normalized();
  815. Vector3 ivec3 = gt.basis.get_axis((i + 2) % 3).normalized();
  816. // Allow some tolerance to make the plane easier to click,
  817. // even if the click is actually slightly outside the plane.
  818. Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gs * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST * 0.6667);
  819. Vector3 r;
  820. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  821. if (plane.intersects_ray(ray_pos, ray, &r)) {
  822. float dist = r.distance_to(grabber_pos);
  823. // Allow some tolerance to make the plane easier to click,
  824. // even if the click is actually slightly outside the plane.
  825. if (dist < (gs * GIZMO_PLANE_SIZE * 1.5)) {
  826. float d = ray_pos.distance_to(r);
  827. if (d < col_d) {
  828. col_d = d;
  829. col_axis = i;
  830. is_plane_scale = true;
  831. }
  832. }
  833. }
  834. }
  835. }
  836. if (col_axis != -1) {
  837. if (p_highlight_only) {
  838. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_scale ? 12 : 9));
  839. } else {
  840. //handle scale
  841. _edit.mode = TRANSFORM_SCALE;
  842. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  843. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_scale ? 3 : 0));
  844. }
  845. return true;
  846. }
  847. }
  848. if (p_highlight_only) {
  849. spatial_editor->select_gizmo_highlight_axis(-1);
  850. }
  851. return false;
  852. }
  853. void SpatialEditorViewport::_surface_mouse_enter() {
  854. if (!surface->has_focus() && (!get_focus_owner() || !get_focus_owner()->is_text_field())) {
  855. surface->grab_focus();
  856. }
  857. }
  858. void SpatialEditorViewport::_surface_mouse_exit() {
  859. _remove_preview();
  860. }
  861. void SpatialEditorViewport::_surface_focus_enter() {
  862. view_menu->set_disable_shortcuts(false);
  863. }
  864. void SpatialEditorViewport::_surface_focus_exit() {
  865. view_menu->set_disable_shortcuts(true);
  866. }
  867. bool SpatialEditorViewport ::_is_node_locked(const Node *p_node) {
  868. return p_node->has_meta("_edit_lock_") && p_node->get_meta("_edit_lock_");
  869. }
  870. void SpatialEditorViewport::_list_select(Ref<InputEventMouseButton> b) {
  871. _find_items_at_pos(b->get_position(), clicked_includes_current, selection_results, b->get_shift());
  872. Node *scene = editor->get_edited_scene();
  873. for (int i = 0; i < selection_results.size(); i++) {
  874. Spatial *item = selection_results[i].item;
  875. if (item != scene && item->get_owner() != scene && item != scene->get_deepest_editable_node(item)) {
  876. //invalid result
  877. selection_results.remove(i);
  878. i--;
  879. }
  880. }
  881. clicked_wants_append = b->get_shift();
  882. bool allow_locked = b->get_alt() && spatial_editor->get_tool_mode() != SpatialEditor::TOOL_MODE_LIST_SELECT;
  883. if (selection_results.size() == 1) {
  884. clicked = selection_results[0].item->get_instance_id();
  885. selection_results.clear();
  886. if (clicked) {
  887. _select_clicked(clicked_wants_append, true, allow_locked);
  888. clicked = 0;
  889. }
  890. } else if (!selection_results.empty()) {
  891. NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
  892. StringName root_name = root_path.get_name(root_path.get_name_count() - 1);
  893. for (int i = 0; i < selection_results.size(); i++) {
  894. Spatial *spat = selection_results[i].item;
  895. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(spat, "Node");
  896. String node_path = "/" + root_name + "/" + root_path.rel_path_to(spat->get_path());
  897. int locked = 0;
  898. if (_is_node_locked(spat)) {
  899. locked = 1;
  900. } else {
  901. Node *ed_scene = editor->get_edited_scene();
  902. Node *node = spat;
  903. while (node && node != ed_scene->get_parent()) {
  904. Spatial *selected_tmp = Object::cast_to<Spatial>(node);
  905. if (selected_tmp && node->has_meta("_edit_group_")) {
  906. locked = 2;
  907. }
  908. node = node->get_parent();
  909. }
  910. }
  911. if (!allow_locked && locked > 0) {
  912. continue;
  913. }
  914. String suffix = String();
  915. if (locked == 1) {
  916. suffix = " (" + TTR("Locked") + ")";
  917. } else if (locked == 2) {
  918. suffix = " (" + TTR("Grouped") + ")";
  919. }
  920. selection_menu->add_item((String)spat->get_name() + suffix);
  921. selection_menu->set_item_icon(i, icon);
  922. selection_menu->set_item_metadata(i, node_path);
  923. selection_menu->set_item_tooltip(i, String(spat->get_name()) + "\nType: " + spat->get_class() + "\nPath: " + node_path);
  924. }
  925. if (selection_menu->get_item_count() > 0) {
  926. selection_menu->set_global_position(b->get_global_position());
  927. selection_menu->popup();
  928. }
  929. }
  930. }
  931. void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
  932. if (previewing) {
  933. return; //do NONE
  934. }
  935. {
  936. EditorNode *en = editor;
  937. EditorPluginList *force_input_forwarding_list = en->get_editor_plugins_force_input_forwarding();
  938. if (!force_input_forwarding_list->empty()) {
  939. bool discard = force_input_forwarding_list->forward_spatial_gui_input(camera, p_event, true);
  940. if (discard) {
  941. return;
  942. }
  943. }
  944. }
  945. {
  946. EditorNode *en = editor;
  947. EditorPluginList *over_plugin_list = en->get_editor_plugins_over();
  948. if (!over_plugin_list->empty()) {
  949. bool discard = over_plugin_list->forward_spatial_gui_input(camera, p_event, false);
  950. if (discard) {
  951. return;
  952. }
  953. }
  954. }
  955. Ref<InputEventMouseButton> b = p_event;
  956. if (b.is_valid()) {
  957. emit_signal("clicked", this);
  958. float zoom_factor = 1 + (ZOOM_FREELOOK_MULTIPLIER - 1) * b->get_factor();
  959. switch (b->get_button_index()) {
  960. case BUTTON_WHEEL_UP: {
  961. if (is_freelook_active()) {
  962. scale_freelook_speed(zoom_factor);
  963. } else {
  964. scale_cursor_distance(1.0 / zoom_factor);
  965. }
  966. } break;
  967. case BUTTON_WHEEL_DOWN: {
  968. if (is_freelook_active()) {
  969. scale_freelook_speed(1.0 / zoom_factor);
  970. } else {
  971. scale_cursor_distance(zoom_factor);
  972. }
  973. } break;
  974. case BUTTON_RIGHT: {
  975. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  976. if (b->is_pressed() && _edit.gizmo.is_valid()) {
  977. //restore
  978. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_initial_value, true);
  979. _edit.gizmo = Ref<EditorSpatialGizmo>();
  980. }
  981. if (_edit.mode == TRANSFORM_NONE && b->is_pressed()) {
  982. if (b->get_alt()) {
  983. if (nav_scheme == NAVIGATION_MAYA) {
  984. break;
  985. }
  986. _list_select(b);
  987. return;
  988. }
  989. }
  990. if (_edit.mode != TRANSFORM_NONE && b->is_pressed()) {
  991. //cancel motion
  992. _edit.mode = TRANSFORM_NONE;
  993. List<Node *> &selection = editor_selection->get_selected_node_list();
  994. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  995. Spatial *sp = Object::cast_to<Spatial>(E->get());
  996. if (!sp) {
  997. continue;
  998. }
  999. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1000. if (!se) {
  1001. continue;
  1002. }
  1003. sp->set_global_transform(se->original);
  1004. }
  1005. surface->update();
  1006. set_message(TTR("Transform Aborted."), 3);
  1007. }
  1008. if (b->is_pressed()) {
  1009. const int mod = _get_key_modifier(b);
  1010. if (!orthogonal) {
  1011. if (mod == _get_key_modifier_setting("editors/3d/freelook/freelook_activation_modifier")) {
  1012. set_freelook_active(true);
  1013. }
  1014. }
  1015. } else {
  1016. set_freelook_active(false);
  1017. }
  1018. if (freelook_active && !surface->has_focus()) {
  1019. // Focus usually doesn't trigger on right-click, but in case of freelook it should,
  1020. // otherwise using keyboard navigation would misbehave
  1021. surface->grab_focus();
  1022. }
  1023. } break;
  1024. case BUTTON_MIDDLE: {
  1025. if (b->is_pressed() && _edit.mode != TRANSFORM_NONE) {
  1026. switch (_edit.plane) {
  1027. case TRANSFORM_VIEW: {
  1028. _edit.plane = TRANSFORM_X_AXIS;
  1029. set_message(TTR("X-Axis Transform."), 2);
  1030. view_type = VIEW_TYPE_USER;
  1031. _update_name();
  1032. } break;
  1033. case TRANSFORM_X_AXIS: {
  1034. _edit.plane = TRANSFORM_Y_AXIS;
  1035. set_message(TTR("Y-Axis Transform."), 2);
  1036. } break;
  1037. case TRANSFORM_Y_AXIS: {
  1038. _edit.plane = TRANSFORM_Z_AXIS;
  1039. set_message(TTR("Z-Axis Transform."), 2);
  1040. } break;
  1041. case TRANSFORM_Z_AXIS: {
  1042. _edit.plane = TRANSFORM_VIEW;
  1043. set_message(TTR("View Plane Transform."), 2);
  1044. } break;
  1045. case TRANSFORM_YZ:
  1046. case TRANSFORM_XZ:
  1047. case TRANSFORM_XY: {
  1048. } break;
  1049. }
  1050. }
  1051. } break;
  1052. case BUTTON_LEFT: {
  1053. if (b->is_pressed()) {
  1054. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1055. if ((nav_scheme == NAVIGATION_MAYA || nav_scheme == NAVIGATION_MODO) && b->get_alt()) {
  1056. break;
  1057. }
  1058. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_LIST_SELECT) {
  1059. _list_select(b);
  1060. break;
  1061. }
  1062. _edit.mouse_pos = b->get_position();
  1063. _edit.original_mouse_pos = b->get_position();
  1064. _edit.snap = spatial_editor->is_snap_enabled();
  1065. _edit.mode = TRANSFORM_NONE;
  1066. //gizmo has priority over everything
  1067. bool can_select_gizmos = true;
  1068. {
  1069. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  1070. can_select_gizmos = view_menu->get_popup()->is_item_checked(idx);
  1071. }
  1072. if (can_select_gizmos && spatial_editor->get_selected()) {
  1073. Ref<EditorSpatialGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  1074. if (seg.is_valid()) {
  1075. int handle = -1;
  1076. Vector3 point;
  1077. Vector3 normal;
  1078. bool inters = seg->intersect_ray(camera, _edit.mouse_pos, point, normal, &handle, b->get_shift());
  1079. if (inters && handle != -1) {
  1080. _edit.gizmo = seg;
  1081. _edit.gizmo_handle = handle;
  1082. _edit.gizmo_initial_value = seg->get_handle_value(handle);
  1083. break;
  1084. }
  1085. }
  1086. }
  1087. if (_gizmo_select(_edit.mouse_pos)) {
  1088. break;
  1089. }
  1090. clicked = 0;
  1091. clicked_includes_current = false;
  1092. if ((spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT && b->get_command()) || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE) {
  1093. /* HANDLE ROTATION */
  1094. if (get_selected_count() == 0) {
  1095. break; //bye
  1096. }
  1097. //handle rotate
  1098. _edit.mode = TRANSFORM_ROTATE;
  1099. _compute_edit(b->get_position());
  1100. break;
  1101. }
  1102. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE) {
  1103. if (get_selected_count() == 0) {
  1104. break; //bye
  1105. }
  1106. //handle translate
  1107. _edit.mode = TRANSFORM_TRANSLATE;
  1108. _compute_edit(b->get_position());
  1109. break;
  1110. }
  1111. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE) {
  1112. if (get_selected_count() == 0) {
  1113. break; //bye
  1114. }
  1115. //handle scale
  1116. _edit.mode = TRANSFORM_SCALE;
  1117. _compute_edit(b->get_position());
  1118. break;
  1119. }
  1120. // todo scale
  1121. int gizmo_handle = -1;
  1122. clicked = _select_ray(b->get_position(), b->get_shift(), clicked_includes_current, &gizmo_handle, b->get_shift());
  1123. //clicking is always deferred to either move or release
  1124. clicked_wants_append = b->get_shift();
  1125. if (!clicked) {
  1126. if (!clicked_wants_append) {
  1127. _clear_selected();
  1128. }
  1129. //default to regionselect
  1130. cursor.region_select = true;
  1131. cursor.region_begin = b->get_position();
  1132. cursor.region_end = b->get_position();
  1133. }
  1134. if (clicked && gizmo_handle >= 0) {
  1135. Spatial *spa = Object::cast_to<Spatial>(ObjectDB::get_instance(clicked));
  1136. if (spa) {
  1137. Ref<EditorSpatialGizmo> seg = spa->get_gizmo();
  1138. if (seg.is_valid()) {
  1139. _edit.gizmo = seg;
  1140. _edit.gizmo_handle = gizmo_handle;
  1141. _edit.gizmo_initial_value = seg->get_handle_value(gizmo_handle);
  1142. break;
  1143. }
  1144. }
  1145. }
  1146. surface->update();
  1147. } else {
  1148. if (_edit.gizmo.is_valid()) {
  1149. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_initial_value, false);
  1150. _edit.gizmo = Ref<EditorSpatialGizmo>();
  1151. break;
  1152. }
  1153. if (clicked) {
  1154. _select_clicked(clicked_wants_append, true);
  1155. // Processing was deferred.
  1156. clicked = 0;
  1157. }
  1158. if (cursor.region_select) {
  1159. if (!clicked_wants_append) {
  1160. _clear_selected();
  1161. }
  1162. _select_region();
  1163. cursor.region_select = false;
  1164. surface->update();
  1165. }
  1166. if (_edit.mode != TRANSFORM_NONE) {
  1167. static const char *_transform_name[4] = {
  1168. TTRC("None"),
  1169. TTRC("Rotate"),
  1170. // TRANSLATORS: This refers to the movement that changes the position of an object.
  1171. TTRC("Translate"),
  1172. TTRC("Scale"),
  1173. };
  1174. undo_redo->create_action(TTRGET(_transform_name[_edit.mode]));
  1175. List<Node *> &selection = editor_selection->get_selected_node_list();
  1176. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1177. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1178. if (!sp) {
  1179. continue;
  1180. }
  1181. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1182. if (!se) {
  1183. continue;
  1184. }
  1185. undo_redo->add_do_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  1186. undo_redo->add_undo_method(sp, "set_global_transform", se->original);
  1187. }
  1188. undo_redo->commit_action();
  1189. _edit.mode = TRANSFORM_NONE;
  1190. set_message("");
  1191. }
  1192. surface->update();
  1193. }
  1194. } break;
  1195. }
  1196. }
  1197. Ref<InputEventMouseMotion> m = p_event;
  1198. if (m.is_valid()) {
  1199. _edit.mouse_pos = m->get_position();
  1200. if (spatial_editor->get_selected()) {
  1201. Ref<EditorSpatialGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  1202. if (seg.is_valid()) {
  1203. int selected_handle = -1;
  1204. int handle = -1;
  1205. Vector3 point;
  1206. Vector3 normal;
  1207. bool inters = seg->intersect_ray(camera, _edit.mouse_pos, point, normal, &handle, false);
  1208. if (inters && handle != -1) {
  1209. selected_handle = handle;
  1210. }
  1211. if (selected_handle != spatial_editor->get_over_gizmo_handle()) {
  1212. spatial_editor->set_over_gizmo_handle(selected_handle);
  1213. spatial_editor->get_selected()->update_gizmo();
  1214. if (selected_handle != -1) {
  1215. spatial_editor->select_gizmo_highlight_axis(-1);
  1216. }
  1217. }
  1218. }
  1219. }
  1220. if (spatial_editor->get_over_gizmo_handle() == -1 && !(m->get_button_mask() & 1) && !_edit.gizmo.is_valid()) {
  1221. _gizmo_select(_edit.mouse_pos, true);
  1222. }
  1223. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1224. NavigationMode nav_mode = NAVIGATION_NONE;
  1225. if (_edit.gizmo.is_valid()) {
  1226. _edit.gizmo->set_handle(_edit.gizmo_handle, camera, m->get_position());
  1227. Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle);
  1228. String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle);
  1229. set_message(n + ": " + String(v));
  1230. } else if (m->get_button_mask() & BUTTON_MASK_LEFT) {
  1231. if (nav_scheme == NAVIGATION_MAYA && m->get_alt()) {
  1232. nav_mode = NAVIGATION_ORBIT;
  1233. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt() && m->get_shift()) {
  1234. nav_mode = NAVIGATION_PAN;
  1235. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt() && m->get_control()) {
  1236. nav_mode = NAVIGATION_ZOOM;
  1237. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt()) {
  1238. nav_mode = NAVIGATION_ORBIT;
  1239. } else {
  1240. const bool movement_threshold_passed = _edit.original_mouse_pos.distance_to(_edit.mouse_pos) > 8 * EDSCALE;
  1241. if (clicked && movement_threshold_passed) {
  1242. if (!clicked_includes_current) {
  1243. _select_clicked(clicked_wants_append, true);
  1244. // Processing was deferred.
  1245. }
  1246. _compute_edit(_edit.mouse_pos);
  1247. clicked = 0;
  1248. _edit.mode = TRANSFORM_TRANSLATE;
  1249. }
  1250. if (cursor.region_select) {
  1251. cursor.region_end = m->get_position();
  1252. surface->update();
  1253. return;
  1254. }
  1255. if (_edit.mode == TRANSFORM_NONE) {
  1256. return;
  1257. }
  1258. Vector3 ray_pos = _get_ray_pos(m->get_position());
  1259. Vector3 ray = _get_ray(m->get_position());
  1260. double snap = EDITOR_GET("interface/inspector/default_float_step");
  1261. int snap_step_decimals = Math::range_step_decimals(snap);
  1262. switch (_edit.mode) {
  1263. case TRANSFORM_SCALE: {
  1264. Vector3 motion_mask;
  1265. Plane plane;
  1266. bool plane_mv = false;
  1267. switch (_edit.plane) {
  1268. case TRANSFORM_VIEW:
  1269. motion_mask = Vector3(0, 0, 0);
  1270. plane = Plane(_edit.center, _get_camera_normal());
  1271. break;
  1272. case TRANSFORM_X_AXIS:
  1273. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0);
  1274. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1275. break;
  1276. case TRANSFORM_Y_AXIS:
  1277. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(1);
  1278. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1279. break;
  1280. case TRANSFORM_Z_AXIS:
  1281. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2);
  1282. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1283. break;
  1284. case TRANSFORM_YZ:
  1285. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2) + spatial_editor->get_gizmo_transform().basis.get_axis(1);
  1286. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  1287. plane_mv = true;
  1288. break;
  1289. case TRANSFORM_XZ:
  1290. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2) + spatial_editor->get_gizmo_transform().basis.get_axis(0);
  1291. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  1292. plane_mv = true;
  1293. break;
  1294. case TRANSFORM_XY:
  1295. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0) + spatial_editor->get_gizmo_transform().basis.get_axis(1);
  1296. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  1297. plane_mv = true;
  1298. break;
  1299. }
  1300. Vector3 intersection;
  1301. if (!plane.intersects_ray(ray_pos, ray, &intersection)) {
  1302. break;
  1303. }
  1304. Vector3 click;
  1305. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) {
  1306. break;
  1307. }
  1308. Vector3 motion = intersection - click;
  1309. if (_edit.plane != TRANSFORM_VIEW) {
  1310. if (!plane_mv) {
  1311. motion = motion_mask.dot(motion) * motion_mask;
  1312. } else {
  1313. // Alternative planar scaling mode
  1314. if (_get_key_modifier(m) != KEY_SHIFT) {
  1315. motion = motion_mask.dot(motion) * motion_mask;
  1316. }
  1317. }
  1318. } else {
  1319. float center_click_dist = click.distance_to(_edit.center);
  1320. float center_inters_dist = intersection.distance_to(_edit.center);
  1321. if (center_click_dist == 0) {
  1322. break;
  1323. }
  1324. float scale = center_inters_dist - center_click_dist;
  1325. motion = Vector3(scale, scale, scale);
  1326. }
  1327. motion /= click.distance_to(_edit.center);
  1328. List<Node *> &selection = editor_selection->get_selected_node_list();
  1329. // Disable local transformation for TRANSFORM_VIEW
  1330. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  1331. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1332. snap = spatial_editor->get_scale_snap() / 100;
  1333. }
  1334. Vector3 motion_snapped = motion;
  1335. motion_snapped.snap(Vector3(snap, snap, snap));
  1336. // This might not be necessary anymore after issue #288 is solved (in 4.0?).
  1337. set_message(TTR("Scaling: ") + "(" + String::num(motion_snapped.x, snap_step_decimals) + ", " +
  1338. String::num(motion_snapped.y, snap_step_decimals) + ", " + String::num(motion_snapped.z, snap_step_decimals) + ")");
  1339. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1340. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1341. if (!sp) {
  1342. continue;
  1343. }
  1344. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1345. if (!se) {
  1346. continue;
  1347. }
  1348. if (sp->has_meta("_edit_lock_")) {
  1349. continue;
  1350. }
  1351. Transform original = se->original;
  1352. Transform original_local = se->original_local;
  1353. Transform base = Transform(Basis(), _edit.center);
  1354. Transform t;
  1355. Vector3 local_scale;
  1356. if (local_coords) {
  1357. Basis g = original.basis.orthonormalized();
  1358. Vector3 local_motion = g.inverse().xform(motion);
  1359. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1360. local_motion.snap(Vector3(snap, snap, snap));
  1361. }
  1362. local_scale = original_local.basis.get_scale() * (local_motion + Vector3(1, 1, 1));
  1363. // Prevent scaling to 0 it would break the gizmo
  1364. Basis check = original_local.basis;
  1365. check.scale(local_scale);
  1366. if (check.determinant() != 0) {
  1367. // Apply scale
  1368. sp->set_scale(local_scale);
  1369. }
  1370. } else {
  1371. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1372. motion.snap(Vector3(snap, snap, snap));
  1373. }
  1374. Transform r;
  1375. r.basis.scale(motion + Vector3(1, 1, 1));
  1376. t = base * (r * (base.inverse() * original));
  1377. // Apply scale
  1378. sp->set_global_transform(t);
  1379. }
  1380. }
  1381. surface->update();
  1382. } break;
  1383. case TRANSFORM_TRANSLATE: {
  1384. Vector3 motion_mask;
  1385. Plane plane;
  1386. bool plane_mv = false;
  1387. switch (_edit.plane) {
  1388. case TRANSFORM_VIEW:
  1389. plane = Plane(_edit.center, _get_camera_normal());
  1390. break;
  1391. case TRANSFORM_X_AXIS:
  1392. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0);
  1393. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1394. break;
  1395. case TRANSFORM_Y_AXIS:
  1396. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(1);
  1397. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1398. break;
  1399. case TRANSFORM_Z_AXIS:
  1400. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2);
  1401. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1402. break;
  1403. case TRANSFORM_YZ:
  1404. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  1405. plane_mv = true;
  1406. break;
  1407. case TRANSFORM_XZ:
  1408. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  1409. plane_mv = true;
  1410. break;
  1411. case TRANSFORM_XY:
  1412. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  1413. plane_mv = true;
  1414. break;
  1415. }
  1416. Vector3 intersection;
  1417. if (!plane.intersects_ray(ray_pos, ray, &intersection)) {
  1418. break;
  1419. }
  1420. Vector3 click;
  1421. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) {
  1422. break;
  1423. }
  1424. Vector3 motion = intersection - click;
  1425. if (_edit.plane != TRANSFORM_VIEW) {
  1426. if (!plane_mv) {
  1427. motion = motion_mask.dot(motion) * motion_mask;
  1428. }
  1429. }
  1430. List<Node *> &selection = editor_selection->get_selected_node_list();
  1431. // Disable local transformation for TRANSFORM_VIEW
  1432. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  1433. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1434. snap = spatial_editor->get_translate_snap();
  1435. }
  1436. Vector3 motion_snapped = motion;
  1437. motion_snapped.snap(Vector3(snap, snap, snap));
  1438. set_message(TTR("Translating: ") + "(" + String::num(motion_snapped.x, snap_step_decimals) + ", " +
  1439. String::num(motion_snapped.y, snap_step_decimals) + ", " + String::num(motion_snapped.z, snap_step_decimals) + ")");
  1440. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1441. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1442. if (!sp) {
  1443. continue;
  1444. }
  1445. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1446. if (!se) {
  1447. continue;
  1448. }
  1449. if (sp->has_meta("_edit_lock_")) {
  1450. continue;
  1451. }
  1452. Transform original = se->original;
  1453. Transform t;
  1454. if (local_coords) {
  1455. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1456. Basis g = original.basis.orthonormalized();
  1457. Vector3 local_motion = g.inverse().xform(motion);
  1458. local_motion.snap(Vector3(snap, snap, snap));
  1459. motion = g.xform(local_motion);
  1460. }
  1461. } else {
  1462. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1463. motion.snap(Vector3(snap, snap, snap));
  1464. }
  1465. }
  1466. // Apply translation
  1467. t = original;
  1468. t.origin += motion;
  1469. sp->set_global_transform(t);
  1470. }
  1471. surface->update();
  1472. } break;
  1473. case TRANSFORM_ROTATE: {
  1474. Plane plane;
  1475. Vector3 axis;
  1476. switch (_edit.plane) {
  1477. case TRANSFORM_VIEW:
  1478. plane = Plane(_edit.center, _get_camera_normal());
  1479. break;
  1480. case TRANSFORM_X_AXIS:
  1481. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  1482. axis = Vector3(1, 0, 0);
  1483. break;
  1484. case TRANSFORM_Y_AXIS:
  1485. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  1486. axis = Vector3(0, 1, 0);
  1487. break;
  1488. case TRANSFORM_Z_AXIS:
  1489. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  1490. axis = Vector3(0, 0, 1);
  1491. break;
  1492. case TRANSFORM_YZ:
  1493. case TRANSFORM_XZ:
  1494. case TRANSFORM_XY:
  1495. break;
  1496. }
  1497. Vector3 intersection;
  1498. if (!plane.intersects_ray(ray_pos, ray, &intersection)) {
  1499. break;
  1500. }
  1501. Vector3 click;
  1502. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) {
  1503. break;
  1504. }
  1505. Vector3 y_axis = (click - _edit.center).normalized();
  1506. Vector3 x_axis = plane.normal.cross(y_axis).normalized();
  1507. double angle = Math::atan2(x_axis.dot(intersection - _edit.center), y_axis.dot(intersection - _edit.center));
  1508. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1509. snap = spatial_editor->get_rotate_snap();
  1510. }
  1511. angle = Math::rad2deg(angle) + snap * 0.5; //else it won't reach +180
  1512. angle -= Math::fmod(angle, snap);
  1513. set_message(vformat(TTR("Rotating %s degrees."), String::num(angle, snap_step_decimals)));
  1514. angle = Math::deg2rad(angle);
  1515. List<Node *> &selection = editor_selection->get_selected_node_list();
  1516. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); // Disable local transformation for TRANSFORM_VIEW
  1517. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1518. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1519. if (!sp) {
  1520. continue;
  1521. }
  1522. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1523. if (!se) {
  1524. continue;
  1525. }
  1526. if (sp->has_meta("_edit_lock_")) {
  1527. continue;
  1528. }
  1529. Transform t;
  1530. if (local_coords) {
  1531. Transform original_local = se->original_local;
  1532. Basis rot = Basis(axis, angle);
  1533. t.basis = original_local.get_basis().orthonormalized() * rot;
  1534. t.origin = original_local.origin;
  1535. // Apply rotation
  1536. sp->set_transform(t);
  1537. sp->set_scale(original_local.basis.get_scale()); // re-apply original scale
  1538. } else {
  1539. Transform original = se->original;
  1540. Transform r;
  1541. Transform base = Transform(Basis(), _edit.center);
  1542. r.basis.rotate(plane.normal, angle);
  1543. t = base * r * base.inverse() * original;
  1544. // Apply rotation
  1545. sp->set_global_transform(t);
  1546. }
  1547. }
  1548. surface->update();
  1549. } break;
  1550. default: {
  1551. }
  1552. }
  1553. }
  1554. } else if ((m->get_button_mask() & BUTTON_MASK_RIGHT) || freelook_active) {
  1555. if (nav_scheme == NAVIGATION_MAYA && m->get_alt()) {
  1556. nav_mode = NAVIGATION_ZOOM;
  1557. } else if (freelook_active) {
  1558. nav_mode = NAVIGATION_LOOK;
  1559. } else if (orthogonal) {
  1560. nav_mode = NAVIGATION_PAN;
  1561. }
  1562. } else if (m->get_button_mask() & BUTTON_MASK_MIDDLE) {
  1563. const int mod = _get_key_modifier(m);
  1564. if (nav_scheme == NAVIGATION_GODOT) {
  1565. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier")) {
  1566. nav_mode = NAVIGATION_PAN;
  1567. } else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier")) {
  1568. nav_mode = NAVIGATION_ZOOM;
  1569. } else if (mod == KEY_ALT || mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier")) {
  1570. // Always allow Alt as a modifier to better support graphic tablets.
  1571. nav_mode = NAVIGATION_ORBIT;
  1572. }
  1573. } else if (nav_scheme == NAVIGATION_MAYA) {
  1574. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier")) {
  1575. nav_mode = NAVIGATION_PAN;
  1576. }
  1577. }
  1578. } else if (EditorSettings::get_singleton()->get("editors/3d/navigation/emulate_3_button_mouse")) {
  1579. // Handle trackpad (no external mouse) use case
  1580. const int mod = _get_key_modifier(m);
  1581. if (mod) {
  1582. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier")) {
  1583. nav_mode = NAVIGATION_PAN;
  1584. } else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier")) {
  1585. nav_mode = NAVIGATION_ZOOM;
  1586. } else if (mod == KEY_ALT || mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier")) {
  1587. // Always allow Alt as a modifier to better support graphic tablets.
  1588. nav_mode = NAVIGATION_ORBIT;
  1589. }
  1590. }
  1591. }
  1592. switch (nav_mode) {
  1593. case NAVIGATION_PAN: {
  1594. _nav_pan(m, _get_warped_mouse_motion(m));
  1595. } break;
  1596. case NAVIGATION_ZOOM: {
  1597. _nav_zoom(m, m->get_relative());
  1598. } break;
  1599. case NAVIGATION_ORBIT: {
  1600. _nav_orbit(m, _get_warped_mouse_motion(m));
  1601. } break;
  1602. case NAVIGATION_LOOK: {
  1603. _nav_look(m, _get_warped_mouse_motion(m));
  1604. } break;
  1605. default: {
  1606. }
  1607. }
  1608. }
  1609. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  1610. if (magnify_gesture.is_valid()) {
  1611. if (is_freelook_active()) {
  1612. scale_freelook_speed(magnify_gesture->get_factor());
  1613. } else {
  1614. scale_cursor_distance(1.0 / magnify_gesture->get_factor());
  1615. }
  1616. }
  1617. Ref<InputEventPanGesture> pan_gesture = p_event;
  1618. if (pan_gesture.is_valid()) {
  1619. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1620. NavigationMode nav_mode = NAVIGATION_NONE;
  1621. if (nav_scheme == NAVIGATION_GODOT) {
  1622. const int mod = _get_key_modifier(pan_gesture);
  1623. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier")) {
  1624. nav_mode = NAVIGATION_PAN;
  1625. } else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier")) {
  1626. nav_mode = NAVIGATION_ZOOM;
  1627. } else if (mod == KEY_ALT || mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier")) {
  1628. // Always allow Alt as a modifier to better support graphic tablets.
  1629. nav_mode = NAVIGATION_ORBIT;
  1630. }
  1631. } else if (nav_scheme == NAVIGATION_MAYA) {
  1632. if (pan_gesture->get_alt()) {
  1633. nav_mode = NAVIGATION_PAN;
  1634. }
  1635. }
  1636. switch (nav_mode) {
  1637. case NAVIGATION_PAN: {
  1638. _nav_pan(pan_gesture, pan_gesture->get_delta());
  1639. } break;
  1640. case NAVIGATION_ZOOM: {
  1641. _nav_zoom(pan_gesture, pan_gesture->get_delta());
  1642. } break;
  1643. case NAVIGATION_ORBIT: {
  1644. _nav_orbit(pan_gesture, pan_gesture->get_delta());
  1645. } break;
  1646. case NAVIGATION_LOOK: {
  1647. _nav_look(pan_gesture, pan_gesture->get_delta());
  1648. } break;
  1649. default: {
  1650. }
  1651. }
  1652. }
  1653. Ref<InputEventKey> k = p_event;
  1654. if (k.is_valid()) {
  1655. if (!k->is_pressed()) {
  1656. return;
  1657. }
  1658. if (EditorSettings::get_singleton()->get("editors/3d/navigation/emulate_numpad")) {
  1659. const uint32_t code = k->get_scancode();
  1660. if (code >= KEY_0 && code <= KEY_9) {
  1661. k->set_scancode(code - KEY_0 + KEY_KP_0);
  1662. }
  1663. }
  1664. if (ED_IS_SHORTCUT("spatial_editor/snap", p_event)) {
  1665. if (_edit.mode != TRANSFORM_NONE) {
  1666. _edit.snap = !_edit.snap;
  1667. }
  1668. }
  1669. if (ED_IS_SHORTCUT("spatial_editor/bottom_view", p_event)) {
  1670. _menu_option(VIEW_BOTTOM);
  1671. }
  1672. if (ED_IS_SHORTCUT("spatial_editor/top_view", p_event)) {
  1673. _menu_option(VIEW_TOP);
  1674. }
  1675. if (ED_IS_SHORTCUT("spatial_editor/rear_view", p_event)) {
  1676. _menu_option(VIEW_REAR);
  1677. }
  1678. if (ED_IS_SHORTCUT("spatial_editor/front_view", p_event)) {
  1679. _menu_option(VIEW_FRONT);
  1680. }
  1681. if (ED_IS_SHORTCUT("spatial_editor/left_view", p_event)) {
  1682. _menu_option(VIEW_LEFT);
  1683. }
  1684. if (ED_IS_SHORTCUT("spatial_editor/right_view", p_event)) {
  1685. _menu_option(VIEW_RIGHT);
  1686. }
  1687. if (ED_IS_SHORTCUT("spatial_editor/focus_origin", p_event)) {
  1688. _menu_option(VIEW_CENTER_TO_ORIGIN);
  1689. }
  1690. if (ED_IS_SHORTCUT("spatial_editor/focus_selection", p_event)) {
  1691. _menu_option(VIEW_CENTER_TO_SELECTION);
  1692. }
  1693. // Orthgonal mode doesn't work in freelook.
  1694. if (!freelook_active && ED_IS_SHORTCUT("spatial_editor/switch_perspective_orthogonal", p_event)) {
  1695. _menu_option(orthogonal ? VIEW_PERSPECTIVE : VIEW_ORTHOGONAL);
  1696. }
  1697. if (ED_IS_SHORTCUT("spatial_editor/align_transform_with_view", p_event)) {
  1698. _menu_option(VIEW_ALIGN_TRANSFORM_WITH_VIEW);
  1699. }
  1700. if (ED_IS_SHORTCUT("spatial_editor/align_rotation_with_view", p_event)) {
  1701. _menu_option(VIEW_ALIGN_ROTATION_WITH_VIEW);
  1702. }
  1703. if (ED_IS_SHORTCUT("spatial_editor/insert_anim_key", p_event)) {
  1704. if (!get_selected_count() || _edit.mode != TRANSFORM_NONE) {
  1705. return;
  1706. }
  1707. if (!AnimationPlayerEditor::singleton->get_track_editor()->has_keying()) {
  1708. set_message(TTR("Keying is disabled (no key inserted)."));
  1709. return;
  1710. }
  1711. List<Node *> &selection = editor_selection->get_selected_node_list();
  1712. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1713. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1714. if (!sp) {
  1715. continue;
  1716. }
  1717. spatial_editor->emit_signal("transform_key_request", sp, "", sp->get_transform());
  1718. }
  1719. set_message(TTR("Animation Key Inserted."));
  1720. }
  1721. // Freelook doesn't work in orthogonal mode.
  1722. if (!orthogonal && ED_IS_SHORTCUT("spatial_editor/freelook_toggle", p_event)) {
  1723. set_freelook_active(!is_freelook_active());
  1724. } else if (k->get_scancode() == KEY_ESCAPE) {
  1725. set_freelook_active(false);
  1726. }
  1727. if (k->get_scancode() == KEY_SPACE) {
  1728. if (!k->is_pressed()) {
  1729. emit_signal("toggle_maximize_view", this);
  1730. }
  1731. }
  1732. }
  1733. // freelook uses most of the useful shortcuts, like save, so its ok
  1734. // to consider freelook active as end of the line for future events.
  1735. if (freelook_active) {
  1736. accept_event();
  1737. }
  1738. }
  1739. void SpatialEditorViewport::_nav_pan(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1740. const NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1741. real_t pan_speed = 1 / 150.0;
  1742. int pan_speed_modifier = 10;
  1743. if (nav_scheme == NAVIGATION_MAYA && p_event->get_shift()) {
  1744. pan_speed *= pan_speed_modifier;
  1745. }
  1746. Transform camera_transform;
  1747. camera_transform.translate(cursor.pos);
  1748. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  1749. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  1750. const bool invert_x_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_x_axis");
  1751. const bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y_axis");
  1752. Vector3 translation(
  1753. (invert_x_axis ? -1 : 1) * -p_relative.x * pan_speed,
  1754. (invert_y_axis ? -1 : 1) * p_relative.y * pan_speed,
  1755. 0);
  1756. translation *= cursor.distance / DISTANCE_DEFAULT;
  1757. camera_transform.translate(translation);
  1758. cursor.pos = camera_transform.origin;
  1759. }
  1760. void SpatialEditorViewport::_nav_zoom(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1761. const NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1762. real_t zoom_speed = 1 / 80.0;
  1763. int zoom_speed_modifier = 10;
  1764. if (nav_scheme == NAVIGATION_MAYA && p_event->get_shift()) {
  1765. zoom_speed *= zoom_speed_modifier;
  1766. }
  1767. NavigationZoomStyle zoom_style = (NavigationZoomStyle)EditorSettings::get_singleton()->get("editors/3d/navigation/zoom_style").operator int();
  1768. if (zoom_style == NAVIGATION_ZOOM_HORIZONTAL) {
  1769. if (p_relative.x > 0) {
  1770. scale_cursor_distance(1 - p_relative.x * zoom_speed);
  1771. } else if (p_relative.x < 0) {
  1772. scale_cursor_distance(1.0 / (1 + p_relative.x * zoom_speed));
  1773. }
  1774. } else {
  1775. if (p_relative.y > 0) {
  1776. scale_cursor_distance(1 + p_relative.y * zoom_speed);
  1777. } else if (p_relative.y < 0) {
  1778. scale_cursor_distance(1.0 / (1 - p_relative.y * zoom_speed));
  1779. }
  1780. }
  1781. }
  1782. void SpatialEditorViewport::_nav_orbit(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1783. if (lock_rotation) {
  1784. _nav_pan(p_event, p_relative);
  1785. return;
  1786. }
  1787. if (orthogonal && auto_orthogonal) {
  1788. _menu_option(VIEW_PERSPECTIVE);
  1789. }
  1790. const real_t degrees_per_pixel = EditorSettings::get_singleton()->get("editors/3d/navigation_feel/orbit_sensitivity");
  1791. const real_t radians_per_pixel = Math::deg2rad(degrees_per_pixel);
  1792. const bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y_axis");
  1793. const bool invert_x_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_x_axis");
  1794. if (invert_y_axis) {
  1795. cursor.x_rot -= p_relative.y * radians_per_pixel;
  1796. } else {
  1797. cursor.x_rot += p_relative.y * radians_per_pixel;
  1798. }
  1799. // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1800. cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57);
  1801. if (invert_x_axis) {
  1802. cursor.y_rot -= p_relative.x * radians_per_pixel;
  1803. } else {
  1804. cursor.y_rot += p_relative.x * radians_per_pixel;
  1805. }
  1806. view_type = VIEW_TYPE_USER;
  1807. _update_name();
  1808. }
  1809. void SpatialEditorViewport::_nav_look(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1810. if (orthogonal) {
  1811. _nav_pan(p_event, p_relative);
  1812. return;
  1813. }
  1814. if (orthogonal && auto_orthogonal) {
  1815. _menu_option(VIEW_PERSPECTIVE);
  1816. }
  1817. const real_t degrees_per_pixel = EditorSettings::get_singleton()->get("editors/3d/navigation_feel/orbit_sensitivity");
  1818. const real_t radians_per_pixel = Math::deg2rad(degrees_per_pixel);
  1819. const bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y_axis");
  1820. // Note: do NOT assume the camera has the "current" transform, because it is interpolated and may have "lag".
  1821. const Transform prev_camera_transform = to_camera_transform(cursor);
  1822. if (invert_y_axis) {
  1823. cursor.x_rot -= p_relative.y * radians_per_pixel;
  1824. } else {
  1825. cursor.x_rot += p_relative.y * radians_per_pixel;
  1826. }
  1827. // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1828. cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57);
  1829. cursor.y_rot += p_relative.x * radians_per_pixel;
  1830. // Look is like the opposite of Orbit: the focus point rotates around the camera
  1831. Transform camera_transform = to_camera_transform(cursor);
  1832. Vector3 pos = camera_transform.xform(Vector3(0, 0, 0));
  1833. Vector3 prev_pos = prev_camera_transform.xform(Vector3(0, 0, 0));
  1834. Vector3 diff = prev_pos - pos;
  1835. cursor.pos += diff;
  1836. view_type = VIEW_TYPE_USER;
  1837. _update_name();
  1838. }
  1839. void SpatialEditorViewport::set_freelook_active(bool active_now) {
  1840. if (!freelook_active && active_now) {
  1841. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  1842. cursor = camera_cursor;
  1843. // Make sure eye_pos is synced, because freelook referential is eye pos rather than orbit pos
  1844. Vector3 forward = to_camera_transform(cursor).basis.xform(Vector3(0, 0, -1));
  1845. cursor.eye_pos = cursor.pos - cursor.distance * forward;
  1846. // Also sync the camera cursor, otherwise switching to freelook will be trippy if inertia is active
  1847. camera_cursor.eye_pos = cursor.eye_pos;
  1848. if (EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_speed_zoom_link")) {
  1849. // Re-adjust freelook speed from the current zoom level
  1850. real_t base_speed = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_base_speed");
  1851. freelook_speed = base_speed * cursor.distance;
  1852. }
  1853. previous_mouse_position = get_local_mouse_position();
  1854. // Hide mouse like in an FPS (warping doesn't work)
  1855. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  1856. } else if (freelook_active && !active_now) {
  1857. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  1858. cursor = camera_cursor;
  1859. // Restore mouse
  1860. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  1861. // Restore the previous mouse position when leaving freelook mode.
  1862. // This is done because leaving `Input.MOUSE_MODE_CAPTURED` will center the cursor
  1863. // due to OS limitations.
  1864. warp_mouse(previous_mouse_position);
  1865. }
  1866. freelook_active = active_now;
  1867. }
  1868. void SpatialEditorViewport::scale_cursor_distance(real_t scale) {
  1869. real_t min_distance = MAX(camera->get_znear() * 4, ZOOM_FREELOOK_MIN);
  1870. real_t max_distance = MIN(camera->get_zfar() / 2, ZOOM_FREELOOK_MAX);
  1871. if (unlikely(min_distance > max_distance)) {
  1872. cursor.distance = (min_distance + max_distance) / 2;
  1873. } else {
  1874. cursor.distance = CLAMP(cursor.distance * scale, min_distance, max_distance);
  1875. }
  1876. if (cursor.distance == max_distance || cursor.distance == min_distance) {
  1877. zoom_failed_attempts_count++;
  1878. } else {
  1879. zoom_failed_attempts_count = 0;
  1880. }
  1881. zoom_indicator_delay = ZOOM_FREELOOK_INDICATOR_DELAY_S;
  1882. surface->update();
  1883. }
  1884. void SpatialEditorViewport::scale_freelook_speed(real_t scale) {
  1885. real_t min_speed = MAX(camera->get_znear() * 4, ZOOM_FREELOOK_MIN);
  1886. real_t max_speed = MIN(camera->get_zfar() / 2, ZOOM_FREELOOK_MAX);
  1887. if (unlikely(min_speed > max_speed)) {
  1888. freelook_speed = (min_speed + max_speed) / 2;
  1889. } else {
  1890. freelook_speed = CLAMP(freelook_speed * scale, min_speed, max_speed);
  1891. }
  1892. zoom_indicator_delay = ZOOM_FREELOOK_INDICATOR_DELAY_S;
  1893. surface->update();
  1894. }
  1895. Point2i SpatialEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const {
  1896. Point2i relative;
  1897. if (bool(EDITOR_DEF("editors/3d/navigation/warped_mouse_panning", false))) {
  1898. relative = Input::get_singleton()->warp_mouse_motion(p_ev_mouse_motion, surface->get_global_rect());
  1899. } else {
  1900. relative = p_ev_mouse_motion->get_relative();
  1901. }
  1902. return relative;
  1903. }
  1904. static bool is_shortcut_pressed(const String &p_path) {
  1905. Ref<ShortCut> shortcut = ED_GET_SHORTCUT(p_path);
  1906. if (shortcut.is_null()) {
  1907. return false;
  1908. }
  1909. InputEventKey *k = Object::cast_to<InputEventKey>(shortcut->get_shortcut().ptr());
  1910. if (k == nullptr) {
  1911. return false;
  1912. }
  1913. const Input &input = *Input::get_singleton();
  1914. int scancode = k->get_scancode();
  1915. return input.is_key_pressed(scancode);
  1916. }
  1917. void SpatialEditorViewport::_update_freelook(real_t delta) {
  1918. if (!is_freelook_active()) {
  1919. return;
  1920. }
  1921. const FreelookNavigationScheme navigation_scheme = (FreelookNavigationScheme)EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_navigation_scheme").operator int();
  1922. Vector3 forward;
  1923. if (navigation_scheme == FREELOOK_FULLY_AXIS_LOCKED) {
  1924. // Forward/backward keys will always go straight forward/backward, never moving on the Y axis.
  1925. forward = Vector3(0, 0, -1).rotated(Vector3(0, 1, 0), camera->get_rotation().y);
  1926. } else {
  1927. // Forward/backward keys will be relative to the camera pitch.
  1928. forward = camera->get_transform().basis.xform(Vector3(0, 0, -1));
  1929. }
  1930. const Vector3 right = camera->get_transform().basis.xform(Vector3(1, 0, 0));
  1931. Vector3 up;
  1932. if (navigation_scheme == FREELOOK_PARTIALLY_AXIS_LOCKED || navigation_scheme == FREELOOK_FULLY_AXIS_LOCKED) {
  1933. // Up/down keys will always go up/down regardless of camera pitch.
  1934. up = Vector3(0, 1, 0);
  1935. } else {
  1936. // Up/down keys will be relative to the camera pitch.
  1937. up = camera->get_transform().basis.xform(Vector3(0, 1, 0));
  1938. }
  1939. Vector3 direction;
  1940. if (is_shortcut_pressed("spatial_editor/freelook_left")) {
  1941. direction -= right;
  1942. }
  1943. if (is_shortcut_pressed("spatial_editor/freelook_right")) {
  1944. direction += right;
  1945. }
  1946. if (is_shortcut_pressed("spatial_editor/freelook_forward")) {
  1947. direction += forward;
  1948. }
  1949. if (is_shortcut_pressed("spatial_editor/freelook_backwards")) {
  1950. direction -= forward;
  1951. }
  1952. if (is_shortcut_pressed("spatial_editor/freelook_up")) {
  1953. direction += up;
  1954. }
  1955. if (is_shortcut_pressed("spatial_editor/freelook_down")) {
  1956. direction -= up;
  1957. }
  1958. real_t speed = freelook_speed;
  1959. if (is_shortcut_pressed("spatial_editor/freelook_speed_modifier")) {
  1960. speed *= 3.0;
  1961. }
  1962. if (is_shortcut_pressed("spatial_editor/freelook_slow_modifier")) {
  1963. speed *= 0.333333;
  1964. }
  1965. const Vector3 motion = direction * speed * delta;
  1966. cursor.pos += motion;
  1967. cursor.eye_pos += motion;
  1968. }
  1969. void SpatialEditorViewport::set_message(String p_message, float p_time) {
  1970. message = p_message;
  1971. message_time = p_time;
  1972. }
  1973. void SpatialEditorPlugin::edited_scene_changed() {
  1974. for (uint32_t i = 0; i < SpatialEditor::VIEWPORTS_COUNT; i++) {
  1975. SpatialEditorViewport *viewport = SpatialEditor::get_singleton()->get_editor_viewport(i);
  1976. if (viewport->is_visible()) {
  1977. viewport->notification(Control::NOTIFICATION_VISIBILITY_CHANGED);
  1978. }
  1979. }
  1980. }
  1981. void SpatialEditorViewport::_notification(int p_what) {
  1982. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  1983. bool visible = is_visible_in_tree();
  1984. set_process(visible);
  1985. if (visible) {
  1986. orthogonal = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL));
  1987. _update_name();
  1988. _update_camera(0);
  1989. } else {
  1990. set_freelook_active(false);
  1991. }
  1992. call_deferred("update_transform_gizmo_view");
  1993. rotation_control->set_visible(EditorSettings::get_singleton()->get("editors/3d/navigation/show_viewport_rotation_gizmo"));
  1994. }
  1995. if (p_what == NOTIFICATION_RESIZED) {
  1996. call_deferred("update_transform_gizmo_view");
  1997. }
  1998. if (p_what == NOTIFICATION_PROCESS) {
  1999. real_t delta = get_process_delta_time();
  2000. if (zoom_indicator_delay > 0) {
  2001. zoom_indicator_delay -= delta;
  2002. if (zoom_indicator_delay <= 0) {
  2003. surface->update();
  2004. zoom_limit_label->hide();
  2005. }
  2006. }
  2007. _update_freelook(delta);
  2008. Node *scene_root = editor->get_scene_tree_dock()->get_editor_data()->get_edited_scene_root();
  2009. if (previewing_cinema && scene_root != nullptr) {
  2010. Camera *cam = scene_root->get_viewport()->get_camera();
  2011. if (cam != nullptr && cam != previewing) {
  2012. //then switch the viewport's camera to the scene's viewport camera
  2013. if (previewing != nullptr) {
  2014. previewing->disconnect("tree_exited", this, "_preview_exited_scene");
  2015. }
  2016. previewing = cam;
  2017. previewing->connect("tree_exited", this, "_preview_exited_scene");
  2018. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), cam->get_camera());
  2019. surface->update();
  2020. }
  2021. }
  2022. _update_camera(delta);
  2023. Map<Node *, Object *> &selection = editor_selection->get_selection();
  2024. bool changed = false;
  2025. bool exist = false;
  2026. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  2027. Spatial *sp = Object::cast_to<Spatial>(E->key());
  2028. if (!sp) {
  2029. continue;
  2030. }
  2031. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  2032. if (!se) {
  2033. continue;
  2034. }
  2035. Transform t = sp->get_global_gizmo_transform();
  2036. VisualInstance *vi = Object::cast_to<VisualInstance>(sp);
  2037. AABB new_aabb = vi ? vi->get_aabb() : _calculate_spatial_bounds(sp);
  2038. exist = true;
  2039. if (se->last_xform == t && se->aabb == new_aabb && !se->last_xform_dirty) {
  2040. continue;
  2041. }
  2042. changed = true;
  2043. se->last_xform_dirty = false;
  2044. se->last_xform = t;
  2045. se->aabb = new_aabb;
  2046. t.translate(se->aabb.position);
  2047. // apply AABB scaling before item's global transform
  2048. Basis aabb_s;
  2049. aabb_s.scale(se->aabb.size);
  2050. t.basis = t.basis * aabb_s;
  2051. VisualServer::get_singleton()->instance_set_transform(se->sbox_instance, t);
  2052. VisualServer::get_singleton()->instance_set_transform(se->sbox_instance_xray, t);
  2053. }
  2054. if (changed || (spatial_editor->is_gizmo_visible() && !exist)) {
  2055. spatial_editor->update_transform_gizmo();
  2056. }
  2057. if (message_time > 0) {
  2058. if (message != last_message) {
  2059. surface->update();
  2060. last_message = message;
  2061. }
  2062. message_time -= get_physics_process_delta_time();
  2063. if (message_time < 0) {
  2064. surface->update();
  2065. }
  2066. }
  2067. //update shadow atlas if changed
  2068. int shadowmap_size = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/size");
  2069. int atlas_q0 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_0_subdiv");
  2070. int atlas_q1 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_1_subdiv");
  2071. int atlas_q2 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_2_subdiv");
  2072. int atlas_q3 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_3_subdiv");
  2073. viewport->set_shadow_atlas_size(shadowmap_size);
  2074. viewport->set_shadow_atlas_quadrant_subdiv(0, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q0));
  2075. viewport->set_shadow_atlas_quadrant_subdiv(1, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q1));
  2076. viewport->set_shadow_atlas_quadrant_subdiv(2, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q2));
  2077. viewport->set_shadow_atlas_quadrant_subdiv(3, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q3));
  2078. bool shrink = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION));
  2079. if (shrink != (viewport_container->get_stretch_shrink() > 1)) {
  2080. viewport_container->set_stretch_shrink(shrink ? 2 : 1);
  2081. }
  2082. // Update MSAA, FXAA, debanding and HDR if changed.
  2083. int msaa_mode = ProjectSettings::get_singleton()->get("rendering/quality/filters/msaa");
  2084. viewport->set_msaa(Viewport::MSAA(msaa_mode));
  2085. bool use_fxaa = ProjectSettings::get_singleton()->get("rendering/quality/filters/use_fxaa");
  2086. viewport->set_use_fxaa(use_fxaa);
  2087. bool use_debanding = ProjectSettings::get_singleton()->get("rendering/quality/filters/use_debanding");
  2088. viewport->set_use_debanding(use_debanding);
  2089. float sharpen_intensity = ProjectSettings::get_singleton()->get("rendering/quality/filters/sharpen_intensity");
  2090. viewport->set_sharpen_intensity(sharpen_intensity);
  2091. bool hdr = ProjectSettings::get_singleton()->get("rendering/quality/depth/hdr");
  2092. viewport->set_hdr(hdr);
  2093. bool show_info = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  2094. info_label->set_visible(show_info);
  2095. Camera *current_camera;
  2096. if (previewing) {
  2097. current_camera = previewing;
  2098. } else {
  2099. current_camera = camera;
  2100. }
  2101. if (show_info) {
  2102. const String viewport_size = vformat(String::utf8("%d × %d"), viewport->get_size().x, viewport->get_size().y);
  2103. String text;
  2104. text += "X: " + rtos(current_camera->get_translation().x).pad_decimals(1) + "\n";
  2105. text += "Y: " + rtos(current_camera->get_translation().y).pad_decimals(1) + "\n";
  2106. text += "Z: " + rtos(current_camera->get_translation().z).pad_decimals(1) + "\n";
  2107. text += TTR("Pitch:") + " " + itos(Math::round(current_camera->get_rotation_degrees().x)) + "\n";
  2108. text += TTR("Yaw:") + " " + itos(Math::round(current_camera->get_rotation_degrees().y)) + "\n\n";
  2109. text += TTR("Size:") +
  2110. vformat(
  2111. " %s (%.1fMP)\n",
  2112. viewport_size,
  2113. viewport->get_size().x * viewport->get_size().y * 0.000'001);
  2114. text += TTR("Objects Drawn:") + " " + itos(viewport->get_render_info(Viewport::RENDER_INFO_OBJECTS_IN_FRAME)) + "\n";
  2115. text += TTR("Material Changes:") + " " + itos(viewport->get_render_info(Viewport::RENDER_INFO_MATERIAL_CHANGES_IN_FRAME)) + "\n";
  2116. text += TTR("Shader Changes:") + " " + itos(viewport->get_render_info(Viewport::RENDER_INFO_SHADER_CHANGES_IN_FRAME)) + "\n";
  2117. text += TTR("Surface Changes:") + " " + itos(viewport->get_render_info(Viewport::RENDER_INFO_SURFACE_CHANGES_IN_FRAME)) + "\n";
  2118. text += TTR("Draw Calls:") + " " + itos(viewport->get_render_info(Viewport::RENDER_INFO_DRAW_CALLS_IN_FRAME)) + "\n";
  2119. text += TTR("Vertices:") + " " + itos(viewport->get_render_info(Viewport::RENDER_INFO_VERTICES_IN_FRAME));
  2120. info_label->set_text(text);
  2121. }
  2122. // FPS Counter.
  2123. bool show_fps = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FPS));
  2124. fps_label->set_visible(show_fps);
  2125. if (show_fps) {
  2126. String text;
  2127. const float temp_fps = Engine::get_singleton()->get_frames_per_second();
  2128. text += vformat(TTR("FPS: %d (%s ms)"), temp_fps, String::num(1000.0f / temp_fps, 2));
  2129. fps_label->set_text(text);
  2130. }
  2131. bool show_cinema = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW));
  2132. cinema_label->set_visible(show_cinema);
  2133. if (show_cinema) {
  2134. float cinema_half_width = cinema_label->get_size().width / 2.0f;
  2135. cinema_label->set_anchor_and_margin(MARGIN_LEFT, 0.5f, -cinema_half_width);
  2136. }
  2137. if (lock_rotation) {
  2138. float locked_half_width = locked_label->get_size().width / 2.0f;
  2139. locked_label->set_anchor_and_margin(MARGIN_LEFT, 0.5f, -locked_half_width);
  2140. }
  2141. }
  2142. if (p_what == NOTIFICATION_ENTER_TREE) {
  2143. surface->connect("draw", this, "_draw");
  2144. surface->connect("gui_input", this, "_sinput");
  2145. surface->connect("mouse_entered", this, "_surface_mouse_enter");
  2146. surface->connect("mouse_exited", this, "_surface_mouse_exit");
  2147. surface->connect("focus_entered", this, "_surface_focus_enter");
  2148. surface->connect("focus_exited", this, "_surface_focus_exit");
  2149. _init_gizmo_instance(index);
  2150. }
  2151. if (p_what == NOTIFICATION_EXIT_TREE) {
  2152. _finish_gizmo_instances();
  2153. }
  2154. if (p_what == NOTIFICATION_THEME_CHANGED) {
  2155. view_menu->set_icon(get_icon("GuiTabMenuHl", "EditorIcons"));
  2156. preview_camera->set_icon(get_icon("Camera", "EditorIcons"));
  2157. view_menu->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2158. view_menu->add_style_override("hover", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2159. view_menu->add_style_override("pressed", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2160. view_menu->add_style_override("focus", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2161. view_menu->add_style_override("disabled", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2162. preview_camera->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2163. preview_camera->add_style_override("hover", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2164. preview_camera->add_style_override("pressed", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2165. preview_camera->add_style_override("focus", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2166. preview_camera->add_style_override("disabled", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2167. info_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2168. fps_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2169. cinema_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2170. locked_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  2171. }
  2172. }
  2173. static void draw_indicator_bar(Control &surface, real_t fill, const Ref<Texture> icon, const Ref<Font> font, const String &text) {
  2174. // Adjust bar size from control height
  2175. const Vector2 surface_size = surface.get_size();
  2176. const real_t h = surface_size.y / 2.0;
  2177. const real_t y = (surface_size.y - h) / 2.0;
  2178. const Rect2 r(10 * EDSCALE, y, 6 * EDSCALE, h);
  2179. const real_t sy = r.size.y * fill;
  2180. // Note: because this bar appears over the viewport, it has to stay readable for any background color
  2181. // Draw both neutral dark and bright colors to account this
  2182. surface.draw_rect(r, Color(1, 1, 1, 0.2));
  2183. surface.draw_rect(Rect2(r.position.x, r.position.y + r.size.y - sy, r.size.x, sy), Color(1, 1, 1, 0.6));
  2184. surface.draw_rect(r.grow(1), Color(0, 0, 0, 0.7), false, Math::round(EDSCALE));
  2185. const Vector2 icon_size = icon->get_size();
  2186. const Vector2 icon_pos = Vector2(r.position.x - (icon_size.x - r.size.x) / 2, r.position.y + r.size.y + 2 * EDSCALE);
  2187. surface.draw_texture(icon, icon_pos);
  2188. // Draw text below the bar (for speed/zoom information).
  2189. surface.draw_string(font, Vector2(icon_pos.x, icon_pos.y + icon_size.y + 16 * EDSCALE), text);
  2190. }
  2191. void SpatialEditorViewport::_draw() {
  2192. EditorPluginList *over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_over();
  2193. if (!over_plugin_list->empty()) {
  2194. over_plugin_list->forward_spatial_draw_over_viewport(surface);
  2195. }
  2196. EditorPluginList *force_over_plugin_list = editor->get_editor_plugins_force_over();
  2197. if (!force_over_plugin_list->empty()) {
  2198. force_over_plugin_list->forward_spatial_force_draw_over_viewport(surface);
  2199. }
  2200. if (surface->has_focus()) {
  2201. Size2 size = surface->get_size();
  2202. Rect2 r = Rect2(Point2(), size);
  2203. get_stylebox("Focus", "EditorStyles")->draw(surface->get_canvas_item(), r);
  2204. }
  2205. if (cursor.region_select) {
  2206. const Rect2 selection_rect = Rect2(cursor.region_begin, cursor.region_end - cursor.region_begin);
  2207. surface->draw_rect(
  2208. selection_rect,
  2209. get_color("box_selection_fill_color", "Editor"));
  2210. surface->draw_rect(
  2211. selection_rect,
  2212. get_color("box_selection_stroke_color", "Editor"),
  2213. false,
  2214. Math::round(EDSCALE));
  2215. }
  2216. RID ci = surface->get_canvas_item();
  2217. if (message_time > 0) {
  2218. Ref<Font> font = get_font("font", "Label");
  2219. Point2 msgpos = Point2(5, get_size().y - 20);
  2220. font->draw(ci, msgpos + Point2(1, 1), message, Color(0, 0, 0, 0.8));
  2221. font->draw(ci, msgpos + Point2(-1, -1), message, Color(0, 0, 0, 0.8));
  2222. font->draw(ci, msgpos, message, Color(1, 1, 1, 1));
  2223. }
  2224. if (_edit.mode == TRANSFORM_ROTATE) {
  2225. Point2 center = _point_to_screen(_edit.center);
  2226. Color handle_color;
  2227. switch (_edit.plane) {
  2228. case TRANSFORM_X_AXIS:
  2229. handle_color = get_color("axis_x_color", "Editor");
  2230. break;
  2231. case TRANSFORM_Y_AXIS:
  2232. handle_color = get_color("axis_y_color", "Editor");
  2233. break;
  2234. case TRANSFORM_Z_AXIS:
  2235. handle_color = get_color("axis_z_color", "Editor");
  2236. break;
  2237. default:
  2238. handle_color = get_color("accent_color", "Editor");
  2239. break;
  2240. }
  2241. handle_color = handle_color.from_hsv(handle_color.get_h(), 0.25, 1.0, 1);
  2242. VisualServer::get_singleton()->canvas_item_add_line(
  2243. ci,
  2244. _edit.mouse_pos,
  2245. center,
  2246. handle_color,
  2247. Math::round(2 * EDSCALE),
  2248. true);
  2249. }
  2250. if (previewing) {
  2251. Size2 ss = Size2(ProjectSettings::get_singleton()->get("display/window/size/width"), ProjectSettings::get_singleton()->get("display/window/size/height"));
  2252. float aspect = ss.aspect();
  2253. Size2 s = get_size();
  2254. Rect2 draw_rect;
  2255. switch (previewing->get_keep_aspect_mode()) {
  2256. case Camera::KEEP_WIDTH: {
  2257. draw_rect.size = Size2(s.width, s.width / aspect);
  2258. draw_rect.position.x = 0;
  2259. draw_rect.position.y = (s.height - draw_rect.size.y) * 0.5;
  2260. } break;
  2261. case Camera::KEEP_HEIGHT: {
  2262. draw_rect.size = Size2(s.height * aspect, s.height);
  2263. draw_rect.position.y = 0;
  2264. draw_rect.position.x = (s.width - draw_rect.size.x) * 0.5;
  2265. } break;
  2266. }
  2267. draw_rect = Rect2(Vector2(), s).clip(draw_rect);
  2268. surface->draw_rect(draw_rect, Color(0.6, 0.6, 0.1, 0.5), false, Math::round(2 * EDSCALE));
  2269. } else {
  2270. if (zoom_indicator_delay > 0.0) {
  2271. if (is_freelook_active()) {
  2272. // Show speed
  2273. real_t min_speed = MAX(camera->get_znear() * 4, ZOOM_FREELOOK_MIN);
  2274. real_t max_speed = MIN(camera->get_zfar() / 2, ZOOM_FREELOOK_MAX);
  2275. real_t scale_length = (max_speed - min_speed);
  2276. if (!Math::is_zero_approx(scale_length)) {
  2277. real_t logscale_t = 1.0 - Math::log(1 + freelook_speed - min_speed) / Math::log(1 + scale_length);
  2278. // Display the freelook speed to help the user get a better sense of scale.
  2279. const int precision = freelook_speed < 1.0 ? 2 : 1;
  2280. draw_indicator_bar(
  2281. *surface,
  2282. 1.0 - logscale_t,
  2283. get_icon("ViewportSpeed", "EditorIcons"),
  2284. get_font("font", "Label"),
  2285. vformat("%s u/s", String::num(freelook_speed).pad_decimals(precision)));
  2286. }
  2287. } else {
  2288. // Show zoom
  2289. zoom_limit_label->set_visible(zoom_failed_attempts_count > 15);
  2290. real_t min_distance = MAX(camera->get_znear() * 4, ZOOM_FREELOOK_MIN);
  2291. real_t max_distance = MIN(camera->get_zfar() / 2, ZOOM_FREELOOK_MAX);
  2292. real_t scale_length = (max_distance - min_distance);
  2293. if (!Math::is_zero_approx(scale_length)) {
  2294. real_t logscale_t = 1.0 - Math::log(1 + cursor.distance - min_distance) / Math::log(1 + scale_length);
  2295. // Display the zoom center distance to help the user get a better sense of scale.
  2296. const int precision = cursor.distance < 1.0 ? 2 : 1;
  2297. draw_indicator_bar(
  2298. *surface,
  2299. logscale_t,
  2300. get_icon("ViewportZoom", "EditorIcons"),
  2301. get_font("font", "Label"),
  2302. vformat("%s u", String::num(cursor.distance).pad_decimals(precision)));
  2303. }
  2304. }
  2305. }
  2306. }
  2307. }
  2308. void SpatialEditorViewport::_menu_option(int p_option) {
  2309. switch (p_option) {
  2310. case VIEW_TOP: {
  2311. cursor.y_rot = 0;
  2312. cursor.x_rot = Math_PI / 2.0;
  2313. set_message(TTR("Top View."), 2);
  2314. view_type = VIEW_TYPE_TOP;
  2315. _set_auto_orthogonal();
  2316. _update_name();
  2317. } break;
  2318. case VIEW_BOTTOM: {
  2319. cursor.y_rot = 0;
  2320. cursor.x_rot = -Math_PI / 2.0;
  2321. set_message(TTR("Bottom View."), 2);
  2322. view_type = VIEW_TYPE_BOTTOM;
  2323. _set_auto_orthogonal();
  2324. _update_name();
  2325. } break;
  2326. case VIEW_LEFT: {
  2327. cursor.x_rot = 0;
  2328. cursor.y_rot = Math_PI / 2.0;
  2329. set_message(TTR("Left View."), 2);
  2330. view_type = VIEW_TYPE_LEFT;
  2331. _set_auto_orthogonal();
  2332. _update_name();
  2333. } break;
  2334. case VIEW_RIGHT: {
  2335. cursor.x_rot = 0;
  2336. cursor.y_rot = -Math_PI / 2.0;
  2337. set_message(TTR("Right View."), 2);
  2338. view_type = VIEW_TYPE_RIGHT;
  2339. _set_auto_orthogonal();
  2340. _update_name();
  2341. } break;
  2342. case VIEW_FRONT: {
  2343. cursor.x_rot = 0;
  2344. cursor.y_rot = Math_PI;
  2345. set_message(TTR("Front View."), 2);
  2346. view_type = VIEW_TYPE_FRONT;
  2347. _set_auto_orthogonal();
  2348. _update_name();
  2349. } break;
  2350. case VIEW_REAR: {
  2351. cursor.x_rot = 0;
  2352. cursor.y_rot = 0;
  2353. set_message(TTR("Rear View."), 2);
  2354. view_type = VIEW_TYPE_REAR;
  2355. _set_auto_orthogonal();
  2356. _update_name();
  2357. } break;
  2358. case VIEW_CENTER_TO_ORIGIN: {
  2359. cursor.pos = Vector3(0, 0, 0);
  2360. } break;
  2361. case VIEW_CENTER_TO_SELECTION: {
  2362. focus_selection();
  2363. } break;
  2364. case VIEW_ALIGN_TRANSFORM_WITH_VIEW: {
  2365. if (!get_selected_count()) {
  2366. break;
  2367. }
  2368. Transform camera_transform = camera->get_global_transform();
  2369. List<Node *> &selection = editor_selection->get_selected_node_list();
  2370. undo_redo->create_action(TTR("Align Transform with View"));
  2371. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2372. Spatial *sp = Object::cast_to<Spatial>(E->get());
  2373. if (!sp) {
  2374. continue;
  2375. }
  2376. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  2377. if (!se) {
  2378. continue;
  2379. }
  2380. Transform xform;
  2381. if (orthogonal) {
  2382. xform = sp->get_global_transform();
  2383. xform.basis.set_euler(camera_transform.basis.get_euler());
  2384. } else {
  2385. xform = camera_transform;
  2386. xform.scale_basis(sp->get_scale());
  2387. }
  2388. undo_redo->add_do_method(sp, "set_global_transform", xform);
  2389. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  2390. }
  2391. undo_redo->commit_action();
  2392. } break;
  2393. case VIEW_ALIGN_ROTATION_WITH_VIEW: {
  2394. if (!get_selected_count()) {
  2395. break;
  2396. }
  2397. Transform camera_transform = camera->get_global_transform();
  2398. List<Node *> &selection = editor_selection->get_selected_node_list();
  2399. undo_redo->create_action(TTR("Align Rotation with View"));
  2400. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2401. Spatial *sp = Object::cast_to<Spatial>(E->get());
  2402. if (!sp) {
  2403. continue;
  2404. }
  2405. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  2406. if (!se) {
  2407. continue;
  2408. }
  2409. undo_redo->add_do_method(sp, "set_rotation", camera_transform.basis.get_rotation());
  2410. undo_redo->add_undo_method(sp, "set_rotation", sp->get_rotation());
  2411. }
  2412. undo_redo->commit_action();
  2413. } break;
  2414. case VIEW_ENVIRONMENT: {
  2415. int idx = view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT);
  2416. bool current = view_menu->get_popup()->is_item_checked(idx);
  2417. current = !current;
  2418. if (current) {
  2419. camera->set_environment(RES());
  2420. } else {
  2421. camera->set_environment(SpatialEditor::get_singleton()->get_viewport_environment());
  2422. }
  2423. view_menu->get_popup()->set_item_checked(idx, current);
  2424. } break;
  2425. case VIEW_PERSPECTIVE: {
  2426. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  2427. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), false);
  2428. orthogonal = false;
  2429. auto_orthogonal = false;
  2430. call_deferred("update_transform_gizmo_view");
  2431. _update_name();
  2432. } break;
  2433. case VIEW_ORTHOGONAL: {
  2434. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), false);
  2435. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), true);
  2436. orthogonal = true;
  2437. auto_orthogonal = false;
  2438. call_deferred("update_transform_gizmo_view");
  2439. _update_name();
  2440. } break;
  2441. case VIEW_AUTO_ORTHOGONAL: {
  2442. int idx = view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL);
  2443. bool current = view_menu->get_popup()->is_item_checked(idx);
  2444. current = !current;
  2445. view_menu->get_popup()->set_item_checked(idx, current);
  2446. if (auto_orthogonal) {
  2447. auto_orthogonal = false;
  2448. _update_name();
  2449. }
  2450. } break;
  2451. case VIEW_LOCK_ROTATION: {
  2452. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  2453. bool current = view_menu->get_popup()->is_item_checked(idx);
  2454. lock_rotation = !current;
  2455. view_menu->get_popup()->set_item_checked(idx, !current);
  2456. if (lock_rotation) {
  2457. locked_label->show();
  2458. } else {
  2459. locked_label->hide();
  2460. }
  2461. } break;
  2462. case VIEW_AUDIO_LISTENER: {
  2463. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  2464. bool current = view_menu->get_popup()->is_item_checked(idx);
  2465. current = !current;
  2466. viewport->set_as_audio_listener(current);
  2467. view_menu->get_popup()->set_item_checked(idx, current);
  2468. } break;
  2469. case VIEW_AUDIO_DOPPLER: {
  2470. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  2471. bool current = view_menu->get_popup()->is_item_checked(idx);
  2472. current = !current;
  2473. camera->set_doppler_tracking(current ? Camera::DOPPLER_TRACKING_IDLE_STEP : Camera::DOPPLER_TRACKING_DISABLED);
  2474. view_menu->get_popup()->set_item_checked(idx, current);
  2475. } break;
  2476. case VIEW_CINEMATIC_PREVIEW: {
  2477. int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW);
  2478. bool current = view_menu->get_popup()->is_item_checked(idx);
  2479. current = !current;
  2480. view_menu->get_popup()->set_item_checked(idx, current);
  2481. previewing_cinema = true;
  2482. _toggle_cinema_preview(current);
  2483. if (current) {
  2484. preview_camera->hide();
  2485. } else {
  2486. if (previewing != nullptr) {
  2487. preview_camera->show();
  2488. }
  2489. }
  2490. } break;
  2491. case VIEW_GIZMOS: {
  2492. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  2493. bool current = view_menu->get_popup()->is_item_checked(idx);
  2494. current = !current;
  2495. uint32_t layers = ((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + index)) | (1 << GIZMO_GRID_LAYER) | (1 << MISC_TOOL_LAYER);
  2496. if (current) {
  2497. layers |= (1 << GIZMO_EDIT_LAYER);
  2498. }
  2499. camera->set_cull_mask(layers);
  2500. view_menu->get_popup()->set_item_checked(idx, current);
  2501. } break;
  2502. case VIEW_HALF_RESOLUTION: {
  2503. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  2504. bool current = view_menu->get_popup()->is_item_checked(idx);
  2505. current = !current;
  2506. view_menu->get_popup()->set_item_checked(idx, current);
  2507. } break;
  2508. case VIEW_INFORMATION: {
  2509. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  2510. bool current = view_menu->get_popup()->is_item_checked(idx);
  2511. view_menu->get_popup()->set_item_checked(idx, !current);
  2512. } break;
  2513. case VIEW_FPS: {
  2514. int idx = view_menu->get_popup()->get_item_index(VIEW_FPS);
  2515. bool current = view_menu->get_popup()->is_item_checked(idx);
  2516. view_menu->get_popup()->set_item_checked(idx, !current);
  2517. } break;
  2518. case VIEW_DISPLAY_NORMAL: {
  2519. viewport->set_debug_draw(Viewport::DEBUG_DRAW_DISABLED);
  2520. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true);
  2521. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  2522. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  2523. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  2524. } break;
  2525. case VIEW_DISPLAY_WIREFRAME: {
  2526. viewport->set_debug_draw(Viewport::DEBUG_DRAW_WIREFRAME);
  2527. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  2528. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), true);
  2529. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  2530. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  2531. } break;
  2532. case VIEW_DISPLAY_OVERDRAW: {
  2533. viewport->set_debug_draw(Viewport::DEBUG_DRAW_OVERDRAW);
  2534. VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_OVERDRAW);
  2535. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  2536. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  2537. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), true);
  2538. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  2539. } break;
  2540. case VIEW_DISPLAY_SHADELESS: {
  2541. viewport->set_debug_draw(Viewport::DEBUG_DRAW_UNSHADED);
  2542. VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_SHADELESS);
  2543. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  2544. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  2545. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  2546. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), true);
  2547. } break;
  2548. }
  2549. }
  2550. void SpatialEditorViewport::_set_auto_orthogonal() {
  2551. if (!orthogonal && view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL))) {
  2552. _menu_option(VIEW_ORTHOGONAL);
  2553. auto_orthogonal = true;
  2554. }
  2555. }
  2556. void SpatialEditorViewport::_preview_exited_scene() {
  2557. preview_camera->disconnect("toggled", this, "_toggle_camera_preview");
  2558. preview_camera->set_pressed(false);
  2559. _toggle_camera_preview(false);
  2560. preview_camera->connect("toggled", this, "_toggle_camera_preview");
  2561. view_menu->show();
  2562. }
  2563. void SpatialEditorViewport::_init_gizmo_instance(int p_idx) {
  2564. uint32_t layer = 1 << (GIZMO_BASE_LAYER + p_idx);
  2565. for (int i = 0; i < 3; i++) {
  2566. move_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2567. VS::get_singleton()->instance_set_base(move_gizmo_instance[i], spatial_editor->get_move_gizmo(i)->get_rid());
  2568. VS::get_singleton()->instance_set_scenario(move_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2569. VS::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  2570. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2571. VS::get_singleton()->instance_set_layer_mask(move_gizmo_instance[i], layer);
  2572. VS::get_singleton()->instance_set_portal_mode(move_gizmo_instance[i], VisualServer::INSTANCE_PORTAL_MODE_GLOBAL);
  2573. move_plane_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2574. VS::get_singleton()->instance_set_base(move_plane_gizmo_instance[i], spatial_editor->get_move_plane_gizmo(i)->get_rid());
  2575. VS::get_singleton()->instance_set_scenario(move_plane_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2576. VS::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  2577. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_plane_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2578. VS::get_singleton()->instance_set_layer_mask(move_plane_gizmo_instance[i], layer);
  2579. VS::get_singleton()->instance_set_portal_mode(move_plane_gizmo_instance[i], VisualServer::INSTANCE_PORTAL_MODE_GLOBAL);
  2580. rotate_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2581. VS::get_singleton()->instance_set_base(rotate_gizmo_instance[i], spatial_editor->get_rotate_gizmo(i)->get_rid());
  2582. VS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2583. VS::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  2584. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2585. VS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[i], layer);
  2586. VS::get_singleton()->instance_set_portal_mode(rotate_gizmo_instance[i], VisualServer::INSTANCE_PORTAL_MODE_GLOBAL);
  2587. scale_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2588. VS::get_singleton()->instance_set_base(scale_gizmo_instance[i], spatial_editor->get_scale_gizmo(i)->get_rid());
  2589. VS::get_singleton()->instance_set_scenario(scale_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2590. VS::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  2591. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2592. VS::get_singleton()->instance_set_layer_mask(scale_gizmo_instance[i], layer);
  2593. VS::get_singleton()->instance_set_portal_mode(scale_gizmo_instance[i], VisualServer::INSTANCE_PORTAL_MODE_GLOBAL);
  2594. scale_plane_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2595. VS::get_singleton()->instance_set_base(scale_plane_gizmo_instance[i], spatial_editor->get_scale_plane_gizmo(i)->get_rid());
  2596. VS::get_singleton()->instance_set_scenario(scale_plane_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2597. VS::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  2598. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_plane_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2599. VS::get_singleton()->instance_set_layer_mask(scale_plane_gizmo_instance[i], layer);
  2600. VS::get_singleton()->instance_set_portal_mode(scale_plane_gizmo_instance[i], VisualServer::INSTANCE_PORTAL_MODE_GLOBAL);
  2601. }
  2602. // Rotation white outline
  2603. rotate_gizmo_instance[3] = VS::get_singleton()->instance_create();
  2604. VS::get_singleton()->instance_set_base(rotate_gizmo_instance[3], spatial_editor->get_rotate_gizmo(3)->get_rid());
  2605. VS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[3], get_tree()->get_root()->get_world()->get_scenario());
  2606. VS::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false);
  2607. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[3], VS::SHADOW_CASTING_SETTING_OFF);
  2608. VS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[3], layer);
  2609. }
  2610. void SpatialEditorViewport::_finish_gizmo_instances() {
  2611. for (int i = 0; i < 3; i++) {
  2612. VS::get_singleton()->free(move_gizmo_instance[i]);
  2613. VS::get_singleton()->free(move_plane_gizmo_instance[i]);
  2614. VS::get_singleton()->free(rotate_gizmo_instance[i]);
  2615. VS::get_singleton()->free(scale_gizmo_instance[i]);
  2616. VS::get_singleton()->free(scale_plane_gizmo_instance[i]);
  2617. }
  2618. // Rotation white outline
  2619. VS::get_singleton()->free(rotate_gizmo_instance[3]);
  2620. }
  2621. void SpatialEditorViewport::_toggle_camera_preview(bool p_activate) {
  2622. ERR_FAIL_COND(p_activate && !preview);
  2623. ERR_FAIL_COND(!p_activate && !previewing);
  2624. rotation_control->set_visible(!p_activate);
  2625. if (!p_activate) {
  2626. previewing->disconnect("tree_exiting", this, "_preview_exited_scene");
  2627. previewing = nullptr;
  2628. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  2629. if (!preview) {
  2630. preview_camera->hide();
  2631. }
  2632. surface->update();
  2633. } else {
  2634. previewing = preview;
  2635. previewing->connect("tree_exiting", this, "_preview_exited_scene");
  2636. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), preview->get_camera()); //replace
  2637. surface->update();
  2638. }
  2639. }
  2640. void SpatialEditorViewport::_toggle_cinema_preview(bool p_activate) {
  2641. previewing_cinema = p_activate;
  2642. rotation_control->set_visible(!p_activate);
  2643. if (!previewing_cinema) {
  2644. if (previewing != nullptr) {
  2645. previewing->disconnect("tree_exited", this, "_preview_exited_scene");
  2646. }
  2647. previewing = nullptr;
  2648. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  2649. preview_camera->set_pressed(false);
  2650. if (!preview) {
  2651. preview_camera->hide();
  2652. } else {
  2653. preview_camera->show();
  2654. }
  2655. view_menu->show();
  2656. surface->update();
  2657. }
  2658. }
  2659. void SpatialEditorViewport::_selection_result_pressed(int p_result) {
  2660. if (selection_results.size() <= p_result) {
  2661. return;
  2662. }
  2663. clicked = selection_results[p_result].item->get_instance_id();
  2664. if (clicked) {
  2665. _select_clicked(clicked_wants_append, true, spatial_editor->get_tool_mode() != SpatialEditor::TOOL_MODE_LIST_SELECT);
  2666. clicked = 0;
  2667. }
  2668. }
  2669. void SpatialEditorViewport::_selection_menu_hide() {
  2670. selection_results.clear();
  2671. selection_menu->clear();
  2672. selection_menu->set_size(Vector2(0, 0));
  2673. }
  2674. void SpatialEditorViewport::set_can_preview(Camera *p_preview) {
  2675. preview = p_preview;
  2676. if (!preview_camera->is_pressed() && !previewing_cinema) {
  2677. preview_camera->set_visible(p_preview);
  2678. }
  2679. }
  2680. void SpatialEditorViewport::update_transform_gizmo_view() {
  2681. if (!is_visible_in_tree()) {
  2682. return;
  2683. }
  2684. Transform xform = spatial_editor->get_gizmo_transform();
  2685. Transform camera_xform = camera->get_transform();
  2686. if (xform.origin.distance_squared_to(camera_xform.origin) < 0.01) {
  2687. for (int i = 0; i < 3; i++) {
  2688. VisualServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  2689. VisualServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  2690. VisualServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  2691. VisualServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  2692. VisualServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  2693. }
  2694. // Rotation white outline
  2695. VisualServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false);
  2696. return;
  2697. }
  2698. Vector3 camz = -camera_xform.get_basis().get_axis(2).normalized();
  2699. Vector3 camy = -camera_xform.get_basis().get_axis(1).normalized();
  2700. Plane p(camera_xform.origin, camz);
  2701. float gizmo_d = MAX(Math::abs(p.distance_to(xform.origin)), CMP_EPSILON);
  2702. float d0 = camera->unproject_position(camera_xform.origin + camz * gizmo_d).y;
  2703. float d1 = camera->unproject_position(camera_xform.origin + camz * gizmo_d + camy).y;
  2704. float dd = Math::abs(d0 - d1);
  2705. if (dd == 0) {
  2706. dd = 0.0001;
  2707. }
  2708. float gizmo_size = EditorSettings::get_singleton()->get("editors/3d/manipulator_gizmo_size");
  2709. // At low viewport heights, multiply the gizmo scale based on the viewport height.
  2710. // This prevents the gizmo from growing very large and going outside the viewport.
  2711. const int viewport_base_height = 400 * MAX(1, EDSCALE);
  2712. gizmo_scale =
  2713. (gizmo_size / Math::abs(dd)) * MAX(1, EDSCALE) *
  2714. MIN(viewport_base_height, viewport_container->get_size().height) / viewport_base_height /
  2715. viewport_container->get_stretch_shrink();
  2716. Vector3 scale = Vector3(1, 1, 1) * gizmo_scale;
  2717. xform.basis.scale(scale);
  2718. for (int i = 0; i < 3; i++) {
  2719. VisualServer::get_singleton()->instance_set_transform(move_gizmo_instance[i], xform);
  2720. VisualServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE));
  2721. VisualServer::get_singleton()->instance_set_transform(move_plane_gizmo_instance[i], xform);
  2722. VisualServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE));
  2723. VisualServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[i], xform);
  2724. VisualServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE));
  2725. VisualServer::get_singleton()->instance_set_transform(scale_gizmo_instance[i], xform);
  2726. VisualServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE));
  2727. VisualServer::get_singleton()->instance_set_transform(scale_plane_gizmo_instance[i], xform);
  2728. VisualServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE));
  2729. }
  2730. // Rotation white outline
  2731. VisualServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[3], xform);
  2732. VisualServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE));
  2733. }
  2734. void SpatialEditorViewport::set_state(const Dictionary &p_state) {
  2735. if (p_state.has("position")) {
  2736. cursor.pos = p_state["position"];
  2737. }
  2738. if (p_state.has("x_rotation")) {
  2739. cursor.x_rot = p_state["x_rotation"];
  2740. }
  2741. if (p_state.has("y_rotation")) {
  2742. cursor.y_rot = p_state["y_rotation"];
  2743. }
  2744. if (p_state.has("distance")) {
  2745. cursor.distance = p_state["distance"];
  2746. }
  2747. if (p_state.has("use_orthogonal")) {
  2748. bool orth = p_state["use_orthogonal"];
  2749. if (orth) {
  2750. _menu_option(VIEW_ORTHOGONAL);
  2751. } else {
  2752. _menu_option(VIEW_PERSPECTIVE);
  2753. }
  2754. }
  2755. if (p_state.has("view_type")) {
  2756. view_type = ViewType(p_state["view_type"].operator int());
  2757. _update_name();
  2758. }
  2759. if (p_state.has("auto_orthogonal")) {
  2760. auto_orthogonal = p_state["auto_orthogonal"];
  2761. _update_name();
  2762. }
  2763. if (p_state.has("auto_orthogonal_enabled")) {
  2764. bool enabled = p_state["auto_orthogonal_enabled"];
  2765. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL), enabled);
  2766. }
  2767. if (p_state.has("display_mode")) {
  2768. int display = p_state["display_mode"];
  2769. int idx = view_menu->get_popup()->get_item_index(display);
  2770. if (!view_menu->get_popup()->is_item_checked(idx)) {
  2771. _menu_option(display);
  2772. }
  2773. }
  2774. if (p_state.has("lock_rotation")) {
  2775. lock_rotation = p_state["lock_rotation"];
  2776. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  2777. view_menu->get_popup()->set_item_checked(idx, lock_rotation);
  2778. }
  2779. if (p_state.has("use_environment")) {
  2780. bool env = p_state["use_environment"];
  2781. if (env != camera->get_environment().is_valid()) {
  2782. _menu_option(VIEW_ENVIRONMENT);
  2783. }
  2784. }
  2785. if (p_state.has("listener")) {
  2786. bool listener = p_state["listener"];
  2787. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  2788. viewport->set_as_audio_listener(listener);
  2789. view_menu->get_popup()->set_item_checked(idx, listener);
  2790. }
  2791. if (p_state.has("doppler")) {
  2792. bool doppler = p_state["doppler"];
  2793. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  2794. camera->set_doppler_tracking(doppler ? Camera::DOPPLER_TRACKING_IDLE_STEP : Camera::DOPPLER_TRACKING_DISABLED);
  2795. view_menu->get_popup()->set_item_checked(idx, doppler);
  2796. }
  2797. if (p_state.has("gizmos")) {
  2798. bool gizmos = p_state["gizmos"];
  2799. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  2800. if (view_menu->get_popup()->is_item_checked(idx) != gizmos) {
  2801. _menu_option(VIEW_GIZMOS);
  2802. }
  2803. }
  2804. if (p_state.has("information")) {
  2805. bool information = p_state["information"];
  2806. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  2807. if (view_menu->get_popup()->is_item_checked(idx) != information) {
  2808. _menu_option(VIEW_INFORMATION);
  2809. }
  2810. }
  2811. if (p_state.has("fps")) {
  2812. bool fps = p_state["fps"];
  2813. int idx = view_menu->get_popup()->get_item_index(VIEW_FPS);
  2814. if (view_menu->get_popup()->is_item_checked(idx) != fps) {
  2815. _menu_option(VIEW_FPS);
  2816. }
  2817. }
  2818. if (p_state.has("half_res")) {
  2819. bool half_res = p_state["half_res"];
  2820. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  2821. view_menu->get_popup()->set_item_checked(idx, half_res);
  2822. }
  2823. if (p_state.has("cinematic_preview")) {
  2824. previewing_cinema = p_state["cinematic_preview"];
  2825. int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW);
  2826. view_menu->get_popup()->set_item_checked(idx, previewing_cinema);
  2827. }
  2828. if (preview_camera->is_connected("toggled", this, "_toggle_camera_preview")) {
  2829. preview_camera->disconnect("toggled", this, "_toggle_camera_preview");
  2830. }
  2831. if (p_state.has("previewing")) {
  2832. Node *pv = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["previewing"]);
  2833. if (Object::cast_to<Camera>(pv)) {
  2834. previewing = Object::cast_to<Camera>(pv);
  2835. previewing->connect("tree_exiting", this, "_preview_exited_scene");
  2836. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), previewing->get_camera()); //replace
  2837. surface->update();
  2838. preview_camera->set_pressed(true);
  2839. preview_camera->show();
  2840. }
  2841. }
  2842. preview_camera->connect("toggled", this, "_toggle_camera_preview");
  2843. }
  2844. Dictionary SpatialEditorViewport::get_state() const {
  2845. Dictionary d;
  2846. d["position"] = cursor.pos;
  2847. d["x_rotation"] = cursor.x_rot;
  2848. d["y_rotation"] = cursor.y_rot;
  2849. d["distance"] = cursor.distance;
  2850. d["use_environment"] = camera->get_environment().is_valid();
  2851. d["use_orthogonal"] = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL;
  2852. d["view_type"] = view_type;
  2853. d["auto_orthogonal"] = auto_orthogonal;
  2854. d["auto_orthogonal_enabled"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL));
  2855. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL))) {
  2856. d["display_mode"] = VIEW_DISPLAY_NORMAL;
  2857. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME))) {
  2858. d["display_mode"] = VIEW_DISPLAY_WIREFRAME;
  2859. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW))) {
  2860. d["display_mode"] = VIEW_DISPLAY_OVERDRAW;
  2861. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS))) {
  2862. d["display_mode"] = VIEW_DISPLAY_SHADELESS;
  2863. }
  2864. d["listener"] = viewport->is_audio_listener();
  2865. d["doppler"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER));
  2866. d["gizmos"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS));
  2867. d["information"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  2868. d["fps"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FPS));
  2869. d["half_res"] = viewport_container->get_stretch_shrink() > 1;
  2870. d["cinematic_preview"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW));
  2871. if (previewing) {
  2872. d["previewing"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(previewing);
  2873. }
  2874. if (lock_rotation) {
  2875. d["lock_rotation"] = lock_rotation;
  2876. }
  2877. return d;
  2878. }
  2879. void SpatialEditorViewport::_bind_methods() {
  2880. ClassDB::bind_method(D_METHOD("_draw"), &SpatialEditorViewport::_draw);
  2881. ClassDB::bind_method(D_METHOD("_surface_mouse_enter"), &SpatialEditorViewport::_surface_mouse_enter);
  2882. ClassDB::bind_method(D_METHOD("_surface_mouse_exit"), &SpatialEditorViewport::_surface_mouse_exit);
  2883. ClassDB::bind_method(D_METHOD("_surface_focus_enter"), &SpatialEditorViewport::_surface_focus_enter);
  2884. ClassDB::bind_method(D_METHOD("_surface_focus_exit"), &SpatialEditorViewport::_surface_focus_exit);
  2885. ClassDB::bind_method(D_METHOD("_sinput"), &SpatialEditorViewport::_sinput);
  2886. ClassDB::bind_method(D_METHOD("_menu_option"), &SpatialEditorViewport::_menu_option);
  2887. ClassDB::bind_method(D_METHOD("_toggle_camera_preview"), &SpatialEditorViewport::_toggle_camera_preview);
  2888. ClassDB::bind_method(D_METHOD("_preview_exited_scene"), &SpatialEditorViewport::_preview_exited_scene);
  2889. ClassDB::bind_method(D_METHOD("_update_camera"), &SpatialEditorViewport::_update_camera);
  2890. ClassDB::bind_method(D_METHOD("update_transform_gizmo_view"), &SpatialEditorViewport::update_transform_gizmo_view);
  2891. ClassDB::bind_method(D_METHOD("_selection_result_pressed"), &SpatialEditorViewport::_selection_result_pressed);
  2892. ClassDB::bind_method(D_METHOD("_selection_menu_hide"), &SpatialEditorViewport::_selection_menu_hide);
  2893. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SpatialEditorViewport::can_drop_data_fw);
  2894. ClassDB::bind_method(D_METHOD("drop_data_fw"), &SpatialEditorViewport::drop_data_fw);
  2895. ADD_SIGNAL(MethodInfo("toggle_maximize_view", PropertyInfo(Variant::OBJECT, "viewport")));
  2896. ADD_SIGNAL(MethodInfo("clicked", PropertyInfo(Variant::OBJECT, "viewport")));
  2897. }
  2898. void SpatialEditorViewport::reset() {
  2899. orthogonal = false;
  2900. auto_orthogonal = false;
  2901. lock_rotation = false;
  2902. message_time = 0;
  2903. message = "";
  2904. last_message = "";
  2905. view_type = VIEW_TYPE_USER;
  2906. cursor = Cursor();
  2907. _update_name();
  2908. }
  2909. void SpatialEditorViewport::focus_selection() {
  2910. Vector3 center;
  2911. int count = 0;
  2912. List<Node *> &selection = editor_selection->get_selected_node_list();
  2913. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2914. Spatial *sp = Object::cast_to<Spatial>(E->get());
  2915. if (!sp) {
  2916. continue;
  2917. }
  2918. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  2919. if (!se) {
  2920. continue;
  2921. }
  2922. center += sp->get_global_gizmo_transform().origin;
  2923. count++;
  2924. }
  2925. if (count != 0) {
  2926. center /= float(count);
  2927. }
  2928. cursor.pos = center;
  2929. }
  2930. void SpatialEditorViewport::assign_pending_data_pointers(Spatial *p_preview_node, AABB *p_preview_bounds, AcceptDialog *p_accept) {
  2931. preview_node = p_preview_node;
  2932. preview_bounds = p_preview_bounds;
  2933. accept = p_accept;
  2934. }
  2935. Vector3 SpatialEditorViewport::_get_instance_position(const Point2 &p_pos) const {
  2936. const float MAX_DISTANCE = 10;
  2937. Vector3 world_ray = _get_ray(p_pos);
  2938. Vector3 world_pos = _get_ray_pos(p_pos);
  2939. Vector3 point = world_pos + world_ray * MAX_DISTANCE;
  2940. PhysicsDirectSpaceState *ss = get_tree()->get_root()->get_world()->get_direct_space_state();
  2941. PhysicsDirectSpaceState::RayResult result;
  2942. if (ss->intersect_ray(world_pos, world_pos + world_ray * MAX_DISTANCE, result)) {
  2943. point = result.position;
  2944. }
  2945. return point;
  2946. }
  2947. AABB SpatialEditorViewport::_calculate_spatial_bounds(const Spatial *p_parent, bool p_exclude_toplevel_transform) {
  2948. AABB bounds;
  2949. const VisualInstance *visual_instance = Object::cast_to<VisualInstance>(p_parent);
  2950. if (visual_instance) {
  2951. bounds = visual_instance->get_aabb();
  2952. }
  2953. for (int i = 0; i < p_parent->get_child_count(); i++) {
  2954. Spatial *child = Object::cast_to<Spatial>(p_parent->get_child(i));
  2955. if (child) {
  2956. AABB child_bounds = _calculate_spatial_bounds(child, false);
  2957. if (bounds.size == Vector3() && p_parent->get_class_name() == StringName("Spatial")) {
  2958. bounds = child_bounds;
  2959. } else {
  2960. bounds.merge_with(child_bounds);
  2961. }
  2962. }
  2963. }
  2964. if (bounds.size == Vector3() && p_parent->get_class_name() != StringName("Spatial")) {
  2965. bounds = AABB(Vector3(-0.2, -0.2, -0.2), Vector3(0.4, 0.4, 0.4));
  2966. }
  2967. if (!p_exclude_toplevel_transform) {
  2968. bounds = p_parent->get_transform().xform(bounds);
  2969. }
  2970. return bounds;
  2971. }
  2972. void SpatialEditorViewport::_create_preview(const Vector<String> &files) const {
  2973. for (int i = 0; i < files.size(); i++) {
  2974. String path = files[i];
  2975. RES res = ResourceLoader::load(path);
  2976. ERR_CONTINUE(res.is_null());
  2977. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  2978. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  2979. if (mesh != nullptr || scene != nullptr) {
  2980. if (mesh != nullptr) {
  2981. MeshInstance *mesh_instance = memnew(MeshInstance);
  2982. mesh_instance->set_mesh(mesh);
  2983. preview_node->add_child(mesh_instance);
  2984. } else {
  2985. if (scene.is_valid()) {
  2986. Node *instance = scene->instance();
  2987. if (instance) {
  2988. preview_node->add_child(instance);
  2989. }
  2990. }
  2991. }
  2992. editor->get_scene_root()->add_child(preview_node);
  2993. }
  2994. }
  2995. *preview_bounds = _calculate_spatial_bounds(preview_node);
  2996. }
  2997. void SpatialEditorViewport::_remove_preview() {
  2998. if (preview_node->get_parent()) {
  2999. for (int i = preview_node->get_child_count() - 1; i >= 0; i--) {
  3000. Node *node = preview_node->get_child(i);
  3001. node->queue_delete();
  3002. preview_node->remove_child(node);
  3003. }
  3004. editor->get_scene_root()->remove_child(preview_node);
  3005. }
  3006. }
  3007. bool SpatialEditorViewport::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
  3008. if (p_desired_node->get_filename() == p_target_scene_path) {
  3009. return true;
  3010. }
  3011. int childCount = p_desired_node->get_child_count();
  3012. for (int i = 0; i < childCount; i++) {
  3013. Node *child = p_desired_node->get_child(i);
  3014. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  3015. return true;
  3016. }
  3017. }
  3018. return false;
  3019. }
  3020. bool SpatialEditorViewport::_create_instance(Node *parent, String &path, const Point2 &p_point) {
  3021. RES res = ResourceLoader::load(path);
  3022. ERR_FAIL_COND_V(res.is_null(), false);
  3023. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  3024. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  3025. Node *instanced_scene = nullptr;
  3026. if (mesh != nullptr || scene != nullptr) {
  3027. if (mesh != nullptr) {
  3028. MeshInstance *mesh_instance = memnew(MeshInstance);
  3029. mesh_instance->set_mesh(mesh);
  3030. mesh_instance->set_name(path.get_file().get_basename());
  3031. instanced_scene = mesh_instance;
  3032. } else {
  3033. if (!scene.is_valid()) { // invalid scene
  3034. return false;
  3035. } else {
  3036. instanced_scene = scene->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  3037. }
  3038. }
  3039. }
  3040. if (instanced_scene == nullptr) {
  3041. return false;
  3042. }
  3043. if (editor->get_edited_scene()->get_filename() != "") { // cyclical instancing
  3044. if (_cyclical_dependency_exists(editor->get_edited_scene()->get_filename(), instanced_scene)) {
  3045. memdelete(instanced_scene);
  3046. return false;
  3047. }
  3048. }
  3049. if (scene != nullptr) {
  3050. instanced_scene->set_filename(ProjectSettings::get_singleton()->localize_path(path));
  3051. }
  3052. editor_data->get_undo_redo().add_do_method(parent, "add_child", instanced_scene);
  3053. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_owner", editor->get_edited_scene());
  3054. editor_data->get_undo_redo().add_do_reference(instanced_scene);
  3055. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instanced_scene);
  3056. String new_name = parent->validate_child_name(instanced_scene);
  3057. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  3058. editor_data->get_undo_redo().add_do_method(sed, "live_debug_instance_node", editor->get_edited_scene()->get_path_to(parent), path, new_name);
  3059. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(editor->get_edited_scene()->get_path_to(parent)) + "/" + new_name));
  3060. Spatial *spatial = Object::cast_to<Spatial>(instanced_scene);
  3061. if (spatial) {
  3062. Transform global_transform;
  3063. Spatial *parent_spatial = Object::cast_to<Spatial>(parent);
  3064. if (parent_spatial) {
  3065. global_transform = parent_spatial->get_global_gizmo_transform();
  3066. }
  3067. global_transform.origin = spatial_editor->snap_point(_get_instance_position(p_point));
  3068. global_transform.basis *= spatial->get_transform().basis;
  3069. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_global_transform", global_transform);
  3070. }
  3071. return true;
  3072. }
  3073. void SpatialEditorViewport::_perform_drop_data() {
  3074. _remove_preview();
  3075. Vector<String> error_files;
  3076. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  3077. for (int i = 0; i < selected_files.size(); i++) {
  3078. String path = selected_files[i];
  3079. RES res = ResourceLoader::load(path);
  3080. if (res.is_null()) {
  3081. continue;
  3082. }
  3083. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  3084. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  3085. if (mesh != nullptr || scene != nullptr) {
  3086. bool success = _create_instance(target_node, path, drop_pos);
  3087. if (!success) {
  3088. error_files.push_back(path);
  3089. }
  3090. }
  3091. }
  3092. editor_data->get_undo_redo().commit_action();
  3093. if (error_files.size() > 0) {
  3094. String files_str;
  3095. for (int i = 0; i < error_files.size(); i++) {
  3096. files_str += error_files[i].get_file().get_basename() + ",";
  3097. }
  3098. files_str = files_str.substr(0, files_str.length() - 1);
  3099. accept->set_text(vformat(TTR("Error instancing scene from %s"), files_str.c_str()));
  3100. accept->popup_centered_minsize();
  3101. }
  3102. }
  3103. bool SpatialEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  3104. bool can_instance = false;
  3105. if (!preview_node->is_inside_tree()) {
  3106. Dictionary d = p_data;
  3107. if (d.has("type") && (String(d["type"]) == "files")) {
  3108. Vector<String> files = d["files"];
  3109. List<String> scene_extensions;
  3110. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  3111. List<String> mesh_extensions;
  3112. ResourceLoader::get_recognized_extensions_for_type("Mesh", &mesh_extensions);
  3113. for (int i = 0; i < files.size(); i++) {
  3114. if (mesh_extensions.find(files[i].get_extension()) || scene_extensions.find(files[i].get_extension())) {
  3115. RES res = ResourceLoader::load(files[i]);
  3116. if (res.is_null()) {
  3117. continue;
  3118. }
  3119. String type = res->get_class();
  3120. if (type == "PackedScene") {
  3121. Ref<PackedScene> sdata = ResourceLoader::load(files[i]);
  3122. Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  3123. if (!instanced_scene) {
  3124. continue;
  3125. }
  3126. memdelete(instanced_scene);
  3127. } else if (type == "Mesh" || type == "ArrayMesh" || type == "PrimitiveMesh") {
  3128. Ref<Mesh> mesh = ResourceLoader::load(files[i]);
  3129. if (!mesh.is_valid()) {
  3130. continue;
  3131. }
  3132. } else {
  3133. continue;
  3134. }
  3135. can_instance = true;
  3136. break;
  3137. }
  3138. }
  3139. if (can_instance) {
  3140. _create_preview(files);
  3141. }
  3142. }
  3143. } else {
  3144. can_instance = true;
  3145. }
  3146. if (can_instance) {
  3147. Transform global_transform = Transform(Basis(), _get_instance_position(p_point));
  3148. preview_node->set_global_transform(global_transform);
  3149. }
  3150. return can_instance;
  3151. }
  3152. void SpatialEditorViewport::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  3153. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  3154. return;
  3155. }
  3156. bool is_shift = Input::get_singleton()->is_key_pressed(KEY_SHIFT);
  3157. selected_files.clear();
  3158. Dictionary d = p_data;
  3159. if (d.has("type") && String(d["type"]) == "files") {
  3160. selected_files = d["files"];
  3161. }
  3162. List<Node *> list = editor->get_editor_selection()->get_selected_node_list();
  3163. if (list.size() == 0) {
  3164. Node *root_node = editor->get_edited_scene();
  3165. if (root_node) {
  3166. list.push_back(root_node);
  3167. } else {
  3168. accept->set_text(TTR("No parent to instance a child at."));
  3169. accept->popup_centered_minsize();
  3170. _remove_preview();
  3171. return;
  3172. }
  3173. }
  3174. if (list.size() != 1) {
  3175. accept->set_text(TTR("This operation requires a single selected node."));
  3176. accept->popup_centered_minsize();
  3177. _remove_preview();
  3178. return;
  3179. }
  3180. target_node = list[0];
  3181. if (is_shift && target_node != editor->get_edited_scene()) {
  3182. target_node = target_node->get_parent();
  3183. }
  3184. drop_pos = p_point;
  3185. _perform_drop_data();
  3186. }
  3187. SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, EditorNode *p_editor, int p_index) {
  3188. _edit.mode = TRANSFORM_NONE;
  3189. _edit.plane = TRANSFORM_VIEW;
  3190. _edit.edited_gizmo = 0;
  3191. _edit.snap = true;
  3192. _edit.gizmo_handle = 0;
  3193. index = p_index;
  3194. editor = p_editor;
  3195. editor_data = editor->get_scene_tree_dock()->get_editor_data();
  3196. editor_selection = editor->get_editor_selection();
  3197. undo_redo = editor->get_undo_redo();
  3198. clicked = 0;
  3199. clicked_includes_current = false;
  3200. orthogonal = false;
  3201. auto_orthogonal = false;
  3202. lock_rotation = false;
  3203. message_time = 0;
  3204. zoom_indicator_delay = 0.0;
  3205. spatial_editor = p_spatial_editor;
  3206. ViewportContainer *c = memnew(ViewportContainer);
  3207. viewport_container = c;
  3208. c->set_stretch(true);
  3209. add_child(c);
  3210. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  3211. viewport = memnew(Viewport);
  3212. viewport->set_disable_input(true);
  3213. c->add_child(viewport);
  3214. surface = memnew(Control);
  3215. surface->set_drag_forwarding(this);
  3216. add_child(surface);
  3217. surface->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  3218. surface->set_clip_contents(true);
  3219. camera = memnew(Camera);
  3220. camera->set_disable_gizmo(true);
  3221. 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));
  3222. viewport->add_child(camera);
  3223. camera->make_current();
  3224. surface->set_focus_mode(FOCUS_ALL);
  3225. VBoxContainer *vbox = memnew(VBoxContainer);
  3226. surface->add_child(vbox);
  3227. vbox->set_position(Point2(10, 10) * EDSCALE);
  3228. view_menu = memnew(MenuButton);
  3229. view_menu->set_flat(false);
  3230. vbox->add_child(view_menu);
  3231. view_menu->set_h_size_flags(0);
  3232. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/top_view"), VIEW_TOP);
  3233. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/bottom_view"), VIEW_BOTTOM);
  3234. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/left_view"), VIEW_LEFT);
  3235. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/right_view"), VIEW_RIGHT);
  3236. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/front_view"), VIEW_FRONT);
  3237. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/rear_view"), VIEW_REAR);
  3238. view_menu->get_popup()->add_separator();
  3239. view_menu->get_popup()->add_radio_check_item(TTR("Perspective") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_PERSPECTIVE);
  3240. view_menu->get_popup()->add_radio_check_item(TTR("Orthogonal") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_ORTHOGONAL);
  3241. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  3242. view_menu->get_popup()->add_check_item(TTR("Auto Orthogonal Enabled"), VIEW_AUTO_ORTHOGONAL);
  3243. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL), true);
  3244. view_menu->get_popup()->add_separator();
  3245. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_lock_rotation", TTR("Lock View Rotation")), VIEW_LOCK_ROTATION);
  3246. view_menu->get_popup()->add_separator();
  3247. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_normal", TTR("Display Normal")), VIEW_DISPLAY_NORMAL);
  3248. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_wireframe", TTR("Display Wireframe")), VIEW_DISPLAY_WIREFRAME);
  3249. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_overdraw", TTR("Display Overdraw")), VIEW_DISPLAY_OVERDRAW);
  3250. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_unshaded", TTR("Display Unshaded")), VIEW_DISPLAY_SHADELESS);
  3251. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true);
  3252. view_menu->get_popup()->add_separator();
  3253. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment", TTR("View Environment")), VIEW_ENVIRONMENT);
  3254. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT), true);
  3255. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos", TTR("View Gizmos")), VIEW_GIZMOS);
  3256. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS), true);
  3257. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_information", TTR("View Information")), VIEW_INFORMATION);
  3258. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_fps", TTR("View FPS")), VIEW_FPS);
  3259. view_menu->get_popup()->add_separator();
  3260. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_half_resolution", TTR("Half Resolution")), VIEW_HALF_RESOLUTION);
  3261. view_menu->get_popup()->add_separator();
  3262. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_listener", TTR("Audio Listener")), VIEW_AUDIO_LISTENER);
  3263. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_doppler", TTR("Enable Doppler")), VIEW_AUDIO_DOPPLER);
  3264. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER), true);
  3265. view_menu->get_popup()->add_separator();
  3266. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_cinematic_preview", TTR("Cinematic Preview")), VIEW_CINEMATIC_PREVIEW);
  3267. view_menu->get_popup()->add_separator();
  3268. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_origin"), VIEW_CENTER_TO_ORIGIN);
  3269. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_selection"), VIEW_CENTER_TO_SELECTION);
  3270. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_transform_with_view"), VIEW_ALIGN_TRANSFORM_WITH_VIEW);
  3271. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_rotation_with_view"), VIEW_ALIGN_ROTATION_WITH_VIEW);
  3272. view_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  3273. view_menu->set_disable_shortcuts(true);
  3274. if (OS::get_singleton()->get_current_video_driver() == OS::VIDEO_DRIVER_GLES2) {
  3275. // Alternate display modes only work when using the GLES3 renderer; make this explicit.
  3276. const int normal_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL);
  3277. const int wireframe_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME);
  3278. const int overdraw_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW);
  3279. const int shadeless_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS);
  3280. const String unsupported_tooltip = TTR("Not available when using the GLES2 renderer.");
  3281. view_menu->get_popup()->set_item_disabled(normal_idx, true);
  3282. view_menu->get_popup()->set_item_tooltip(normal_idx, unsupported_tooltip);
  3283. view_menu->get_popup()->set_item_disabled(wireframe_idx, true);
  3284. view_menu->get_popup()->set_item_tooltip(wireframe_idx, unsupported_tooltip);
  3285. view_menu->get_popup()->set_item_disabled(overdraw_idx, true);
  3286. view_menu->get_popup()->set_item_tooltip(overdraw_idx, unsupported_tooltip);
  3287. view_menu->get_popup()->set_item_disabled(shadeless_idx, true);
  3288. view_menu->get_popup()->set_item_tooltip(shadeless_idx, unsupported_tooltip);
  3289. }
  3290. ED_SHORTCUT("spatial_editor/freelook_left", TTR("Freelook Left"), KEY_A);
  3291. ED_SHORTCUT("spatial_editor/freelook_right", TTR("Freelook Right"), KEY_D);
  3292. ED_SHORTCUT("spatial_editor/freelook_forward", TTR("Freelook Forward"), KEY_W);
  3293. ED_SHORTCUT("spatial_editor/freelook_backwards", TTR("Freelook Backwards"), KEY_S);
  3294. ED_SHORTCUT("spatial_editor/freelook_up", TTR("Freelook Up"), KEY_E);
  3295. ED_SHORTCUT("spatial_editor/freelook_down", TTR("Freelook Down"), KEY_Q);
  3296. ED_SHORTCUT("spatial_editor/freelook_speed_modifier", TTR("Freelook Speed Modifier"), KEY_SHIFT);
  3297. ED_SHORTCUT("spatial_editor/freelook_slow_modifier", TTR("Freelook Slow Modifier"), KEY_ALT);
  3298. preview_camera = memnew(CheckBox);
  3299. preview_camera->set_text(TTR("Preview"));
  3300. preview_camera->set_shortcut(ED_SHORTCUT("spatial_editor/toggle_camera_preview", TTR("Toggle Camera Preview"), KEY_MASK_CMD | KEY_P));
  3301. vbox->add_child(preview_camera);
  3302. preview_camera->set_h_size_flags(0);
  3303. preview_camera->hide();
  3304. preview_camera->connect("toggled", this, "_toggle_camera_preview");
  3305. previewing = nullptr;
  3306. gizmo_scale = 1.0;
  3307. preview_node = nullptr;
  3308. info_label = memnew(Label);
  3309. info_label->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -90 * EDSCALE);
  3310. info_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -90 * EDSCALE);
  3311. info_label->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -10 * EDSCALE);
  3312. info_label->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  3313. info_label->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  3314. info_label->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  3315. surface->add_child(info_label);
  3316. info_label->hide();
  3317. cinema_label = memnew(Label);
  3318. cinema_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  3319. cinema_label->set_h_grow_direction(GROW_DIRECTION_END);
  3320. cinema_label->set_align(Label::ALIGN_CENTER);
  3321. surface->add_child(cinema_label);
  3322. cinema_label->set_text(TTR("Cinematic Preview"));
  3323. cinema_label->hide();
  3324. previewing_cinema = false;
  3325. locked_label = memnew(Label);
  3326. locked_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -20 * EDSCALE);
  3327. locked_label->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  3328. locked_label->set_h_grow_direction(GROW_DIRECTION_END);
  3329. locked_label->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  3330. locked_label->set_align(Label::ALIGN_CENTER);
  3331. surface->add_child(locked_label);
  3332. locked_label->set_text(TTR("View Rotation Locked"));
  3333. locked_label->hide();
  3334. zoom_limit_label = memnew(Label);
  3335. zoom_limit_label->set_anchors_and_margins_preset(LayoutPreset::PRESET_BOTTOM_LEFT);
  3336. zoom_limit_label->set_margin(Margin::MARGIN_TOP, -28 * EDSCALE);
  3337. zoom_limit_label->set_text(TTR("To zoom further, change the camera's clipping planes (View -> Settings...)"));
  3338. zoom_limit_label->set_name("ZoomLimitMessageLabel");
  3339. zoom_limit_label->add_color_override("font_color", Color(1, 1, 1, 1));
  3340. zoom_limit_label->hide();
  3341. surface->add_child(zoom_limit_label);
  3342. top_right_vbox = memnew(VBoxContainer);
  3343. top_right_vbox->set_anchors_and_margins_preset(PRESET_TOP_RIGHT, PRESET_MODE_MINSIZE, 2.0 * EDSCALE);
  3344. top_right_vbox->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  3345. rotation_control = memnew(ViewportRotationControl);
  3346. rotation_control->set_custom_minimum_size(Size2(80, 80) * EDSCALE);
  3347. rotation_control->set_h_size_flags(SIZE_SHRINK_END);
  3348. rotation_control->set_viewport(this);
  3349. top_right_vbox->add_child(rotation_control);
  3350. fps_label = memnew(Label);
  3351. fps_label->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -90 * EDSCALE);
  3352. fps_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  3353. fps_label->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -10 * EDSCALE);
  3354. fps_label->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  3355. fps_label->set_tooltip(TTR("Note: The FPS value displayed is the editor's framerate.\nIt cannot be used as a reliable indication of in-game performance."));
  3356. fps_label->set_mouse_filter(MOUSE_FILTER_PASS); // Otherwise tooltip doesn't show.
  3357. top_right_vbox->add_child(fps_label);
  3358. fps_label->hide();
  3359. surface->add_child(top_right_vbox);
  3360. accept = nullptr;
  3361. freelook_active = false;
  3362. freelook_speed = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_base_speed");
  3363. selection_menu = memnew(PopupMenu);
  3364. add_child(selection_menu);
  3365. selection_menu->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  3366. selection_menu->connect("id_pressed", this, "_selection_result_pressed");
  3367. selection_menu->connect("popup_hide", this, "_selection_menu_hide");
  3368. if (p_index == 0) {
  3369. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER), true);
  3370. viewport->set_as_audio_listener(true);
  3371. }
  3372. view_type = VIEW_TYPE_USER;
  3373. _update_name();
  3374. EditorSettings::get_singleton()->connect("settings_changed", this, "update_transform_gizmo_view");
  3375. }
  3376. //////////////////////////////////////////////////////////////
  3377. void SpatialEditorViewportContainer::_gui_input(const Ref<InputEvent> &p_event) {
  3378. Ref<InputEventMouseButton> mb = p_event;
  3379. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  3380. if (mb->is_pressed()) {
  3381. Vector2 size = get_size();
  3382. int h_sep = get_constant("separation", "HSplitContainer");
  3383. int v_sep = get_constant("separation", "VSplitContainer");
  3384. int mid_w = size.width * ratio_h;
  3385. int mid_h = size.height * ratio_v;
  3386. dragging_h = mb->get_position().x > (mid_w - h_sep / 2) && mb->get_position().x < (mid_w + h_sep / 2);
  3387. dragging_v = mb->get_position().y > (mid_h - v_sep / 2) && mb->get_position().y < (mid_h + v_sep / 2);
  3388. drag_begin_pos = mb->get_position();
  3389. drag_begin_ratio.x = ratio_h;
  3390. drag_begin_ratio.y = ratio_v;
  3391. switch (view) {
  3392. case VIEW_USE_1_VIEWPORT: {
  3393. dragging_h = false;
  3394. dragging_v = false;
  3395. } break;
  3396. case VIEW_USE_2_VIEWPORTS: {
  3397. dragging_h = false;
  3398. } break;
  3399. case VIEW_USE_2_VIEWPORTS_ALT: {
  3400. dragging_v = false;
  3401. } break;
  3402. case VIEW_USE_3_VIEWPORTS:
  3403. case VIEW_USE_3_VIEWPORTS_ALT:
  3404. case VIEW_USE_4_VIEWPORTS: {
  3405. // Do nothing.
  3406. } break;
  3407. }
  3408. } else {
  3409. dragging_h = false;
  3410. dragging_v = false;
  3411. }
  3412. }
  3413. Ref<InputEventMouseMotion> mm = p_event;
  3414. if (mm.is_valid()) {
  3415. if (view == VIEW_USE_3_VIEWPORTS || view == VIEW_USE_3_VIEWPORTS_ALT || view == VIEW_USE_4_VIEWPORTS) {
  3416. Vector2 size = get_size();
  3417. int h_sep = get_constant("separation", "HSplitContainer");
  3418. int v_sep = get_constant("separation", "VSplitContainer");
  3419. int mid_w = size.width * ratio_h;
  3420. int mid_h = size.height * ratio_v;
  3421. bool was_hovering_h = hovering_h;
  3422. bool was_hovering_v = hovering_v;
  3423. hovering_h = mm->get_position().x > (mid_w - h_sep / 2) && mm->get_position().x < (mid_w + h_sep / 2);
  3424. hovering_v = mm->get_position().y > (mid_h - v_sep / 2) && mm->get_position().y < (mid_h + v_sep / 2);
  3425. if (was_hovering_h != hovering_h || was_hovering_v != hovering_v) {
  3426. update();
  3427. }
  3428. }
  3429. if (dragging_h) {
  3430. float new_ratio = drag_begin_ratio.x + (mm->get_position().x - drag_begin_pos.x) / get_size().width;
  3431. new_ratio = CLAMP(new_ratio, 40 / get_size().width, (get_size().width - 40) / get_size().width);
  3432. ratio_h = new_ratio;
  3433. queue_sort();
  3434. update();
  3435. }
  3436. if (dragging_v) {
  3437. float new_ratio = drag_begin_ratio.y + (mm->get_position().y - drag_begin_pos.y) / get_size().height;
  3438. new_ratio = CLAMP(new_ratio, 40 / get_size().height, (get_size().height - 40) / get_size().height);
  3439. ratio_v = new_ratio;
  3440. queue_sort();
  3441. update();
  3442. }
  3443. }
  3444. }
  3445. void SpatialEditorViewportContainer::_notification(int p_what) {
  3446. if (p_what == NOTIFICATION_MOUSE_ENTER || p_what == NOTIFICATION_MOUSE_EXIT) {
  3447. mouseover = (p_what == NOTIFICATION_MOUSE_ENTER);
  3448. update();
  3449. }
  3450. if (p_what == NOTIFICATION_DRAW && mouseover) {
  3451. Ref<Texture> h_grabber = get_icon("grabber", "HSplitContainer");
  3452. Ref<Texture> v_grabber = get_icon("grabber", "VSplitContainer");
  3453. Ref<Texture> hdiag_grabber = get_icon("GuiViewportHdiagsplitter", "EditorIcons");
  3454. Ref<Texture> vdiag_grabber = get_icon("GuiViewportVdiagsplitter", "EditorIcons");
  3455. Ref<Texture> vh_grabber = get_icon("GuiViewportVhsplitter", "EditorIcons");
  3456. Vector2 size = get_size();
  3457. int h_sep = get_constant("separation", "HSplitContainer");
  3458. int v_sep = get_constant("separation", "VSplitContainer");
  3459. int mid_w = size.width * ratio_h;
  3460. int mid_h = size.height * ratio_v;
  3461. int size_left = mid_w - h_sep / 2;
  3462. int size_bottom = size.height - mid_h - v_sep / 2;
  3463. switch (view) {
  3464. case VIEW_USE_1_VIEWPORT: {
  3465. // Nothing to show.
  3466. } break;
  3467. case VIEW_USE_2_VIEWPORTS: {
  3468. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  3469. set_default_cursor_shape(CURSOR_VSPLIT);
  3470. } break;
  3471. case VIEW_USE_2_VIEWPORTS_ALT: {
  3472. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  3473. set_default_cursor_shape(CURSOR_HSPLIT);
  3474. } break;
  3475. case VIEW_USE_3_VIEWPORTS: {
  3476. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  3477. draw_texture(hdiag_grabber, Vector2(mid_w - hdiag_grabber->get_width() / 2, mid_h - v_grabber->get_height() / 4));
  3478. set_default_cursor_shape(CURSOR_DRAG);
  3479. } else if ((hovering_v && !dragging_h) || dragging_v) {
  3480. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  3481. set_default_cursor_shape(CURSOR_VSPLIT);
  3482. } else if (hovering_h || dragging_h) {
  3483. 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));
  3484. set_default_cursor_shape(CURSOR_HSPLIT);
  3485. }
  3486. } break;
  3487. case VIEW_USE_3_VIEWPORTS_ALT: {
  3488. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  3489. draw_texture(vdiag_grabber, Vector2(mid_w - vdiag_grabber->get_width() + v_grabber->get_height() / 4, mid_h - vdiag_grabber->get_height() / 2));
  3490. set_default_cursor_shape(CURSOR_DRAG);
  3491. } else if ((hovering_v && !dragging_h) || dragging_v) {
  3492. draw_texture(v_grabber, Vector2((size_left - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  3493. set_default_cursor_shape(CURSOR_VSPLIT);
  3494. } else if (hovering_h || dragging_h) {
  3495. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  3496. set_default_cursor_shape(CURSOR_HSPLIT);
  3497. }
  3498. } break;
  3499. case VIEW_USE_4_VIEWPORTS: {
  3500. Vector2 half(mid_w, mid_h);
  3501. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  3502. draw_texture(vh_grabber, half - vh_grabber->get_size() / 2.0);
  3503. set_default_cursor_shape(CURSOR_DRAG);
  3504. } else if ((hovering_v && !dragging_h) || dragging_v) {
  3505. draw_texture(v_grabber, half - v_grabber->get_size() / 2.0);
  3506. set_default_cursor_shape(CURSOR_VSPLIT);
  3507. } else if (hovering_h || dragging_h) {
  3508. draw_texture(h_grabber, half - h_grabber->get_size() / 2.0);
  3509. set_default_cursor_shape(CURSOR_HSPLIT);
  3510. }
  3511. } break;
  3512. }
  3513. }
  3514. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  3515. SpatialEditorViewport *viewports[4];
  3516. int vc = 0;
  3517. for (int i = 0; i < get_child_count(); i++) {
  3518. viewports[vc] = Object::cast_to<SpatialEditorViewport>(get_child(i));
  3519. if (viewports[vc]) {
  3520. vc++;
  3521. }
  3522. }
  3523. ERR_FAIL_COND(vc != 4);
  3524. Size2 size = get_size();
  3525. if (size.x < 10 || size.y < 10) {
  3526. for (int i = 0; i < 4; i++) {
  3527. viewports[i]->hide();
  3528. }
  3529. return;
  3530. }
  3531. int h_sep = get_constant("separation", "HSplitContainer");
  3532. int v_sep = get_constant("separation", "VSplitContainer");
  3533. int mid_w = size.width * ratio_h;
  3534. int mid_h = size.height * ratio_v;
  3535. int size_left = mid_w - h_sep / 2;
  3536. int size_right = size.width - mid_w - h_sep / 2;
  3537. int size_top = mid_h - v_sep / 2;
  3538. int size_bottom = size.height - mid_h - v_sep / 2;
  3539. switch (view) {
  3540. case VIEW_USE_1_VIEWPORT: {
  3541. viewports[0]->show();
  3542. for (int i = 1; i < 4; i++) {
  3543. viewports[i]->hide();
  3544. }
  3545. fit_child_in_rect(viewports[0], Rect2(Vector2(), size));
  3546. } break;
  3547. case VIEW_USE_2_VIEWPORTS: {
  3548. for (int i = 0; i < 4; i++) {
  3549. if (i == 1 || i == 3) {
  3550. viewports[i]->hide();
  3551. } else {
  3552. viewports[i]->show();
  3553. }
  3554. }
  3555. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  3556. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size.width, size_bottom)));
  3557. } break;
  3558. case VIEW_USE_2_VIEWPORTS_ALT: {
  3559. for (int i = 0; i < 4; i++) {
  3560. if (i == 1 || i == 3) {
  3561. viewports[i]->hide();
  3562. } else {
  3563. viewports[i]->show();
  3564. }
  3565. }
  3566. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size.height)));
  3567. fit_child_in_rect(viewports[2], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  3568. } break;
  3569. case VIEW_USE_3_VIEWPORTS: {
  3570. for (int i = 0; i < 4; i++) {
  3571. if (i == 1) {
  3572. viewports[i]->hide();
  3573. } else {
  3574. viewports[i]->show();
  3575. }
  3576. }
  3577. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  3578. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  3579. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  3580. } break;
  3581. case VIEW_USE_3_VIEWPORTS_ALT: {
  3582. for (int i = 0; i < 4; i++) {
  3583. if (i == 1) {
  3584. viewports[i]->hide();
  3585. } else {
  3586. viewports[i]->show();
  3587. }
  3588. }
  3589. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  3590. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  3591. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  3592. } break;
  3593. case VIEW_USE_4_VIEWPORTS: {
  3594. for (int i = 0; i < 4; i++) {
  3595. viewports[i]->show();
  3596. }
  3597. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  3598. fit_child_in_rect(viewports[1], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size_top)));
  3599. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  3600. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  3601. } break;
  3602. }
  3603. }
  3604. }
  3605. void SpatialEditorViewportContainer::set_view(View p_view) {
  3606. view = p_view;
  3607. queue_sort();
  3608. }
  3609. SpatialEditorViewportContainer::View SpatialEditorViewportContainer::get_view() {
  3610. return view;
  3611. }
  3612. void SpatialEditorViewportContainer::_bind_methods() {
  3613. ClassDB::bind_method("_gui_input", &SpatialEditorViewportContainer::_gui_input);
  3614. }
  3615. SpatialEditorViewportContainer::SpatialEditorViewportContainer() {
  3616. set_clip_contents(true);
  3617. view = VIEW_USE_1_VIEWPORT;
  3618. mouseover = false;
  3619. ratio_h = 0.5;
  3620. ratio_v = 0.5;
  3621. hovering_v = false;
  3622. hovering_h = false;
  3623. dragging_v = false;
  3624. dragging_h = false;
  3625. }
  3626. ///////////////////////////////////////////////////////////////////
  3627. SpatialEditor *SpatialEditor::singleton = nullptr;
  3628. SpatialEditorSelectedItem::~SpatialEditorSelectedItem() {
  3629. if (sbox_instance.is_valid()) {
  3630. VisualServer::get_singleton()->free(sbox_instance);
  3631. }
  3632. if (sbox_instance_xray.is_valid()) {
  3633. VisualServer::get_singleton()->free(sbox_instance_xray);
  3634. }
  3635. }
  3636. void SpatialEditor::select_gizmo_highlight_axis(int p_axis) {
  3637. for (int i = 0; i < 3; i++) {
  3638. move_gizmo[i]->surface_set_material(0, i == p_axis ? gizmo_color_hl[i] : gizmo_color[i]);
  3639. move_plane_gizmo[i]->surface_set_material(0, (i + 6) == p_axis ? plane_gizmo_color_hl[i] : plane_gizmo_color[i]);
  3640. rotate_gizmo[i]->surface_set_material(0, (i + 3) == p_axis ? rotate_gizmo_color_hl[i] : rotate_gizmo_color[i]);
  3641. scale_gizmo[i]->surface_set_material(0, (i + 9) == p_axis ? gizmo_color_hl[i] : gizmo_color[i]);
  3642. scale_plane_gizmo[i]->surface_set_material(0, (i + 12) == p_axis ? plane_gizmo_color_hl[i] : plane_gizmo_color[i]);
  3643. }
  3644. }
  3645. void SpatialEditor::show_advanced_portal_tools(bool p_show) {
  3646. // toolbar button
  3647. Button *const button = tool_button[TOOL_CONVERT_ROOMS];
  3648. if (p_show) {
  3649. button->set_text(TTR("Convert Rooms"));
  3650. } else {
  3651. button->set_text("");
  3652. }
  3653. }
  3654. void SpatialEditor::update_portal_tools() {
  3655. // just some protection against calling null pointers, hopefully not needed
  3656. if (view_menu && view_menu->get_popup()) {
  3657. // the view portal culling toggle
  3658. int view_portal_item_index = view_menu->get_popup()->get_item_index(MENU_VIEW_PORTAL_CULLING);
  3659. if (RoomManager::active_room_manager) {
  3660. view_menu->get_popup()->set_item_disabled(view_portal_item_index, false);
  3661. bool active = RoomManager::static_rooms_get_active();
  3662. view_menu->get_popup()->set_item_checked(view_portal_item_index, active);
  3663. } else {
  3664. view_menu->get_popup()->set_item_disabled(view_portal_item_index, true);
  3665. }
  3666. // toolbar button
  3667. Button *const button = tool_button[TOOL_CONVERT_ROOMS];
  3668. if (RoomManager::active_room_manager) {
  3669. button->show();
  3670. } else {
  3671. button->hide();
  3672. }
  3673. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  3674. viewports[i]->_update_name();
  3675. }
  3676. }
  3677. }
  3678. void SpatialEditor::update_transform_gizmo() {
  3679. List<Node *> &selection = editor_selection->get_selected_node_list();
  3680. AABB center;
  3681. bool first = true;
  3682. Basis gizmo_basis;
  3683. bool local_gizmo_coords = are_local_coords_enabled();
  3684. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3685. Spatial *sp = Object::cast_to<Spatial>(E->get());
  3686. if (!sp) {
  3687. continue;
  3688. }
  3689. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  3690. if (!se) {
  3691. continue;
  3692. }
  3693. Transform xf = se->sp->get_global_gizmo_transform();
  3694. if (first) {
  3695. center.position = xf.origin;
  3696. first = false;
  3697. if (local_gizmo_coords) {
  3698. gizmo_basis = xf.basis;
  3699. gizmo_basis.orthonormalize();
  3700. }
  3701. } else {
  3702. center.expand_to(xf.origin);
  3703. gizmo_basis = Basis();
  3704. }
  3705. }
  3706. Vector3 pcenter = center.position + center.size * 0.5;
  3707. gizmo.visible = !first;
  3708. gizmo.transform.origin = pcenter;
  3709. gizmo.transform.basis = gizmo_basis;
  3710. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  3711. viewports[i]->update_transform_gizmo_view();
  3712. }
  3713. }
  3714. void _update_all_gizmos(Node *p_node) {
  3715. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  3716. Spatial *spatial_node = Object::cast_to<Spatial>(p_node->get_child(i));
  3717. if (spatial_node) {
  3718. spatial_node->update_gizmo();
  3719. }
  3720. _update_all_gizmos(p_node->get_child(i));
  3721. }
  3722. }
  3723. void SpatialEditor::update_all_gizmos(Node *p_node) {
  3724. if (!p_node) {
  3725. if (SceneTree::get_singleton()) {
  3726. p_node = SceneTree::get_singleton()->get_root();
  3727. } else {
  3728. // No scene tree, so nothing to update.
  3729. return;
  3730. }
  3731. }
  3732. _update_all_gizmos(p_node);
  3733. }
  3734. Object *SpatialEditor::_get_editor_data(Object *p_what) {
  3735. Spatial *sp = Object::cast_to<Spatial>(p_what);
  3736. if (!sp) {
  3737. return nullptr;
  3738. }
  3739. SpatialEditorSelectedItem *si = memnew(SpatialEditorSelectedItem);
  3740. si->sp = sp;
  3741. si->sbox_instance = VisualServer::get_singleton()->instance_create2(
  3742. selection_box->get_rid(),
  3743. sp->get_world()->get_scenario());
  3744. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(
  3745. si->sbox_instance,
  3746. VS::SHADOW_CASTING_SETTING_OFF);
  3747. // Use the Edit layer to hide the selection box when View Gizmos is disabled, since it is a bit distracting.
  3748. // It's still possible to approximately guess what is selected by looking at the manipulation gizmo position.
  3749. VS::get_singleton()->instance_set_layer_mask(si->sbox_instance, 1 << SpatialEditorViewport::GIZMO_EDIT_LAYER);
  3750. si->sbox_instance_xray = VisualServer::get_singleton()->instance_create2(
  3751. selection_box_xray->get_rid(),
  3752. sp->get_world()->get_scenario());
  3753. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(
  3754. si->sbox_instance_xray,
  3755. VS::SHADOW_CASTING_SETTING_OFF);
  3756. // Use the Edit layer to hide the selection box when View Gizmos is disabled, since it is a bit distracting.
  3757. // It's still possible to approximately guess what is selected by looking at the manipulation gizmo position.
  3758. VS::get_singleton()->instance_set_layer_mask(si->sbox_instance_xray, 1 << SpatialEditorViewport::GIZMO_EDIT_LAYER);
  3759. return si;
  3760. }
  3761. void SpatialEditor::_generate_selection_boxes() {
  3762. // Use two AABBs to create the illusion of a slightly thicker line.
  3763. AABB aabb(Vector3(), Vector3(1, 1, 1));
  3764. AABB aabb_offset(Vector3(), Vector3(1, 1, 1));
  3765. // Grow the bounding boxes slightly to avoid Z-fighting with the mesh's edges.
  3766. aabb.grow_by(0.005);
  3767. aabb_offset.grow_by(0.01);
  3768. // Create a x-ray (visible through solid surfaces) and standard version of the selection box.
  3769. // Both will be drawn at the same position, but with different opacity.
  3770. // This lets the user see where the selection is while still having a sense of depth.
  3771. Ref<SurfaceTool> st = memnew(SurfaceTool);
  3772. Ref<SurfaceTool> st_xray = memnew(SurfaceTool);
  3773. st->begin(Mesh::PRIMITIVE_LINES);
  3774. st_xray->begin(Mesh::PRIMITIVE_LINES);
  3775. for (int i = 0; i < 12; i++) {
  3776. Vector3 a, b;
  3777. aabb.get_edge(i, a, b);
  3778. st->add_vertex(a);
  3779. st->add_vertex(b);
  3780. st_xray->add_vertex(a);
  3781. st_xray->add_vertex(b);
  3782. }
  3783. for (int i = 0; i < 12; i++) {
  3784. Vector3 a, b;
  3785. aabb_offset.get_edge(i, a, b);
  3786. st->add_vertex(a);
  3787. st->add_vertex(b);
  3788. st_xray->add_vertex(a);
  3789. st_xray->add_vertex(b);
  3790. }
  3791. Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
  3792. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3793. const Color selection_box_color = EDITOR_GET("editors/3d/selection_box_color");
  3794. mat->set_albedo(selection_box_color);
  3795. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3796. st->set_material(mat);
  3797. selection_box = st->commit();
  3798. Ref<SpatialMaterial> mat_xray = memnew(SpatialMaterial);
  3799. mat_xray->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3800. mat_xray->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, true);
  3801. mat_xray->set_albedo(selection_box_color * Color(1, 1, 1, 0.15));
  3802. mat_xray->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3803. st_xray->set_material(mat_xray);
  3804. selection_box_xray = st_xray->commit();
  3805. }
  3806. Dictionary SpatialEditor::get_state() const {
  3807. Dictionary d;
  3808. d["snap_enabled"] = snap_enabled;
  3809. d["translate_snap"] = get_translate_snap();
  3810. d["rotate_snap"] = get_rotate_snap();
  3811. d["scale_snap"] = get_scale_snap();
  3812. d["local_coords"] = tool_option_button[TOOL_OPT_LOCAL_COORDS]->is_pressed();
  3813. int vc = 0;
  3814. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT))) {
  3815. vc = 1;
  3816. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS))) {
  3817. vc = 2;
  3818. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS))) {
  3819. vc = 3;
  3820. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS))) {
  3821. vc = 4;
  3822. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT))) {
  3823. vc = 5;
  3824. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT))) {
  3825. vc = 6;
  3826. }
  3827. d["viewport_mode"] = vc;
  3828. Array vpdata;
  3829. for (int i = 0; i < 4; i++) {
  3830. vpdata.push_back(viewports[i]->get_state());
  3831. }
  3832. d["viewports"] = vpdata;
  3833. d["show_grid"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID));
  3834. d["show_origin"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN));
  3835. d["fov"] = get_fov();
  3836. d["znear"] = get_znear();
  3837. d["zfar"] = get_zfar();
  3838. Dictionary gizmos_status;
  3839. for (int i = 0; i < gizmo_plugins_by_name.size(); i++) {
  3840. if (!gizmo_plugins_by_name[i]->can_be_hidden()) {
  3841. continue;
  3842. }
  3843. int state = gizmos_menu->get_item_state(gizmos_menu->get_item_index(i));
  3844. String name = gizmo_plugins_by_name[i]->get_name();
  3845. gizmos_status[name] = state;
  3846. }
  3847. d["gizmos_status"] = gizmos_status;
  3848. return d;
  3849. }
  3850. void SpatialEditor::set_state(const Dictionary &p_state) {
  3851. Dictionary d = p_state;
  3852. if (d.has("snap_enabled")) {
  3853. snap_enabled = d["snap_enabled"];
  3854. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(d["snap_enabled"]);
  3855. }
  3856. if (d.has("translate_snap")) {
  3857. snap_translate_value = d["translate_snap"];
  3858. }
  3859. if (d.has("rotate_snap")) {
  3860. snap_rotate_value = d["rotate_snap"];
  3861. }
  3862. if (d.has("scale_snap")) {
  3863. snap_scale_value = d["scale_snap"];
  3864. }
  3865. _snap_update();
  3866. if (d.has("local_coords")) {
  3867. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(d["local_coords"]);
  3868. update_transform_gizmo();
  3869. }
  3870. if (d.has("viewport_mode")) {
  3871. int vc = d["viewport_mode"];
  3872. if (vc == 1) {
  3873. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  3874. } else if (vc == 2) {
  3875. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  3876. } else if (vc == 3) {
  3877. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  3878. } else if (vc == 4) {
  3879. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  3880. } else if (vc == 5) {
  3881. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  3882. } else if (vc == 6) {
  3883. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  3884. }
  3885. }
  3886. if (d.has("viewports")) {
  3887. Array vp = d["viewports"];
  3888. uint32_t vp_size = static_cast<uint32_t>(vp.size());
  3889. if (vp_size > VIEWPORTS_COUNT) {
  3890. WARN_PRINT("Ignoring superfluous viewport settings from spatial editor state.");
  3891. vp_size = VIEWPORTS_COUNT;
  3892. }
  3893. for (uint32_t i = 0; i < vp_size; i++) {
  3894. viewports[i]->set_state(vp[i]);
  3895. }
  3896. }
  3897. if (d.has("zfar")) {
  3898. settings_zfar->set_value(float(d["zfar"]));
  3899. }
  3900. if (d.has("znear")) {
  3901. settings_znear->set_value(float(d["znear"]));
  3902. }
  3903. if (d.has("fov")) {
  3904. settings_fov->set_value(float(d["fov"]));
  3905. }
  3906. if (d.has("show_grid")) {
  3907. bool use = d["show_grid"];
  3908. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID))) {
  3909. _menu_item_pressed(MENU_VIEW_GRID);
  3910. }
  3911. }
  3912. if (d.has("show_origin")) {
  3913. bool use = d["show_origin"];
  3914. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN))) {
  3915. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), use);
  3916. VisualServer::get_singleton()->instance_set_visible(origin_instance, use);
  3917. }
  3918. }
  3919. if (d.has("gizmos_status")) {
  3920. Dictionary gizmos_status = d["gizmos_status"];
  3921. List<Variant> keys;
  3922. gizmos_status.get_key_list(&keys);
  3923. for (int j = 0; j < gizmo_plugins_by_name.size(); ++j) {
  3924. if (!gizmo_plugins_by_name[j]->can_be_hidden()) {
  3925. continue;
  3926. }
  3927. int state = EditorSpatialGizmoPlugin::VISIBLE;
  3928. for (int i = 0; i < keys.size(); i++) {
  3929. if (gizmo_plugins_by_name.write[j]->get_name() == keys[i]) {
  3930. state = gizmos_status[keys[i]];
  3931. break;
  3932. }
  3933. }
  3934. gizmo_plugins_by_name.write[j]->set_state(state);
  3935. }
  3936. _update_gizmos_menu();
  3937. }
  3938. }
  3939. void SpatialEditor::edit(Spatial *p_spatial) {
  3940. if (p_spatial != selected) {
  3941. if (selected) {
  3942. Ref<EditorSpatialGizmo> seg = selected->get_gizmo();
  3943. if (seg.is_valid()) {
  3944. seg->set_selected(false);
  3945. selected->update_gizmo();
  3946. }
  3947. }
  3948. selected = p_spatial;
  3949. over_gizmo_handle = -1;
  3950. if (selected) {
  3951. Ref<EditorSpatialGizmo> seg = selected->get_gizmo();
  3952. if (seg.is_valid()) {
  3953. seg->set_selected(true);
  3954. selected->update_gizmo();
  3955. }
  3956. }
  3957. }
  3958. }
  3959. void SpatialEditor::_snap_changed() {
  3960. snap_translate_value = snap_translate->get_text().to_double();
  3961. snap_rotate_value = snap_rotate->get_text().to_double();
  3962. snap_scale_value = snap_scale->get_text().to_double();
  3963. }
  3964. void SpatialEditor::_snap_update() {
  3965. snap_translate->set_text(String::num(snap_translate_value));
  3966. snap_rotate->set_text(String::num(snap_rotate_value));
  3967. snap_scale->set_text(String::num(snap_scale_value));
  3968. }
  3969. void SpatialEditor::_xform_dialog_action() {
  3970. Transform t;
  3971. //translation
  3972. Vector3 scale;
  3973. Vector3 rotate;
  3974. Vector3 translate;
  3975. for (int i = 0; i < 3; i++) {
  3976. translate[i] = xform_translate[i]->get_text().to_double();
  3977. rotate[i] = Math::deg2rad(xform_rotate[i]->get_text().to_double());
  3978. scale[i] = xform_scale[i]->get_text().to_double();
  3979. }
  3980. t.basis.scale(scale);
  3981. t.basis.rotate(rotate);
  3982. t.origin = translate;
  3983. undo_redo->create_action(TTR("XForm Dialog"));
  3984. List<Node *> &selection = editor_selection->get_selected_node_list();
  3985. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3986. Spatial *sp = Object::cast_to<Spatial>(E->get());
  3987. if (!sp) {
  3988. continue;
  3989. }
  3990. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  3991. if (!se) {
  3992. continue;
  3993. }
  3994. bool post = xform_type->get_selected() > 0;
  3995. Transform tr = sp->get_global_gizmo_transform();
  3996. if (post) {
  3997. tr = tr * t;
  3998. } else {
  3999. tr.basis = t.basis * tr.basis;
  4000. tr.origin += t.origin;
  4001. }
  4002. undo_redo->add_do_method(sp, "set_global_transform", tr);
  4003. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  4004. }
  4005. undo_redo->commit_action();
  4006. }
  4007. void SpatialEditor::_menu_item_toggled(bool pressed, int p_option) {
  4008. switch (p_option) {
  4009. case MENU_TOOL_LOCAL_COORDS: {
  4010. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(pressed);
  4011. update_transform_gizmo();
  4012. } break;
  4013. case MENU_TOOL_USE_SNAP: {
  4014. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(pressed);
  4015. snap_enabled = pressed;
  4016. } break;
  4017. case MENU_TOOL_OVERRIDE_CAMERA: {
  4018. ScriptEditorDebugger *const debugger = ScriptEditor::get_singleton()->get_debugger();
  4019. if (pressed) {
  4020. using Override = ScriptEditorDebugger::CameraOverride;
  4021. debugger->set_camera_override((Override)(Override::OVERRIDE_3D_1 + camera_override_viewport_id));
  4022. } else {
  4023. debugger->set_camera_override(ScriptEditorDebugger::OVERRIDE_NONE);
  4024. }
  4025. } break;
  4026. }
  4027. }
  4028. void SpatialEditor::_menu_gizmo_toggled(int p_option) {
  4029. const int idx = gizmos_menu->get_item_index(p_option);
  4030. gizmos_menu->toggle_item_multistate(idx);
  4031. // Change icon
  4032. const int state = gizmos_menu->get_item_state(idx);
  4033. switch (state) {
  4034. case EditorSpatialGizmoPlugin::VISIBLE:
  4035. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_visible"));
  4036. break;
  4037. case EditorSpatialGizmoPlugin::ON_TOP:
  4038. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_xray"));
  4039. break;
  4040. case EditorSpatialGizmoPlugin::HIDDEN:
  4041. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_hidden"));
  4042. break;
  4043. }
  4044. gizmo_plugins_by_name.write[p_option]->set_state(state);
  4045. update_all_gizmos();
  4046. }
  4047. void SpatialEditor::_update_camera_override_button(bool p_game_running) {
  4048. Button *const button = tool_option_button[TOOL_OPT_OVERRIDE_CAMERA];
  4049. if (p_game_running) {
  4050. button->set_disabled(false);
  4051. button->set_tooltip(TTR("Project Camera Override\nOverrides the running project's camera with the editor viewport camera."));
  4052. } else {
  4053. button->set_disabled(true);
  4054. button->set_pressed(false);
  4055. button->set_tooltip(TTR("Project Camera Override\nNo project instance running. Run the project from the editor to use this feature."));
  4056. }
  4057. }
  4058. void SpatialEditor::_update_camera_override_viewport(Object *p_viewport) {
  4059. SpatialEditorViewport *current_viewport = Object::cast_to<SpatialEditorViewport>(p_viewport);
  4060. if (!current_viewport) {
  4061. return;
  4062. }
  4063. ScriptEditorDebugger *const debugger = ScriptEditor::get_singleton()->get_debugger();
  4064. camera_override_viewport_id = current_viewport->index;
  4065. if (debugger->get_camera_override() >= ScriptEditorDebugger::OVERRIDE_3D_1) {
  4066. using Override = ScriptEditorDebugger::CameraOverride;
  4067. debugger->set_camera_override((Override)(Override::OVERRIDE_3D_1 + camera_override_viewport_id));
  4068. }
  4069. }
  4070. void SpatialEditor::_menu_item_pressed(int p_option) {
  4071. switch (p_option) {
  4072. case MENU_TOOL_SELECT:
  4073. case MENU_TOOL_MOVE:
  4074. case MENU_TOOL_ROTATE:
  4075. case MENU_TOOL_SCALE:
  4076. case MENU_TOOL_LIST_SELECT: {
  4077. for (int i = 0; i < TOOL_MAX; i++) {
  4078. tool_button[i]->set_pressed(i == p_option);
  4079. }
  4080. tool_mode = (ToolMode)p_option;
  4081. update_transform_gizmo();
  4082. } break;
  4083. case MENU_TOOL_CONVERT_ROOMS: {
  4084. RoomManager::static_rooms_convert();
  4085. update_portal_tools();
  4086. } break;
  4087. case MENU_TRANSFORM_CONFIGURE_SNAP: {
  4088. snap_dialog->popup_centered(Size2(200, 180));
  4089. } break;
  4090. case MENU_TRANSFORM_DIALOG: {
  4091. for (int i = 0; i < 3; i++) {
  4092. xform_translate[i]->set_text("0");
  4093. xform_rotate[i]->set_text("0");
  4094. xform_scale[i]->set_text("1");
  4095. }
  4096. xform_dialog->popup_centered(Size2(320, 240) * EDSCALE);
  4097. } break;
  4098. case MENU_VIEW_USE_1_VIEWPORT: {
  4099. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_1_VIEWPORT);
  4100. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true);
  4101. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  4102. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  4103. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  4104. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  4105. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  4106. } break;
  4107. case MENU_VIEW_USE_2_VIEWPORTS: {
  4108. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_2_VIEWPORTS);
  4109. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  4110. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true);
  4111. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  4112. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  4113. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  4114. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  4115. } break;
  4116. case MENU_VIEW_USE_2_VIEWPORTS_ALT: {
  4117. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_2_VIEWPORTS_ALT);
  4118. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  4119. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  4120. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  4121. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  4122. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), true);
  4123. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  4124. } break;
  4125. case MENU_VIEW_USE_3_VIEWPORTS: {
  4126. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_3_VIEWPORTS);
  4127. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  4128. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  4129. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), true);
  4130. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  4131. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  4132. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  4133. } break;
  4134. case MENU_VIEW_USE_3_VIEWPORTS_ALT: {
  4135. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_3_VIEWPORTS_ALT);
  4136. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  4137. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  4138. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  4139. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  4140. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  4141. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), true);
  4142. } break;
  4143. case MENU_VIEW_USE_4_VIEWPORTS: {
  4144. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_4_VIEWPORTS);
  4145. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  4146. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  4147. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  4148. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), true);
  4149. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  4150. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  4151. } break;
  4152. case MENU_VIEW_ORIGIN: {
  4153. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  4154. origin_enabled = !is_checked;
  4155. VisualServer::get_singleton()->instance_set_visible(origin_instance, origin_enabled);
  4156. // Update the grid since its appearance depends on whether the origin is enabled
  4157. _finish_grid();
  4158. _init_grid();
  4159. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), origin_enabled);
  4160. } break;
  4161. case MENU_VIEW_GRID: {
  4162. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  4163. grid_enabled = !is_checked;
  4164. for (int i = 0; i < 3; ++i) {
  4165. if (grid_enable[i]) {
  4166. grid_visible[i] = grid_enabled;
  4167. }
  4168. }
  4169. _finish_grid();
  4170. _init_grid();
  4171. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), grid_enabled);
  4172. } break;
  4173. case MENU_VIEW_PORTAL_CULLING: {
  4174. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  4175. RoomManager::static_rooms_set_active(!is_checked);
  4176. update_portal_tools();
  4177. } break;
  4178. case MENU_VIEW_OCCLUSION_CULLING: {
  4179. int checkbox_id = view_menu->get_popup()->get_item_index(p_option);
  4180. bool is_checked = view_menu->get_popup()->is_item_checked(checkbox_id);
  4181. VisualServer::get_singleton()->set_use_occlusion_culling(!is_checked);
  4182. view_menu->get_popup()->set_item_checked(checkbox_id, !is_checked);
  4183. } break;
  4184. case MENU_VIEW_CAMERA_SETTINGS: {
  4185. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50));
  4186. } break;
  4187. case MENU_SNAP_TO_FLOOR: {
  4188. snap_selected_nodes_to_floor();
  4189. } break;
  4190. case MENU_LOCK_SELECTED: {
  4191. undo_redo->create_action(TTR("Lock Selected"));
  4192. List<Node *> &selection = editor_selection->get_selected_node_list();
  4193. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  4194. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  4195. if (!spatial || !spatial->is_visible_in_tree()) {
  4196. continue;
  4197. }
  4198. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  4199. continue;
  4200. }
  4201. undo_redo->add_do_method(spatial, "set_meta", "_edit_lock_", true);
  4202. undo_redo->add_undo_method(spatial, "remove_meta", "_edit_lock_");
  4203. undo_redo->add_do_method(this, "emit_signal", "item_lock_status_changed");
  4204. undo_redo->add_undo_method(this, "emit_signal", "item_lock_status_changed");
  4205. }
  4206. undo_redo->add_do_method(this, "_refresh_menu_icons", Variant());
  4207. undo_redo->add_undo_method(this, "_refresh_menu_icons", Variant());
  4208. undo_redo->commit_action();
  4209. } break;
  4210. case MENU_UNLOCK_SELECTED: {
  4211. undo_redo->create_action(TTR("Unlock Selected"));
  4212. List<Node *> &selection = editor_selection->get_selected_node_list();
  4213. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  4214. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  4215. if (!spatial || !spatial->is_visible_in_tree()) {
  4216. continue;
  4217. }
  4218. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  4219. continue;
  4220. }
  4221. undo_redo->add_do_method(spatial, "remove_meta", "_edit_lock_");
  4222. undo_redo->add_undo_method(spatial, "set_meta", "_edit_lock_", true);
  4223. undo_redo->add_do_method(this, "emit_signal", "item_lock_status_changed");
  4224. undo_redo->add_undo_method(this, "emit_signal", "item_lock_status_changed");
  4225. }
  4226. undo_redo->add_do_method(this, "_refresh_menu_icons", Variant());
  4227. undo_redo->add_undo_method(this, "_refresh_menu_icons", Variant());
  4228. undo_redo->commit_action();
  4229. } break;
  4230. case MENU_GROUP_SELECTED: {
  4231. undo_redo->create_action(TTR("Group Selected"));
  4232. List<Node *> &selection = editor_selection->get_selected_node_list();
  4233. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  4234. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  4235. if (!spatial || !spatial->is_visible_in_tree()) {
  4236. continue;
  4237. }
  4238. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  4239. continue;
  4240. }
  4241. undo_redo->add_do_method(spatial, "set_meta", "_edit_group_", true);
  4242. undo_redo->add_undo_method(spatial, "remove_meta", "_edit_group_");
  4243. undo_redo->add_do_method(this, "emit_signal", "item_group_status_changed");
  4244. undo_redo->add_undo_method(this, "emit_signal", "item_group_status_changed");
  4245. }
  4246. undo_redo->add_do_method(this, "_refresh_menu_icons", Variant());
  4247. undo_redo->add_undo_method(this, "_refresh_menu_icons", Variant());
  4248. undo_redo->commit_action();
  4249. } break;
  4250. case MENU_UNGROUP_SELECTED: {
  4251. undo_redo->create_action(TTR("Ungroup Selected"));
  4252. List<Node *> &selection = editor_selection->get_selected_node_list();
  4253. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  4254. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  4255. if (!spatial || !spatial->is_visible_in_tree()) {
  4256. continue;
  4257. }
  4258. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  4259. continue;
  4260. }
  4261. undo_redo->add_do_method(spatial, "remove_meta", "_edit_group_");
  4262. undo_redo->add_undo_method(spatial, "set_meta", "_edit_group_", true);
  4263. undo_redo->add_do_method(this, "emit_signal", "item_group_status_changed");
  4264. undo_redo->add_undo_method(this, "emit_signal", "item_group_status_changed");
  4265. }
  4266. undo_redo->add_do_method(this, "_refresh_menu_icons", Variant());
  4267. undo_redo->add_undo_method(this, "_refresh_menu_icons", Variant());
  4268. undo_redo->commit_action();
  4269. } break;
  4270. }
  4271. }
  4272. void SpatialEditor::_init_indicators() {
  4273. {
  4274. origin_enabled = true;
  4275. grid_enabled = true;
  4276. indicator_mat.instance();
  4277. indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  4278. indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  4279. indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  4280. indicator_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  4281. Vector<Color> origin_colors;
  4282. Vector<Vector3> origin_points;
  4283. for (int i = 0; i < 3; i++) {
  4284. Vector3 axis;
  4285. axis[i] = 1;
  4286. Color origin_color;
  4287. switch (i) {
  4288. case 0:
  4289. origin_color = get_color("axis_x_color", "Editor");
  4290. break;
  4291. case 1:
  4292. origin_color = get_color("axis_y_color", "Editor");
  4293. break;
  4294. case 2:
  4295. origin_color = get_color("axis_z_color", "Editor");
  4296. break;
  4297. default:
  4298. origin_color = Color();
  4299. break;
  4300. }
  4301. grid_enable[i] = false;
  4302. grid_visible[i] = false;
  4303. origin_colors.push_back(origin_color);
  4304. origin_colors.push_back(origin_color);
  4305. origin_colors.push_back(origin_color);
  4306. origin_colors.push_back(origin_color);
  4307. origin_colors.push_back(origin_color);
  4308. origin_colors.push_back(origin_color);
  4309. // To both allow having a large origin size and avoid jitter
  4310. // at small scales, we should segment the line into pieces.
  4311. // 3 pieces seems to do the trick, and let's use powers of 2.
  4312. origin_points.push_back(axis * 1048576);
  4313. origin_points.push_back(axis * 1024);
  4314. origin_points.push_back(axis * 1024);
  4315. origin_points.push_back(axis * -1024);
  4316. origin_points.push_back(axis * -1024);
  4317. origin_points.push_back(axis * -1048576);
  4318. }
  4319. Ref<Shader> grid_shader = memnew(Shader);
  4320. grid_shader->set_code(
  4321. "\n"
  4322. "shader_type spatial; \n"
  4323. "render_mode unshaded; \n"
  4324. "uniform bool orthogonal; \n"
  4325. "uniform float grid_size; \n"
  4326. "\n"
  4327. "void vertex() { \n"
  4328. " // From FLAG_SRGB_VERTEX_COLOR \n"
  4329. " if (!OUTPUT_IS_SRGB) { \n"
  4330. " 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))); \n"
  4331. " } \n"
  4332. "} \n"
  4333. "\n"
  4334. "void fragment() { \n"
  4335. " ALBEDO = COLOR.rgb; \n"
  4336. " vec3 dir = orthogonal ? -vec3(0, 0, 1) : VIEW; \n"
  4337. " float angle_fade = abs(dot(dir, NORMAL)); \n"
  4338. " angle_fade = smoothstep(0.05, 0.2, angle_fade); \n"
  4339. " \n"
  4340. " vec3 world_pos = (CAMERA_MATRIX * vec4(VERTEX, 1.0)).xyz; \n"
  4341. " vec3 world_normal = (CAMERA_MATRIX * vec4(NORMAL, 0.0)).xyz; \n"
  4342. " vec3 camera_world_pos = CAMERA_MATRIX[3].xyz; \n"
  4343. " vec3 camera_world_pos_on_plane = camera_world_pos * (1.0 - world_normal); \n"
  4344. " float dist_fade = 1.0 - (distance(world_pos, camera_world_pos_on_plane) / grid_size); \n"
  4345. " dist_fade = smoothstep(0.02, 0.3, dist_fade); \n"
  4346. " \n"
  4347. " ALPHA = COLOR.a * dist_fade * angle_fade; \n"
  4348. "}");
  4349. for (int i = 0; i < 3; i++) {
  4350. grid_mat[i].instance();
  4351. grid_mat[i]->set_shader(grid_shader);
  4352. }
  4353. grid_enable[0] = EditorSettings::get_singleton()->get("editors/3d/grid_xy_plane");
  4354. grid_enable[1] = EditorSettings::get_singleton()->get("editors/3d/grid_yz_plane");
  4355. grid_enable[2] = EditorSettings::get_singleton()->get("editors/3d/grid_xz_plane");
  4356. grid_visible[0] = grid_enable[0];
  4357. grid_visible[1] = grid_enable[1];
  4358. grid_visible[2] = grid_enable[2];
  4359. _init_grid();
  4360. origin = VisualServer::get_singleton()->mesh_create();
  4361. Array d;
  4362. d.resize(VS::ARRAY_MAX);
  4363. d[VisualServer::ARRAY_VERTEX] = origin_points;
  4364. d[VisualServer::ARRAY_COLOR] = origin_colors;
  4365. VisualServer::get_singleton()->mesh_add_surface_from_arrays(origin, VisualServer::PRIMITIVE_LINES, d);
  4366. VisualServer::get_singleton()->mesh_surface_set_material(origin, 0, indicator_mat->get_rid());
  4367. origin_instance = VisualServer::get_singleton()->instance_create2(origin, get_tree()->get_root()->get_world()->get_scenario());
  4368. VS::get_singleton()->instance_set_layer_mask(origin_instance, 1 << SpatialEditorViewport::GIZMO_GRID_LAYER);
  4369. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(origin_instance, VS::SHADOW_CASTING_SETTING_OFF);
  4370. }
  4371. {
  4372. //move gizmo
  4373. for (int i = 0; i < 3; i++) {
  4374. Color col;
  4375. switch (i) {
  4376. case 0:
  4377. col = get_color("axis_x_color", "Editor");
  4378. break;
  4379. case 1:
  4380. col = get_color("axis_y_color", "Editor");
  4381. break;
  4382. case 2:
  4383. col = get_color("axis_z_color", "Editor");
  4384. break;
  4385. default:
  4386. col = Color();
  4387. break;
  4388. }
  4389. col.a = EditorSettings::get_singleton()->get("editors/3d/manipulator_gizmo_opacity");
  4390. move_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  4391. move_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  4392. rotate_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  4393. scale_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  4394. scale_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  4395. Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
  4396. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  4397. mat->set_on_top_of_alpha();
  4398. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  4399. mat->set_albedo(col);
  4400. gizmo_color[i] = mat;
  4401. Ref<SpatialMaterial> mat_hl = mat->duplicate();
  4402. const Color albedo = col.from_hsv(col.get_h(), 0.25, 1.0, 1);
  4403. mat_hl->set_albedo(albedo);
  4404. gizmo_color_hl[i] = mat_hl;
  4405. Vector3 ivec;
  4406. ivec[i] = 1;
  4407. Vector3 nivec;
  4408. nivec[(i + 1) % 3] = 1;
  4409. nivec[(i + 2) % 3] = 1;
  4410. Vector3 ivec2;
  4411. ivec2[(i + 1) % 3] = 1;
  4412. Vector3 ivec3;
  4413. ivec3[(i + 2) % 3] = 1;
  4414. //translate
  4415. {
  4416. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  4417. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  4418. // Arrow profile
  4419. const int arrow_points = 5;
  4420. Vector3 arrow[5] = {
  4421. nivec * 0.0 + ivec * 0.0,
  4422. nivec * 0.01 + ivec * 0.0,
  4423. nivec * 0.01 + ivec * GIZMO_ARROW_OFFSET,
  4424. nivec * 0.065 + ivec * GIZMO_ARROW_OFFSET,
  4425. nivec * 0.0 + ivec * (GIZMO_ARROW_OFFSET + GIZMO_ARROW_SIZE),
  4426. };
  4427. int arrow_sides = 16;
  4428. for (int k = 0; k < arrow_sides; k++) {
  4429. Basis ma(ivec, Math_PI * 2 * float(k) / arrow_sides);
  4430. Basis mb(ivec, Math_PI * 2 * float(k + 1) / arrow_sides);
  4431. for (int j = 0; j < arrow_points - 1; j++) {
  4432. Vector3 points[4] = {
  4433. ma.xform(arrow[j]),
  4434. mb.xform(arrow[j]),
  4435. mb.xform(arrow[j + 1]),
  4436. ma.xform(arrow[j + 1]),
  4437. };
  4438. surftool->add_vertex(points[0]);
  4439. surftool->add_vertex(points[1]);
  4440. surftool->add_vertex(points[2]);
  4441. surftool->add_vertex(points[0]);
  4442. surftool->add_vertex(points[2]);
  4443. surftool->add_vertex(points[3]);
  4444. }
  4445. }
  4446. surftool->set_material(mat);
  4447. surftool->commit(move_gizmo[i]);
  4448. }
  4449. // Plane Translation
  4450. {
  4451. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  4452. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  4453. Vector3 vec = ivec2 - ivec3;
  4454. Vector3 plane[4] = {
  4455. vec * GIZMO_PLANE_DST,
  4456. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  4457. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  4458. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  4459. };
  4460. Basis ma(ivec, Math_PI / 2);
  4461. Vector3 points[4] = {
  4462. ma.xform(plane[0]),
  4463. ma.xform(plane[1]),
  4464. ma.xform(plane[2]),
  4465. ma.xform(plane[3]),
  4466. };
  4467. surftool->add_vertex(points[0]);
  4468. surftool->add_vertex(points[1]);
  4469. surftool->add_vertex(points[2]);
  4470. surftool->add_vertex(points[0]);
  4471. surftool->add_vertex(points[2]);
  4472. surftool->add_vertex(points[3]);
  4473. Ref<SpatialMaterial> plane_mat = memnew(SpatialMaterial);
  4474. plane_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  4475. plane_mat->set_on_top_of_alpha();
  4476. plane_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  4477. plane_mat->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  4478. plane_mat->set_albedo(col);
  4479. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  4480. surftool->set_material(plane_mat);
  4481. surftool->commit(move_plane_gizmo[i]);
  4482. Ref<SpatialMaterial> plane_mat_hl = plane_mat->duplicate();
  4483. plane_mat_hl->set_albedo(albedo);
  4484. plane_gizmo_color_hl[i] = plane_mat_hl; // needed, so we can draw planes from both sides
  4485. }
  4486. // Rotate
  4487. {
  4488. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  4489. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  4490. int n = 128; // number of circle segments
  4491. int m = 3; // number of thickness segments
  4492. for (int j = 0; j < n; ++j) {
  4493. Basis basis = Basis(ivec, (Math_PI * 2.0f * j) / n);
  4494. Vector3 vertex = basis.xform(ivec2 * GIZMO_CIRCLE_SIZE);
  4495. for (int k = 0; k < m; ++k) {
  4496. Vector2 ofs = Vector2(Math::cos((Math_PI * 2.0 * k) / m), Math::sin((Math_PI * 2.0 * k) / m));
  4497. Vector3 normal = ivec * ofs.x + ivec2 * ofs.y;
  4498. surftool->add_normal(basis.xform(normal));
  4499. surftool->add_vertex(vertex);
  4500. }
  4501. }
  4502. for (int j = 0; j < n; ++j) {
  4503. for (int k = 0; k < m; ++k) {
  4504. int current_ring = j * m;
  4505. int next_ring = ((j + 1) % n) * m;
  4506. int current_segment = k;
  4507. int next_segment = (k + 1) % m;
  4508. surftool->add_index(current_ring + next_segment);
  4509. surftool->add_index(current_ring + current_segment);
  4510. surftool->add_index(next_ring + current_segment);
  4511. surftool->add_index(next_ring + current_segment);
  4512. surftool->add_index(next_ring + next_segment);
  4513. surftool->add_index(current_ring + next_segment);
  4514. }
  4515. }
  4516. Ref<Shader> rotate_shader = memnew(Shader);
  4517. rotate_shader->set_code(
  4518. "\n"
  4519. "shader_type spatial; \n"
  4520. "render_mode unshaded, depth_test_disable; \n"
  4521. "uniform vec4 albedo; \n"
  4522. "\n"
  4523. "mat3 orthonormalize(mat3 m) { \n"
  4524. " vec3 x = normalize(m[0]); \n"
  4525. " vec3 y = normalize(m[1] - x * dot(x, m[1])); \n"
  4526. " vec3 z = m[2] - x * dot(x, m[2]); \n"
  4527. " z = normalize(z - y * (dot(y,m[2]))); \n"
  4528. " return mat3(x,y,z); \n"
  4529. "} \n"
  4530. "\n"
  4531. "void vertex() { \n"
  4532. " mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX)); \n"
  4533. " vec3 n = mv * VERTEX; \n"
  4534. " float orientation = dot(vec3(0,0,-1),n); \n"
  4535. " if (orientation <= 0.005) { \n"
  4536. " VERTEX += NORMAL*0.02; \n"
  4537. " } \n"
  4538. "} \n"
  4539. "\n"
  4540. "void fragment() { \n"
  4541. " ALBEDO = albedo.rgb; \n"
  4542. " ALPHA = albedo.a; \n"
  4543. "}");
  4544. Ref<ShaderMaterial> rotate_mat = memnew(ShaderMaterial);
  4545. rotate_mat->set_render_priority(Material::RENDER_PRIORITY_MAX);
  4546. rotate_mat->set_shader(rotate_shader);
  4547. rotate_mat->set_shader_param("albedo", col);
  4548. rotate_gizmo_color[i] = rotate_mat;
  4549. Array arrays = surftool->commit_to_arrays();
  4550. rotate_gizmo[i]->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
  4551. rotate_gizmo[i]->surface_set_material(0, rotate_mat);
  4552. Ref<ShaderMaterial> rotate_mat_hl = rotate_mat->duplicate();
  4553. rotate_mat_hl->set_shader_param("albedo", albedo);
  4554. rotate_gizmo_color_hl[i] = rotate_mat_hl;
  4555. if (i == 2) { // Rotation white outline
  4556. Ref<ShaderMaterial> border_mat = rotate_mat->duplicate();
  4557. Ref<Shader> border_shader = memnew(Shader);
  4558. border_shader->set_code(
  4559. "\n"
  4560. "shader_type spatial; \n"
  4561. "render_mode unshaded, depth_test_disable; \n"
  4562. "uniform vec4 albedo; \n"
  4563. "\n"
  4564. "mat3 orthonormalize(mat3 m) { \n"
  4565. " vec3 x = normalize(m[0]); \n"
  4566. " vec3 y = normalize(m[1] - x * dot(x, m[1])); \n"
  4567. " vec3 z = m[2] - x * dot(x, m[2]); \n"
  4568. " z = normalize(z - y * (dot(y,m[2]))); \n"
  4569. " return mat3(x,y,z); \n"
  4570. "} \n"
  4571. "\n"
  4572. "void vertex() { \n"
  4573. " mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX)); \n"
  4574. " mv = inverse(mv); \n"
  4575. " VERTEX += NORMAL*0.008; \n"
  4576. " vec3 camera_dir_local = mv * vec3(0,0,1); \n"
  4577. " vec3 camera_up_local = mv * vec3(0,1,0); \n"
  4578. " mat3 rotation_matrix = mat3(cross(camera_dir_local, camera_up_local), camera_up_local, camera_dir_local); \n"
  4579. " VERTEX = rotation_matrix * VERTEX; \n"
  4580. "} \n"
  4581. "\n"
  4582. "void fragment() { \n"
  4583. " ALBEDO = albedo.rgb; \n"
  4584. " ALPHA = albedo.a; \n"
  4585. "}");
  4586. border_mat->set_shader(border_shader);
  4587. border_mat->set_shader_param("albedo", Color(0.75, 0.75, 0.75, col.a / 3.0));
  4588. rotate_gizmo[3] = Ref<ArrayMesh>(memnew(ArrayMesh));
  4589. rotate_gizmo[3]->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
  4590. rotate_gizmo[3]->surface_set_material(0, border_mat);
  4591. }
  4592. }
  4593. // Scale
  4594. {
  4595. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  4596. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  4597. // Cube arrow profile
  4598. const int arrow_points = 6;
  4599. Vector3 arrow[6] = {
  4600. nivec * 0.0 + ivec * 0.0,
  4601. nivec * 0.01 + ivec * 0.0,
  4602. nivec * 0.01 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  4603. nivec * 0.07 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  4604. nivec * 0.07 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  4605. nivec * 0.0 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  4606. };
  4607. int arrow_sides = 4;
  4608. for (int k = 0; k < 4; k++) {
  4609. Basis ma(ivec, Math_PI * 2 * float(k) / arrow_sides);
  4610. Basis mb(ivec, Math_PI * 2 * float(k + 1) / arrow_sides);
  4611. for (int j = 0; j < arrow_points - 1; j++) {
  4612. Vector3 points[4] = {
  4613. ma.xform(arrow[j]),
  4614. mb.xform(arrow[j]),
  4615. mb.xform(arrow[j + 1]),
  4616. ma.xform(arrow[j + 1]),
  4617. };
  4618. surftool->add_vertex(points[0]);
  4619. surftool->add_vertex(points[1]);
  4620. surftool->add_vertex(points[2]);
  4621. surftool->add_vertex(points[0]);
  4622. surftool->add_vertex(points[2]);
  4623. surftool->add_vertex(points[3]);
  4624. }
  4625. }
  4626. surftool->set_material(mat);
  4627. surftool->commit(scale_gizmo[i]);
  4628. }
  4629. // Plane Scale
  4630. {
  4631. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  4632. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  4633. Vector3 vec = ivec2 - ivec3;
  4634. Vector3 plane[4] = {
  4635. vec * GIZMO_PLANE_DST,
  4636. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  4637. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  4638. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  4639. };
  4640. Basis ma(ivec, Math_PI / 2);
  4641. Vector3 points[4] = {
  4642. ma.xform(plane[0]),
  4643. ma.xform(plane[1]),
  4644. ma.xform(plane[2]),
  4645. ma.xform(plane[3]),
  4646. };
  4647. surftool->add_vertex(points[0]);
  4648. surftool->add_vertex(points[1]);
  4649. surftool->add_vertex(points[2]);
  4650. surftool->add_vertex(points[0]);
  4651. surftool->add_vertex(points[2]);
  4652. surftool->add_vertex(points[3]);
  4653. Ref<SpatialMaterial> plane_mat = memnew(SpatialMaterial);
  4654. plane_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  4655. plane_mat->set_on_top_of_alpha();
  4656. plane_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  4657. plane_mat->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  4658. plane_mat->set_albedo(col);
  4659. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  4660. surftool->set_material(plane_mat);
  4661. surftool->commit(scale_plane_gizmo[i]);
  4662. Ref<SpatialMaterial> plane_mat_hl = plane_mat->duplicate();
  4663. plane_mat_hl->set_albedo(col.from_hsv(col.get_h(), 0.25, 1.0, 1));
  4664. plane_gizmo_color_hl[i] = plane_mat_hl; // needed, so we can draw planes from both sides
  4665. }
  4666. }
  4667. }
  4668. _generate_selection_boxes();
  4669. }
  4670. void SpatialEditor::_update_context_menu_stylebox() {
  4671. // This must be called when the theme changes to follow the new accent color.
  4672. Ref<StyleBoxFlat> context_menu_stylebox = memnew(StyleBoxFlat);
  4673. const Color accent_color = EditorNode::get_singleton()->get_gui_base()->get_color("accent_color", "Editor");
  4674. context_menu_stylebox->set_bg_color(accent_color * Color(1, 1, 1, 0.1));
  4675. // Add an underline to the StyleBox, but prevent its minimum vertical size from changing.
  4676. context_menu_stylebox->set_border_color(accent_color);
  4677. context_menu_stylebox->set_border_width(MARGIN_BOTTOM, Math::round(2 * EDSCALE));
  4678. context_menu_stylebox->set_default_margin(MARGIN_BOTTOM, 0);
  4679. context_menu_container->add_style_override("panel", context_menu_stylebox);
  4680. }
  4681. void SpatialEditor::_update_gizmos_menu() {
  4682. gizmos_menu->clear();
  4683. for (int i = 0; i < gizmo_plugins_by_name.size(); ++i) {
  4684. if (!gizmo_plugins_by_name[i]->can_be_hidden()) {
  4685. continue;
  4686. }
  4687. String plugin_name = gizmo_plugins_by_name[i]->get_name();
  4688. const int plugin_state = gizmo_plugins_by_name[i]->get_state();
  4689. gizmos_menu->add_multistate_item(TTR(plugin_name), 3, plugin_state, i);
  4690. const int idx = gizmos_menu->get_item_index(i);
  4691. gizmos_menu->set_item_tooltip(
  4692. idx,
  4693. 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\")."));
  4694. switch (plugin_state) {
  4695. case EditorSpatialGizmoPlugin::VISIBLE:
  4696. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_visible"));
  4697. break;
  4698. case EditorSpatialGizmoPlugin::ON_TOP:
  4699. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_xray"));
  4700. break;
  4701. case EditorSpatialGizmoPlugin::HIDDEN:
  4702. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_hidden"));
  4703. break;
  4704. }
  4705. }
  4706. }
  4707. void SpatialEditor::_update_gizmos_menu_theme() {
  4708. for (int i = 0; i < gizmo_plugins_by_name.size(); ++i) {
  4709. if (!gizmo_plugins_by_name[i]->can_be_hidden()) {
  4710. continue;
  4711. }
  4712. const int plugin_state = gizmo_plugins_by_name[i]->get_state();
  4713. const int idx = gizmos_menu->get_item_index(i);
  4714. switch (plugin_state) {
  4715. case EditorSpatialGizmoPlugin::VISIBLE:
  4716. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_visible"));
  4717. break;
  4718. case EditorSpatialGizmoPlugin::ON_TOP:
  4719. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_xray"));
  4720. break;
  4721. case EditorSpatialGizmoPlugin::HIDDEN:
  4722. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_hidden"));
  4723. break;
  4724. }
  4725. }
  4726. }
  4727. void SpatialEditor::_init_grid() {
  4728. if (!grid_enabled) {
  4729. return;
  4730. }
  4731. Camera *camera = get_editor_viewport(0)->camera;
  4732. Vector3 camera_position = camera->get_translation();
  4733. if (camera_position == Vector3()) {
  4734. return; // Camera is invalid, don't draw the grid.
  4735. }
  4736. bool orthogonal = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL;
  4737. PoolVector<Color> grid_colors[3];
  4738. PoolVector<Vector3> grid_points[3];
  4739. PoolVector<Vector3> grid_normals[3];
  4740. Color primary_grid_color = EditorSettings::get_singleton()->get("editors/3d/primary_grid_color");
  4741. Color secondary_grid_color = EditorSettings::get_singleton()->get("editors/3d/secondary_grid_color");
  4742. int grid_size = EditorSettings::get_singleton()->get("editors/3d/grid_size");
  4743. int primary_grid_steps = EditorSettings::get_singleton()->get("editors/3d/primary_grid_steps");
  4744. // Which grid planes are enabled? Which should we generate?
  4745. grid_enable[0] = grid_visible[0] = EditorSettings::get_singleton()->get("editors/3d/grid_xy_plane");
  4746. grid_enable[1] = grid_visible[1] = EditorSettings::get_singleton()->get("editors/3d/grid_yz_plane");
  4747. grid_enable[2] = grid_visible[2] = EditorSettings::get_singleton()->get("editors/3d/grid_xz_plane");
  4748. // Offsets division_level for bigger or smaller grids.
  4749. // Default value is -0.2. -1.0 gives Blender-like behavior, 0.5 gives huge grids.
  4750. real_t division_level_bias = EditorSettings::get_singleton()->get("editors/3d/grid_division_level_bias");
  4751. // Default largest grid size is 8^2 when primary_grid_steps is 8 (64m apart, so primary grid lines are 512m apart).
  4752. int division_level_max = EditorSettings::get_singleton()->get("editors/3d/grid_division_level_max");
  4753. // Default smallest grid size is 1cm, 10^-2 (default value is -2).
  4754. int division_level_min = EditorSettings::get_singleton()->get("editors/3d/grid_division_level_min");
  4755. 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.");
  4756. if (primary_grid_steps != 10) { // Log10 of 10 is 1.
  4757. // Change of base rule, divide by ln(10).
  4758. real_t div = Math::log((real_t)primary_grid_steps) / (real_t)2.302585092994045901094;
  4759. // Trucation (towards zero) is intentional.
  4760. division_level_max = (int)(division_level_max / div);
  4761. division_level_min = (int)(division_level_min / div);
  4762. }
  4763. for (int a = 0; a < 3; a++) {
  4764. if (!grid_enable[a]) {
  4765. continue; // If this grid plane is disabled, skip generation.
  4766. }
  4767. int b = (a + 1) % 3;
  4768. int c = (a + 2) % 3;
  4769. Vector3 normal;
  4770. normal[c] = 1.0;
  4771. real_t camera_distance = Math::abs(camera_position[c]);
  4772. if (orthogonal) {
  4773. camera_distance = camera->get_size() / 2.0;
  4774. Vector3 camera_direction = -camera->get_global_transform().get_basis().get_axis(2);
  4775. Plane grid_plane = Plane(Vector3(), normal);
  4776. Vector3 intersection;
  4777. if (grid_plane.intersects_ray(camera_position, camera_direction, &intersection)) {
  4778. camera_position = intersection;
  4779. }
  4780. }
  4781. real_t division_level = Math::log(Math::abs(camera_distance)) / Math::log((double)primary_grid_steps) + division_level_bias;
  4782. real_t clamped_division_level = CLAMP(division_level, division_level_min, division_level_max);
  4783. real_t division_level_floored = Math::floor(clamped_division_level);
  4784. real_t division_level_decimals = clamped_division_level - division_level_floored;
  4785. real_t small_step_size = Math::pow(primary_grid_steps, division_level_floored);
  4786. real_t large_step_size = small_step_size * primary_grid_steps;
  4787. real_t center_a = large_step_size * (int)(camera_position[a] / large_step_size);
  4788. real_t center_b = large_step_size * (int)(camera_position[b] / large_step_size);
  4789. real_t bgn_a = center_a - grid_size * small_step_size;
  4790. real_t end_a = center_a + grid_size * small_step_size;
  4791. real_t bgn_b = center_b - grid_size * small_step_size;
  4792. real_t end_b = center_b + grid_size * small_step_size;
  4793. real_t fade_size = Math::pow(primary_grid_steps, division_level - 1.0);
  4794. real_t min_fade_size = Math::pow(primary_grid_steps, float(division_level_min));
  4795. real_t max_fade_size = Math::pow(primary_grid_steps, float(division_level_max));
  4796. fade_size = CLAMP(fade_size, min_fade_size, max_fade_size);
  4797. real_t grid_fade_size = (grid_size - primary_grid_steps) * fade_size;
  4798. grid_mat[c]->set_shader_param("grid_size", grid_fade_size);
  4799. grid_mat[c]->set_shader_param("orthogonal", orthogonal);
  4800. // In each iteration of this loop, draw one line in each direction (so two lines per loop, in each if statement).
  4801. for (int i = -grid_size; i <= grid_size; i++) {
  4802. Color line_color;
  4803. // Is this a primary line? Set the appropriate color.
  4804. if (i % primary_grid_steps == 0) {
  4805. line_color = primary_grid_color.linear_interpolate(secondary_grid_color, division_level_decimals);
  4806. } else {
  4807. line_color = secondary_grid_color;
  4808. line_color.a = line_color.a * (1 - division_level_decimals);
  4809. }
  4810. real_t position_a = center_a + i * small_step_size;
  4811. real_t position_b = center_b + i * small_step_size;
  4812. // Don't draw lines over the origin if it's enabled.
  4813. if (!(origin_enabled && Math::is_zero_approx(position_a))) {
  4814. Vector3 line_bgn = Vector3();
  4815. Vector3 line_end = Vector3();
  4816. line_bgn[a] = position_a;
  4817. line_end[a] = position_a;
  4818. line_bgn[b] = bgn_b;
  4819. line_end[b] = end_b;
  4820. grid_points[c].push_back(line_bgn);
  4821. grid_points[c].push_back(line_end);
  4822. grid_colors[c].push_back(line_color);
  4823. grid_colors[c].push_back(line_color);
  4824. grid_normals[c].push_back(normal);
  4825. grid_normals[c].push_back(normal);
  4826. }
  4827. if (!(origin_enabled && Math::is_zero_approx(position_b))) {
  4828. Vector3 line_bgn = Vector3();
  4829. Vector3 line_end = Vector3();
  4830. line_bgn[b] = position_b;
  4831. line_end[b] = position_b;
  4832. line_bgn[a] = bgn_a;
  4833. line_end[a] = end_a;
  4834. grid_points[c].push_back(line_bgn);
  4835. grid_points[c].push_back(line_end);
  4836. grid_colors[c].push_back(line_color);
  4837. grid_colors[c].push_back(line_color);
  4838. grid_normals[c].push_back(normal);
  4839. grid_normals[c].push_back(normal);
  4840. }
  4841. }
  4842. // Create a mesh from the pushed vector points and colors.
  4843. grid[c] = VisualServer::get_singleton()->mesh_create();
  4844. Array d;
  4845. d.resize(VS::ARRAY_MAX);
  4846. d[VisualServer::ARRAY_VERTEX] = grid_points[c];
  4847. d[VisualServer::ARRAY_COLOR] = grid_colors[c];
  4848. d[VisualServer::ARRAY_NORMAL] = grid_normals[c];
  4849. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[c], VisualServer::PRIMITIVE_LINES, d);
  4850. VisualServer::get_singleton()->mesh_surface_set_material(grid[c], 0, grid_mat[c]->get_rid());
  4851. grid_instance[c] = VisualServer::get_singleton()->instance_create2(grid[c], get_tree()->get_root()->get_world()->get_scenario());
  4852. // Yes, the end of this line is supposed to be a.
  4853. VisualServer::get_singleton()->instance_set_visible(grid_instance[c], grid_visible[a]);
  4854. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(grid_instance[c], VS::SHADOW_CASTING_SETTING_OFF);
  4855. VS::get_singleton()->instance_set_layer_mask(grid_instance[c], 1 << SpatialEditorViewport::GIZMO_GRID_LAYER);
  4856. }
  4857. }
  4858. void SpatialEditor::_finish_indicators() {
  4859. VisualServer::get_singleton()->free(origin_instance);
  4860. VisualServer::get_singleton()->free(origin);
  4861. _finish_grid();
  4862. }
  4863. void SpatialEditor::_finish_grid() {
  4864. for (int i = 0; i < 3; i++) {
  4865. VisualServer::get_singleton()->free(grid_instance[i]);
  4866. VisualServer::get_singleton()->free(grid[i]);
  4867. }
  4868. }
  4869. void SpatialEditor::update_grid() {
  4870. _finish_grid();
  4871. _init_grid();
  4872. }
  4873. bool SpatialEditor::is_any_freelook_active() const {
  4874. for (unsigned int i = 0; i < VIEWPORTS_COUNT; ++i) {
  4875. if (viewports[i]->is_freelook_active()) {
  4876. return true;
  4877. }
  4878. }
  4879. return false;
  4880. }
  4881. void SpatialEditor::_refresh_menu_icons() {
  4882. bool all_locked = true;
  4883. bool all_grouped = true;
  4884. List<Node *> &selection = editor_selection->get_selected_node_list();
  4885. if (selection.empty()) {
  4886. all_locked = false;
  4887. all_grouped = false;
  4888. } else {
  4889. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  4890. if (Object::cast_to<Spatial>(E->get()) && !Object::cast_to<Spatial>(E->get())->has_meta("_edit_lock_")) {
  4891. all_locked = false;
  4892. break;
  4893. }
  4894. }
  4895. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  4896. if (Object::cast_to<Spatial>(E->get()) && !Object::cast_to<Spatial>(E->get())->has_meta("_edit_group_")) {
  4897. all_grouped = false;
  4898. break;
  4899. }
  4900. }
  4901. }
  4902. tool_button[TOOL_LOCK_SELECTED]->set_visible(!all_locked);
  4903. tool_button[TOOL_LOCK_SELECTED]->set_disabled(selection.empty());
  4904. tool_button[TOOL_UNLOCK_SELECTED]->set_visible(all_locked);
  4905. tool_button[TOOL_GROUP_SELECTED]->set_visible(!all_grouped);
  4906. tool_button[TOOL_GROUP_SELECTED]->set_disabled(selection.empty());
  4907. tool_button[TOOL_UNGROUP_SELECTED]->set_visible(all_grouped);
  4908. }
  4909. template <typename T>
  4910. Set<T *> _get_child_nodes(Node *parent_node) {
  4911. Set<T *> nodes = Set<T *>();
  4912. T *node = Node::cast_to<T>(parent_node);
  4913. if (node) {
  4914. nodes.insert(node);
  4915. }
  4916. for (int i = 0; i < parent_node->get_child_count(); i++) {
  4917. Node *child_node = parent_node->get_child(i);
  4918. Set<T *> child_nodes = _get_child_nodes<T>(child_node);
  4919. for (typename Set<T *>::Element *I = child_nodes.front(); I; I = I->next()) {
  4920. nodes.insert(I->get());
  4921. }
  4922. }
  4923. return nodes;
  4924. }
  4925. Set<RID> _get_physics_bodies_rid(Node *node) {
  4926. Set<RID> rids = Set<RID>();
  4927. PhysicsBody *pb = Node::cast_to<PhysicsBody>(node);
  4928. if (pb) {
  4929. rids.insert(pb->get_rid());
  4930. }
  4931. Set<PhysicsBody *> child_nodes = _get_child_nodes<PhysicsBody>(node);
  4932. for (Set<PhysicsBody *>::Element *I = child_nodes.front(); I; I = I->next()) {
  4933. rids.insert(I->get()->get_rid());
  4934. }
  4935. return rids;
  4936. }
  4937. void SpatialEditor::snap_selected_nodes_to_floor() {
  4938. List<Node *> &selection = editor_selection->get_selected_node_list();
  4939. Dictionary snap_data;
  4940. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  4941. Spatial *sp = Object::cast_to<Spatial>(E->get());
  4942. if (sp) {
  4943. Vector3 from = Vector3();
  4944. Vector3 position_offset = Vector3();
  4945. // Priorities for snapping to floor are CollisionShapes, VisualInstances and then origin
  4946. Set<VisualInstance *> vi = _get_child_nodes<VisualInstance>(sp);
  4947. Set<CollisionShape *> cs = _get_child_nodes<CollisionShape>(sp);
  4948. bool found_valid_shape = false;
  4949. if (cs.size()) {
  4950. AABB aabb;
  4951. Set<CollisionShape *>::Element *I = cs.front();
  4952. if (I->get()->get_shape().is_valid()) {
  4953. CollisionShape *collision_shape = cs.front()->get();
  4954. aabb = collision_shape->get_global_transform().xform(collision_shape->get_shape()->get_debug_mesh()->get_aabb());
  4955. found_valid_shape = true;
  4956. }
  4957. for (I = I->next(); I; I = I->next()) {
  4958. CollisionShape *col_shape = I->get();
  4959. if (col_shape->get_shape().is_valid()) {
  4960. aabb.merge_with(col_shape->get_global_transform().xform(col_shape->get_shape()->get_debug_mesh()->get_aabb()));
  4961. found_valid_shape = true;
  4962. }
  4963. }
  4964. if (found_valid_shape) {
  4965. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  4966. from = aabb.position + size;
  4967. position_offset.y = from.y - sp->get_global_transform().origin.y;
  4968. }
  4969. }
  4970. if (!found_valid_shape && vi.size()) {
  4971. AABB aabb = vi.front()->get()->get_transformed_aabb();
  4972. for (Set<VisualInstance *>::Element *I = vi.front(); I; I = I->next()) {
  4973. aabb.merge_with(I->get()->get_transformed_aabb());
  4974. }
  4975. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  4976. from = aabb.position + size;
  4977. position_offset.y = from.y - sp->get_global_transform().origin.y;
  4978. } else if (!found_valid_shape) {
  4979. from = sp->get_global_transform().origin;
  4980. }
  4981. // We add a bit of margin to the from position to avoid it from snapping
  4982. // when the spatial is already on a floor and there's another floor under
  4983. // it
  4984. from = from + Vector3(0.0, 0.2, 0.0);
  4985. Dictionary d;
  4986. d["from"] = from;
  4987. d["position_offset"] = position_offset;
  4988. snap_data[sp] = d;
  4989. }
  4990. }
  4991. PhysicsDirectSpaceState *ss = get_tree()->get_root()->get_world()->get_direct_space_state();
  4992. PhysicsDirectSpaceState::RayResult result;
  4993. Array keys = snap_data.keys();
  4994. // The maximum height an object can travel to be snapped
  4995. const float max_snap_height = 20.0;
  4996. // Will be set to `true` if at least one node from the selection was successfully snapped
  4997. bool snapped_to_floor = false;
  4998. if (keys.size()) {
  4999. // For snapping to be performed, there must be solid geometry under at least one of the selected nodes.
  5000. // We need to check this before snapping to register the undo/redo action only if needed.
  5001. for (int i = 0; i < keys.size(); i++) {
  5002. Node *node = keys[i];
  5003. Spatial *sp = Object::cast_to<Spatial>(node);
  5004. Dictionary d = snap_data[node];
  5005. Vector3 from = d["from"];
  5006. Vector3 to = from - Vector3(0.0, max_snap_height, 0.0);
  5007. Set<RID> excluded = _get_physics_bodies_rid(sp);
  5008. if (ss->intersect_ray(from, to, result, excluded)) {
  5009. snapped_to_floor = true;
  5010. }
  5011. }
  5012. if (snapped_to_floor) {
  5013. undo_redo->create_action(TTR("Snap Nodes to Floor"));
  5014. // Perform snapping if at least one node can be snapped
  5015. for (int i = 0; i < keys.size(); i++) {
  5016. Node *node = keys[i];
  5017. Spatial *sp = Object::cast_to<Spatial>(node);
  5018. Dictionary d = snap_data[node];
  5019. Vector3 from = d["from"];
  5020. Vector3 to = from - Vector3(0.0, max_snap_height, 0.0);
  5021. Set<RID> excluded = _get_physics_bodies_rid(sp);
  5022. if (ss->intersect_ray(from, to, result, excluded)) {
  5023. Vector3 position_offset = d["position_offset"];
  5024. Transform new_transform = sp->get_global_transform();
  5025. new_transform.origin.y = result.position.y;
  5026. new_transform.origin = new_transform.origin - position_offset;
  5027. undo_redo->add_do_method(sp, "set_global_transform", new_transform);
  5028. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_transform());
  5029. }
  5030. }
  5031. undo_redo->commit_action();
  5032. } else {
  5033. EditorNode::get_singleton()->show_warning(TTR("Couldn't find a solid floor to snap the selection to."));
  5034. }
  5035. }
  5036. }
  5037. void SpatialEditor::_unhandled_key_input(Ref<InputEvent> p_event) {
  5038. ERR_FAIL_COND(p_event.is_null());
  5039. if (!is_visible_in_tree() || get_viewport()->gui_has_modal_stack()) {
  5040. return;
  5041. }
  5042. snap_key_enabled = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  5043. }
  5044. void SpatialEditor::_notification(int p_what) {
  5045. if (p_what == NOTIFICATION_READY) {
  5046. tool_button[SpatialEditor::TOOL_MODE_SELECT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  5047. tool_button[SpatialEditor::TOOL_MODE_MOVE]->set_icon(get_icon("ToolMove", "EditorIcons"));
  5048. tool_button[SpatialEditor::TOOL_MODE_ROTATE]->set_icon(get_icon("ToolRotate", "EditorIcons"));
  5049. tool_button[SpatialEditor::TOOL_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons"));
  5050. tool_button[SpatialEditor::TOOL_MODE_LIST_SELECT]->set_icon(get_icon("ListSelect", "EditorIcons"));
  5051. tool_button[SpatialEditor::TOOL_LOCK_SELECTED]->set_icon(get_icon("Lock", "EditorIcons"));
  5052. tool_button[SpatialEditor::TOOL_UNLOCK_SELECTED]->set_icon(get_icon("Unlock", "EditorIcons"));
  5053. tool_button[SpatialEditor::TOOL_GROUP_SELECTED]->set_icon(get_icon("Group", "EditorIcons"));
  5054. tool_button[SpatialEditor::TOOL_UNGROUP_SELECTED]->set_icon(get_icon("Ungroup", "EditorIcons"));
  5055. tool_button[SpatialEditor::TOOL_CONVERT_ROOMS]->set_icon(get_icon("RoomGroup", "EditorIcons"));
  5056. tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->set_icon(get_icon("Object", "EditorIcons"));
  5057. tool_option_button[SpatialEditor::TOOL_OPT_USE_SNAP]->set_icon(get_icon("Snap", "EditorIcons"));
  5058. tool_option_button[SpatialEditor::TOOL_OPT_OVERRIDE_CAMERA]->set_icon(get_icon("Camera", "EditorIcons"));
  5059. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_icon("Panels1", "EditorIcons"));
  5060. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_icon("Panels2", "EditorIcons"));
  5061. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_icon("Panels2Alt", "EditorIcons"));
  5062. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_icon("Panels3", "EditorIcons"));
  5063. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_icon("Panels3Alt", "EditorIcons"));
  5064. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_icon("Panels4", "EditorIcons"));
  5065. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  5066. _refresh_menu_icons();
  5067. get_tree()->connect("node_removed", this, "_node_removed");
  5068. EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor()->connect("node_changed", this, "_refresh_menu_icons");
  5069. editor_selection->connect("selection_changed", this, "_refresh_menu_icons");
  5070. editor->connect("stop_pressed", this, "_update_camera_override_button", make_binds(false));
  5071. editor->connect("play_pressed", this, "_update_camera_override_button", make_binds(true));
  5072. } else if (p_what == NOTIFICATION_ENTER_TREE) {
  5073. _register_all_gizmos();
  5074. _update_gizmos_menu();
  5075. _init_indicators();
  5076. } else if (p_what == NOTIFICATION_THEME_CHANGED) {
  5077. _update_gizmos_menu_theme();
  5078. _update_context_menu_stylebox();
  5079. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  5080. _finish_indicators();
  5081. } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  5082. tool_button[SpatialEditor::TOOL_MODE_SELECT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  5083. tool_button[SpatialEditor::TOOL_MODE_MOVE]->set_icon(get_icon("ToolMove", "EditorIcons"));
  5084. tool_button[SpatialEditor::TOOL_MODE_ROTATE]->set_icon(get_icon("ToolRotate", "EditorIcons"));
  5085. tool_button[SpatialEditor::TOOL_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons"));
  5086. tool_button[SpatialEditor::TOOL_MODE_LIST_SELECT]->set_icon(get_icon("ListSelect", "EditorIcons"));
  5087. tool_button[SpatialEditor::TOOL_LOCK_SELECTED]->set_icon(get_icon("Lock", "EditorIcons"));
  5088. tool_button[SpatialEditor::TOOL_UNLOCK_SELECTED]->set_icon(get_icon("Unlock", "EditorIcons"));
  5089. tool_button[SpatialEditor::TOOL_GROUP_SELECTED]->set_icon(get_icon("Group", "EditorIcons"));
  5090. tool_button[SpatialEditor::TOOL_UNGROUP_SELECTED]->set_icon(get_icon("Ungroup", "EditorIcons"));
  5091. tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->set_icon(get_icon("Object", "EditorIcons"));
  5092. tool_option_button[SpatialEditor::TOOL_OPT_USE_SNAP]->set_icon(get_icon("Snap", "EditorIcons"));
  5093. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_icon("Panels1", "EditorIcons"));
  5094. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_icon("Panels2", "EditorIcons"));
  5095. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_icon("Panels2Alt", "EditorIcons"));
  5096. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_icon("Panels3", "EditorIcons"));
  5097. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_icon("Panels3Alt", "EditorIcons"));
  5098. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_icon("Panels4", "EditorIcons"));
  5099. // Update grid color by rebuilding grid.
  5100. _finish_grid();
  5101. _init_grid();
  5102. } else if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  5103. if (!is_visible() && tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->is_pressed()) {
  5104. ScriptEditorDebugger *debugger = ScriptEditor::get_singleton()->get_debugger();
  5105. debugger->set_camera_override(ScriptEditorDebugger::OVERRIDE_NONE);
  5106. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_pressed(false);
  5107. }
  5108. }
  5109. }
  5110. void SpatialEditor::add_control_to_menu_panel(Control *p_control) {
  5111. hbc_context_menu->add_child(p_control);
  5112. }
  5113. void SpatialEditor::remove_control_from_menu_panel(Control *p_control) {
  5114. hbc_context_menu->remove_child(p_control);
  5115. }
  5116. void SpatialEditor::set_can_preview(Camera *p_preview) {
  5117. for (int i = 0; i < 4; i++) {
  5118. viewports[i]->set_can_preview(p_preview);
  5119. }
  5120. }
  5121. void SpatialEditor::set_message(String p_message, float p_time) {
  5122. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  5123. SpatialEditorViewport *viewport = get_editor_viewport(i);
  5124. if (viewport->is_visible()) {
  5125. viewport->set_message(p_message, p_time);
  5126. }
  5127. }
  5128. }
  5129. VSplitContainer *SpatialEditor::get_shader_split() {
  5130. return shader_split;
  5131. }
  5132. HSplitContainer *SpatialEditor::get_palette_split() {
  5133. return palette_split;
  5134. }
  5135. void SpatialEditor::_request_gizmo(Object *p_obj) {
  5136. Spatial *sp = Object::cast_to<Spatial>(p_obj);
  5137. if (!sp) {
  5138. return;
  5139. }
  5140. if (editor->get_edited_scene() && (sp == editor->get_edited_scene() || (sp->get_owner() && editor->get_edited_scene()->is_a_parent_of(sp)))) {
  5141. Ref<EditorSpatialGizmo> seg;
  5142. for (int i = 0; i < gizmo_plugins_by_priority.size(); ++i) {
  5143. seg = gizmo_plugins_by_priority.write[i]->get_gizmo(sp);
  5144. if (seg.is_valid()) {
  5145. sp->set_gizmo(seg);
  5146. if (sp == selected) {
  5147. seg->set_selected(true);
  5148. selected->update_gizmo();
  5149. }
  5150. break;
  5151. }
  5152. }
  5153. }
  5154. }
  5155. void SpatialEditor::_toggle_maximize_view(Object *p_viewport) {
  5156. if (!p_viewport) {
  5157. return;
  5158. }
  5159. SpatialEditorViewport *current_viewport = Object::cast_to<SpatialEditorViewport>(p_viewport);
  5160. if (!current_viewport) {
  5161. return;
  5162. }
  5163. int index = -1;
  5164. bool maximized = false;
  5165. for (int i = 0; i < 4; i++) {
  5166. if (viewports[i] == current_viewport) {
  5167. index = i;
  5168. if (current_viewport->get_global_rect() == viewport_base->get_global_rect()) {
  5169. maximized = true;
  5170. }
  5171. break;
  5172. }
  5173. }
  5174. if (index == -1) {
  5175. return;
  5176. }
  5177. if (!maximized) {
  5178. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  5179. if (i == (uint32_t)index) {
  5180. viewports[i]->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  5181. } else {
  5182. viewports[i]->hide();
  5183. }
  5184. }
  5185. } else {
  5186. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  5187. viewports[i]->show();
  5188. }
  5189. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT))) {
  5190. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  5191. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS))) {
  5192. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  5193. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT))) {
  5194. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  5195. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS))) {
  5196. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  5197. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT))) {
  5198. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  5199. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS))) {
  5200. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  5201. }
  5202. }
  5203. }
  5204. void SpatialEditor::_node_removed(Node *p_node) {
  5205. if (p_node == selected) {
  5206. selected = nullptr;
  5207. }
  5208. }
  5209. void SpatialEditor::_register_all_gizmos() {
  5210. add_gizmo_plugin(Ref<CameraSpatialGizmoPlugin>(memnew(CameraSpatialGizmoPlugin)));
  5211. add_gizmo_plugin(Ref<LightSpatialGizmoPlugin>(memnew(LightSpatialGizmoPlugin)));
  5212. add_gizmo_plugin(Ref<AudioStreamPlayer3DSpatialGizmoPlugin>(memnew(AudioStreamPlayer3DSpatialGizmoPlugin)));
  5213. add_gizmo_plugin(Ref<ListenerSpatialGizmoPlugin>(memnew(ListenerSpatialGizmoPlugin)));
  5214. add_gizmo_plugin(Ref<MeshInstanceSpatialGizmoPlugin>(memnew(MeshInstanceSpatialGizmoPlugin)));
  5215. add_gizmo_plugin(Ref<SoftBodySpatialGizmoPlugin>(memnew(SoftBodySpatialGizmoPlugin)));
  5216. add_gizmo_plugin(Ref<Sprite3DSpatialGizmoPlugin>(memnew(Sprite3DSpatialGizmoPlugin)));
  5217. add_gizmo_plugin(Ref<SkeletonSpatialGizmoPlugin>(memnew(SkeletonSpatialGizmoPlugin)));
  5218. add_gizmo_plugin(Ref<Position3DSpatialGizmoPlugin>(memnew(Position3DSpatialGizmoPlugin)));
  5219. add_gizmo_plugin(Ref<RayCastSpatialGizmoPlugin>(memnew(RayCastSpatialGizmoPlugin)));
  5220. add_gizmo_plugin(Ref<SpringArmSpatialGizmoPlugin>(memnew(SpringArmSpatialGizmoPlugin)));
  5221. add_gizmo_plugin(Ref<VehicleWheelSpatialGizmoPlugin>(memnew(VehicleWheelSpatialGizmoPlugin)));
  5222. add_gizmo_plugin(Ref<VisibilityNotifierGizmoPlugin>(memnew(VisibilityNotifierGizmoPlugin)));
  5223. add_gizmo_plugin(Ref<ParticlesGizmoPlugin>(memnew(ParticlesGizmoPlugin)));
  5224. add_gizmo_plugin(Ref<CPUParticlesGizmoPlugin>(memnew(CPUParticlesGizmoPlugin)));
  5225. add_gizmo_plugin(Ref<ReflectionProbeGizmoPlugin>(memnew(ReflectionProbeGizmoPlugin)));
  5226. add_gizmo_plugin(Ref<GIProbeGizmoPlugin>(memnew(GIProbeGizmoPlugin)));
  5227. add_gizmo_plugin(Ref<BakedIndirectLightGizmoPlugin>(memnew(BakedIndirectLightGizmoPlugin)));
  5228. add_gizmo_plugin(Ref<CollisionObjectGizmoPlugin>(memnew(CollisionObjectGizmoPlugin)));
  5229. add_gizmo_plugin(Ref<CollisionShapeSpatialGizmoPlugin>(memnew(CollisionShapeSpatialGizmoPlugin)));
  5230. add_gizmo_plugin(Ref<CollisionPolygonSpatialGizmoPlugin>(memnew(CollisionPolygonSpatialGizmoPlugin)));
  5231. add_gizmo_plugin(Ref<NavigationMeshSpatialGizmoPlugin>(memnew(NavigationMeshSpatialGizmoPlugin)));
  5232. add_gizmo_plugin(Ref<JointSpatialGizmoPlugin>(memnew(JointSpatialGizmoPlugin)));
  5233. add_gizmo_plugin(Ref<PhysicalBoneSpatialGizmoPlugin>(memnew(PhysicalBoneSpatialGizmoPlugin)));
  5234. }
  5235. void SpatialEditor::_bind_methods() {
  5236. ClassDB::bind_method("_unhandled_key_input", &SpatialEditor::_unhandled_key_input);
  5237. ClassDB::bind_method("_node_removed", &SpatialEditor::_node_removed);
  5238. ClassDB::bind_method("_menu_item_pressed", &SpatialEditor::_menu_item_pressed);
  5239. ClassDB::bind_method("_menu_gizmo_toggled", &SpatialEditor::_menu_gizmo_toggled);
  5240. ClassDB::bind_method("_menu_item_toggled", &SpatialEditor::_menu_item_toggled);
  5241. ClassDB::bind_method("_xform_dialog_action", &SpatialEditor::_xform_dialog_action);
  5242. ClassDB::bind_method("_get_editor_data", &SpatialEditor::_get_editor_data);
  5243. ClassDB::bind_method("_request_gizmo", &SpatialEditor::_request_gizmo);
  5244. ClassDB::bind_method("_toggle_maximize_view", &SpatialEditor::_toggle_maximize_view);
  5245. ClassDB::bind_method("_refresh_menu_icons", &SpatialEditor::_refresh_menu_icons);
  5246. ClassDB::bind_method("_update_camera_override_button", &SpatialEditor::_update_camera_override_button);
  5247. ClassDB::bind_method("_update_camera_override_viewport", &SpatialEditor::_update_camera_override_viewport);
  5248. ClassDB::bind_method("_snap_changed", &SpatialEditor::_snap_changed);
  5249. ClassDB::bind_method("_snap_update", &SpatialEditor::_snap_update);
  5250. ADD_SIGNAL(MethodInfo("transform_key_request"));
  5251. ADD_SIGNAL(MethodInfo("item_lock_status_changed"));
  5252. ADD_SIGNAL(MethodInfo("item_group_status_changed"));
  5253. }
  5254. void SpatialEditor::clear() {
  5255. settings_fov->set_value(EDITOR_DEF("editors/3d/default_fov", 70.0));
  5256. settings_znear->set_value(EDITOR_DEF("editors/3d/default_z_near", 0.05));
  5257. settings_zfar->set_value(EDITOR_DEF("editors/3d/default_z_far", 1500.0));
  5258. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  5259. viewports[i]->reset();
  5260. }
  5261. VisualServer::get_singleton()->instance_set_visible(origin_instance, true);
  5262. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), true);
  5263. for (int i = 0; i < 3; ++i) {
  5264. if (grid_enable[i]) {
  5265. grid_visible[i] = true;
  5266. }
  5267. }
  5268. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  5269. viewports[i]->view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(SpatialEditorViewport::VIEW_AUDIO_LISTENER), i == 0);
  5270. viewports[i]->viewport->set_as_audio_listener(i == 0);
  5271. }
  5272. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID), true);
  5273. }
  5274. SpatialEditor::SpatialEditor(EditorNode *p_editor) {
  5275. gizmo.visible = true;
  5276. gizmo.scale = 1.0;
  5277. viewport_environment = Ref<Environment>(memnew(Environment));
  5278. undo_redo = p_editor->get_undo_redo();
  5279. VBoxContainer *vbc = this;
  5280. custom_camera = nullptr;
  5281. singleton = this;
  5282. editor = p_editor;
  5283. editor_selection = editor->get_editor_selection();
  5284. editor_selection->add_editor_plugin(this);
  5285. snap_enabled = false;
  5286. snap_key_enabled = false;
  5287. tool_mode = TOOL_MODE_SELECT;
  5288. camera_override_viewport_id = 0;
  5289. hbc_menu = memnew(HBoxContainer);
  5290. vbc->add_child(hbc_menu);
  5291. Vector<Variant> button_binds;
  5292. button_binds.resize(1);
  5293. String sct;
  5294. tool_button[TOOL_MODE_SELECT] = memnew(ToolButton);
  5295. hbc_menu->add_child(tool_button[TOOL_MODE_SELECT]);
  5296. tool_button[TOOL_MODE_SELECT]->set_toggle_mode(true);
  5297. tool_button[TOOL_MODE_SELECT]->set_flat(true);
  5298. tool_button[TOOL_MODE_SELECT]->set_pressed(true);
  5299. button_binds.write[0] = MENU_TOOL_SELECT;
  5300. tool_button[TOOL_MODE_SELECT]->connect("pressed", this, "_menu_item_pressed", button_binds);
  5301. tool_button[TOOL_MODE_SELECT]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_select", TTR("Select Mode"), KEY_Q));
  5302. tool_button[TOOL_MODE_SELECT]->set_tooltip(keycode_get_string(KEY_MASK_CMD) + TTR("Drag: Rotate selected node around pivot.") + "\n" + TTR("Alt+RMB: Show list of all nodes at position clicked, including locked."));
  5303. hbc_menu->add_child(memnew(VSeparator));
  5304. tool_button[TOOL_MODE_MOVE] = memnew(ToolButton);
  5305. hbc_menu->add_child(tool_button[TOOL_MODE_MOVE]);
  5306. tool_button[TOOL_MODE_MOVE]->set_toggle_mode(true);
  5307. tool_button[TOOL_MODE_MOVE]->set_flat(true);
  5308. button_binds.write[0] = MENU_TOOL_MOVE;
  5309. tool_button[TOOL_MODE_MOVE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  5310. tool_button[TOOL_MODE_MOVE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_move", TTR("Move Mode"), KEY_W));
  5311. tool_button[TOOL_MODE_ROTATE] = memnew(ToolButton);
  5312. hbc_menu->add_child(tool_button[TOOL_MODE_ROTATE]);
  5313. tool_button[TOOL_MODE_ROTATE]->set_toggle_mode(true);
  5314. tool_button[TOOL_MODE_ROTATE]->set_flat(true);
  5315. button_binds.write[0] = MENU_TOOL_ROTATE;
  5316. tool_button[TOOL_MODE_ROTATE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  5317. tool_button[TOOL_MODE_ROTATE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_rotate", TTR("Rotate Mode"), KEY_E));
  5318. tool_button[TOOL_MODE_SCALE] = memnew(ToolButton);
  5319. hbc_menu->add_child(tool_button[TOOL_MODE_SCALE]);
  5320. tool_button[TOOL_MODE_SCALE]->set_toggle_mode(true);
  5321. tool_button[TOOL_MODE_SCALE]->set_flat(true);
  5322. button_binds.write[0] = MENU_TOOL_SCALE;
  5323. tool_button[TOOL_MODE_SCALE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  5324. tool_button[TOOL_MODE_SCALE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_scale", TTR("Scale Mode"), KEY_R));
  5325. hbc_menu->add_child(memnew(VSeparator));
  5326. tool_button[TOOL_MODE_LIST_SELECT] = memnew(ToolButton);
  5327. hbc_menu->add_child(tool_button[TOOL_MODE_LIST_SELECT]);
  5328. tool_button[TOOL_MODE_LIST_SELECT]->set_toggle_mode(true);
  5329. tool_button[TOOL_MODE_LIST_SELECT]->set_flat(true);
  5330. button_binds.write[0] = MENU_TOOL_LIST_SELECT;
  5331. tool_button[TOOL_MODE_LIST_SELECT]->connect("pressed", this, "_menu_item_pressed", button_binds);
  5332. tool_button[TOOL_MODE_LIST_SELECT]->set_tooltip(TTR("Show a list of all objects at the position clicked\n(same as Alt+RMB in select mode)."));
  5333. tool_button[TOOL_LOCK_SELECTED] = memnew(ToolButton);
  5334. hbc_menu->add_child(tool_button[TOOL_LOCK_SELECTED]);
  5335. button_binds.write[0] = MENU_LOCK_SELECTED;
  5336. tool_button[TOOL_LOCK_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  5337. tool_button[TOOL_LOCK_SELECTED]->set_tooltip(TTR("Lock the selected object in place (can't be moved)."));
  5338. tool_button[TOOL_UNLOCK_SELECTED] = memnew(ToolButton);
  5339. hbc_menu->add_child(tool_button[TOOL_UNLOCK_SELECTED]);
  5340. button_binds.write[0] = MENU_UNLOCK_SELECTED;
  5341. tool_button[TOOL_UNLOCK_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  5342. tool_button[TOOL_UNLOCK_SELECTED]->set_tooltip(TTR("Unlock the selected object (can be moved)."));
  5343. tool_button[TOOL_GROUP_SELECTED] = memnew(ToolButton);
  5344. hbc_menu->add_child(tool_button[TOOL_GROUP_SELECTED]);
  5345. button_binds.write[0] = MENU_GROUP_SELECTED;
  5346. tool_button[TOOL_GROUP_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  5347. tool_button[TOOL_GROUP_SELECTED]->set_tooltip(TTR("Makes sure the object's children are not selectable."));
  5348. tool_button[TOOL_UNGROUP_SELECTED] = memnew(ToolButton);
  5349. hbc_menu->add_child(tool_button[TOOL_UNGROUP_SELECTED]);
  5350. button_binds.write[0] = MENU_UNGROUP_SELECTED;
  5351. tool_button[TOOL_UNGROUP_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  5352. tool_button[TOOL_UNGROUP_SELECTED]->set_tooltip(TTR("Restores the object's children's ability to be selected."));
  5353. hbc_menu->add_child(memnew(VSeparator));
  5354. tool_option_button[TOOL_OPT_LOCAL_COORDS] = memnew(ToolButton);
  5355. hbc_menu->add_child(tool_option_button[TOOL_OPT_LOCAL_COORDS]);
  5356. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_toggle_mode(true);
  5357. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_flat(true);
  5358. button_binds.write[0] = MENU_TOOL_LOCAL_COORDS;
  5359. tool_option_button[TOOL_OPT_LOCAL_COORDS]->connect("toggled", this, "_menu_item_toggled", button_binds);
  5360. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_shortcut(ED_SHORTCUT("spatial_editor/local_coords", TTR("Use Local Space"), KEY_T));
  5361. tool_option_button[TOOL_OPT_USE_SNAP] = memnew(ToolButton);
  5362. hbc_menu->add_child(tool_option_button[TOOL_OPT_USE_SNAP]);
  5363. tool_option_button[TOOL_OPT_USE_SNAP]->set_toggle_mode(true);
  5364. tool_option_button[TOOL_OPT_USE_SNAP]->set_flat(true);
  5365. button_binds.write[0] = MENU_TOOL_USE_SNAP;
  5366. tool_option_button[TOOL_OPT_USE_SNAP]->connect("toggled", this, "_menu_item_toggled", button_binds);
  5367. tool_option_button[TOOL_OPT_USE_SNAP]->set_shortcut(ED_SHORTCUT("spatial_editor/snap", TTR("Use Snap"), KEY_Y));
  5368. hbc_menu->add_child(memnew(VSeparator));
  5369. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA] = memnew(ToolButton);
  5370. hbc_menu->add_child(tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]);
  5371. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_toggle_mode(true);
  5372. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_flat(true);
  5373. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->set_disabled(true);
  5374. button_binds.write[0] = MENU_TOOL_OVERRIDE_CAMERA;
  5375. tool_option_button[TOOL_OPT_OVERRIDE_CAMERA]->connect("toggled", this, "_menu_item_toggled", button_binds);
  5376. _update_camera_override_button(false);
  5377. tool_button[TOOL_CONVERT_ROOMS] = memnew(ToolButton);
  5378. hbc_menu->add_child(tool_button[TOOL_CONVERT_ROOMS]);
  5379. tool_button[TOOL_CONVERT_ROOMS]->set_toggle_mode(false);
  5380. tool_button[TOOL_CONVERT_ROOMS]->set_flat(true);
  5381. button_binds.write[0] = MENU_TOOL_CONVERT_ROOMS;
  5382. tool_button[TOOL_CONVERT_ROOMS]->connect("pressed", this, "_menu_item_pressed", button_binds);
  5383. tool_button[TOOL_CONVERT_ROOMS]->set_shortcut(ED_SHORTCUT("spatial_editor/convert_rooms", TTR("Convert Rooms"), KEY_MASK_ALT | KEY_C));
  5384. tool_button[TOOL_CONVERT_ROOMS]->set_tooltip(TTR("Converts rooms for portal culling."));
  5385. hbc_menu->add_child(memnew(VSeparator));
  5386. // Drag and drop support;
  5387. preview_node = memnew(Spatial);
  5388. preview_bounds = AABB();
  5389. ED_SHORTCUT("spatial_editor/bottom_view", TTR("Bottom View"), KEY_MASK_ALT + KEY_KP_7);
  5390. ED_SHORTCUT("spatial_editor/top_view", TTR("Top View"), KEY_KP_7);
  5391. ED_SHORTCUT("spatial_editor/rear_view", TTR("Rear View"), KEY_MASK_ALT + KEY_KP_1);
  5392. ED_SHORTCUT("spatial_editor/front_view", TTR("Front View"), KEY_KP_1);
  5393. ED_SHORTCUT("spatial_editor/left_view", TTR("Left View"), KEY_MASK_ALT + KEY_KP_3);
  5394. ED_SHORTCUT("spatial_editor/right_view", TTR("Right View"), KEY_KP_3);
  5395. ED_SHORTCUT("spatial_editor/switch_perspective_orthogonal", TTR("Switch Perspective/Orthogonal View"), KEY_KP_5);
  5396. ED_SHORTCUT("spatial_editor/insert_anim_key", TTR("Insert Animation Key"), KEY_K);
  5397. ED_SHORTCUT("spatial_editor/focus_origin", TTR("Focus Origin"), KEY_O);
  5398. ED_SHORTCUT("spatial_editor/focus_selection", TTR("Focus Selection"), KEY_F);
  5399. ED_SHORTCUT("spatial_editor/align_transform_with_view", TTR("Align Transform with View"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_M);
  5400. ED_SHORTCUT("spatial_editor/align_rotation_with_view", TTR("Align Rotation with View"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_F);
  5401. ED_SHORTCUT("spatial_editor/freelook_toggle", TTR("Toggle Freelook"), KEY_MASK_SHIFT + KEY_F);
  5402. PopupMenu *p;
  5403. transform_menu = memnew(MenuButton);
  5404. transform_menu->set_text(TTR("Transform"));
  5405. transform_menu->set_switch_on_hover(true);
  5406. hbc_menu->add_child(transform_menu);
  5407. p = transform_menu->get_popup();
  5408. p->add_shortcut(ED_SHORTCUT("spatial_editor/snap_to_floor", TTR("Snap Object to Floor"), KEY_PAGEDOWN), MENU_SNAP_TO_FLOOR);
  5409. p->add_shortcut(ED_SHORTCUT("spatial_editor/transform_dialog", TTR("Transform Dialog...")), MENU_TRANSFORM_DIALOG);
  5410. p->add_separator();
  5411. p->add_shortcut(ED_SHORTCUT("spatial_editor/configure_snap", TTR("Configure Snap...")), MENU_TRANSFORM_CONFIGURE_SNAP);
  5412. p->connect("id_pressed", this, "_menu_item_pressed");
  5413. view_menu = memnew(MenuButton);
  5414. view_menu->set_text(TTR("View"));
  5415. view_menu->set_switch_on_hover(true);
  5416. hbc_menu->add_child(view_menu);
  5417. hbc_menu->add_child(memnew(VSeparator));
  5418. context_menu_container = memnew(PanelContainer);
  5419. hbc_context_menu = memnew(HBoxContainer);
  5420. context_menu_container->add_child(hbc_context_menu);
  5421. // Use a custom stylebox to make contextual menu items stand out from the rest.
  5422. // This helps with editor usability as contextual menu items change when selecting nodes,
  5423. // even though it may not be immediately obvious at first.
  5424. hbc_menu->add_child(context_menu_container);
  5425. _update_context_menu_stylebox();
  5426. // Get the view menu popup and have it stay open when a checkable item is selected
  5427. p = view_menu->get_popup();
  5428. p->set_hide_on_checkable_item_selection(false);
  5429. accept = memnew(AcceptDialog);
  5430. editor->get_gui_base()->add_child(accept);
  5431. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/1_viewport", TTR("1 Viewport"), KEY_MASK_CMD + KEY_1), MENU_VIEW_USE_1_VIEWPORT);
  5432. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports", TTR("2 Viewports"), KEY_MASK_CMD + KEY_2), MENU_VIEW_USE_2_VIEWPORTS);
  5433. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports_alt", TTR("2 Viewports (Alt)"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_2), MENU_VIEW_USE_2_VIEWPORTS_ALT);
  5434. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports", TTR("3 Viewports"), KEY_MASK_CMD + KEY_3), MENU_VIEW_USE_3_VIEWPORTS);
  5435. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports_alt", TTR("3 Viewports (Alt)"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_3), MENU_VIEW_USE_3_VIEWPORTS_ALT);
  5436. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/4_viewports", TTR("4 Viewports"), KEY_MASK_CMD + KEY_4), MENU_VIEW_USE_4_VIEWPORTS);
  5437. p->add_separator();
  5438. p->add_submenu_item(TTR("Gizmos"), "GizmosMenu");
  5439. p->add_separator();
  5440. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_origin", TTR("View Origin")), MENU_VIEW_ORIGIN);
  5441. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_grid", TTR("View Grid"), KEY_MASK_CMD + KEY_G), MENU_VIEW_GRID);
  5442. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_portal_culling", TTR("View Portal Culling"), KEY_MASK_ALT | KEY_P), MENU_VIEW_PORTAL_CULLING);
  5443. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_occlusion_culling", TTR("View Occlusion Culling")), MENU_VIEW_OCCLUSION_CULLING);
  5444. p->add_separator();
  5445. p->add_shortcut(ED_SHORTCUT("spatial_editor/settings", TTR("Settings...")), MENU_VIEW_CAMERA_SETTINGS);
  5446. p->set_item_checked(p->get_item_index(MENU_VIEW_ORIGIN), true);
  5447. p->set_item_checked(p->get_item_index(MENU_VIEW_GRID), true);
  5448. p->set_item_checked(p->get_item_index(MENU_VIEW_OCCLUSION_CULLING), true);
  5449. p->connect("id_pressed", this, "_menu_item_pressed");
  5450. gizmos_menu = memnew(PopupMenu);
  5451. p->add_child(gizmos_menu);
  5452. gizmos_menu->set_name("GizmosMenu");
  5453. gizmos_menu->set_hide_on_checkable_item_selection(false);
  5454. gizmos_menu->connect("id_pressed", this, "_menu_gizmo_toggled");
  5455. /* REST OF MENU */
  5456. palette_split = memnew(HSplitContainer);
  5457. palette_split->set_v_size_flags(SIZE_EXPAND_FILL);
  5458. vbc->add_child(palette_split);
  5459. shader_split = memnew(VSplitContainer);
  5460. shader_split->set_h_size_flags(SIZE_EXPAND_FILL);
  5461. palette_split->add_child(shader_split);
  5462. viewport_base = memnew(SpatialEditorViewportContainer);
  5463. shader_split->add_child(viewport_base);
  5464. viewport_base->set_v_size_flags(SIZE_EXPAND_FILL);
  5465. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  5466. viewports[i] = memnew(SpatialEditorViewport(this, editor, i));
  5467. viewports[i]->connect("toggle_maximize_view", this, "_toggle_maximize_view");
  5468. viewports[i]->connect("clicked", this, "_update_camera_override_viewport");
  5469. viewports[i]->assign_pending_data_pointers(preview_node, &preview_bounds, accept);
  5470. viewport_base->add_child(viewports[i]);
  5471. }
  5472. /* SNAP DIALOG */
  5473. snap_translate_value = 1;
  5474. snap_rotate_value = 15;
  5475. snap_scale_value = 10;
  5476. snap_dialog = memnew(ConfirmationDialog);
  5477. snap_dialog->set_title(TTR("Snap Settings"));
  5478. add_child(snap_dialog);
  5479. snap_dialog->connect("confirmed", this, "_snap_changed");
  5480. snap_dialog->get_cancel()->connect("pressed", this, "_snap_update");
  5481. VBoxContainer *snap_dialog_vbc = memnew(VBoxContainer);
  5482. snap_dialog->add_child(snap_dialog_vbc);
  5483. snap_translate = memnew(LineEdit);
  5484. snap_dialog_vbc->add_margin_child(TTR("Translate Snap:"), snap_translate);
  5485. snap_rotate = memnew(LineEdit);
  5486. snap_dialog_vbc->add_margin_child(TTR("Rotate Snap (deg.):"), snap_rotate);
  5487. snap_scale = memnew(LineEdit);
  5488. snap_dialog_vbc->add_margin_child(TTR("Scale Snap (%):"), snap_scale);
  5489. _snap_update();
  5490. /* SETTINGS DIALOG */
  5491. settings_dialog = memnew(ConfirmationDialog);
  5492. settings_dialog->set_title(TTR("Viewport Settings"));
  5493. add_child(settings_dialog);
  5494. settings_vbc = memnew(VBoxContainer);
  5495. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  5496. settings_dialog->add_child(settings_vbc);
  5497. settings_fov = memnew(SpinBox);
  5498. settings_fov->set_max(MAX_FOV);
  5499. settings_fov->set_min(MIN_FOV);
  5500. settings_fov->set_step(0.01);
  5501. settings_fov->set_value(EDITOR_DEF("editors/3d/default_fov", 70.0));
  5502. settings_vbc->add_margin_child(TTR("Perspective FOV (deg.):"), settings_fov);
  5503. settings_znear = memnew(SpinBox);
  5504. settings_znear->set_max(MAX_Z);
  5505. settings_znear->set_min(MIN_Z);
  5506. settings_znear->set_step(0.01);
  5507. settings_znear->set_value(EDITOR_DEF("editors/3d/default_z_near", 0.05));
  5508. settings_vbc->add_margin_child(TTR("View Z-Near:"), settings_znear);
  5509. settings_zfar = memnew(SpinBox);
  5510. settings_zfar->set_max(MAX_Z);
  5511. settings_zfar->set_min(MIN_Z);
  5512. settings_zfar->set_step(0.01);
  5513. settings_zfar->set_value(EDITOR_DEF("editors/3d/default_z_far", 1500));
  5514. settings_vbc->add_margin_child(TTR("View Z-Far:"), settings_zfar);
  5515. for (uint32_t i = 0; i < VIEWPORTS_COUNT; ++i) {
  5516. settings_dialog->connect("confirmed", viewports[i], "_update_camera", varray(0.0));
  5517. }
  5518. /* XFORM DIALOG */
  5519. xform_dialog = memnew(ConfirmationDialog);
  5520. xform_dialog->set_title(TTR("Transform Change"));
  5521. add_child(xform_dialog);
  5522. VBoxContainer *xform_vbc = memnew(VBoxContainer);
  5523. xform_dialog->add_child(xform_vbc);
  5524. Label *l = memnew(Label);
  5525. l->set_text(TTR("Translate:"));
  5526. xform_vbc->add_child(l);
  5527. HBoxContainer *xform_hbc = memnew(HBoxContainer);
  5528. xform_vbc->add_child(xform_hbc);
  5529. for (int i = 0; i < 3; i++) {
  5530. xform_translate[i] = memnew(LineEdit);
  5531. xform_translate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  5532. xform_hbc->add_child(xform_translate[i]);
  5533. }
  5534. l = memnew(Label);
  5535. l->set_text(TTR("Rotate (deg.):"));
  5536. xform_vbc->add_child(l);
  5537. xform_hbc = memnew(HBoxContainer);
  5538. xform_vbc->add_child(xform_hbc);
  5539. for (int i = 0; i < 3; i++) {
  5540. xform_rotate[i] = memnew(LineEdit);
  5541. xform_rotate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  5542. xform_hbc->add_child(xform_rotate[i]);
  5543. }
  5544. l = memnew(Label);
  5545. l->set_text(TTR("Scale (ratio):"));
  5546. xform_vbc->add_child(l);
  5547. xform_hbc = memnew(HBoxContainer);
  5548. xform_vbc->add_child(xform_hbc);
  5549. for (int i = 0; i < 3; i++) {
  5550. xform_scale[i] = memnew(LineEdit);
  5551. xform_scale[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  5552. xform_hbc->add_child(xform_scale[i]);
  5553. }
  5554. l = memnew(Label);
  5555. l->set_text(TTR("Transform Type"));
  5556. xform_vbc->add_child(l);
  5557. xform_type = memnew(OptionButton);
  5558. xform_type->set_h_size_flags(SIZE_EXPAND_FILL);
  5559. xform_type->add_item(TTR("Pre"));
  5560. xform_type->add_item(TTR("Post"));
  5561. xform_vbc->add_child(xform_type);
  5562. xform_dialog->connect("confirmed", this, "_xform_dialog_action");
  5563. scenario_debug = VisualServer::SCENARIO_DEBUG_DISABLED;
  5564. selected = nullptr;
  5565. set_process_unhandled_key_input(true);
  5566. add_to_group("_spatial_editor_group");
  5567. EDITOR_DEF("editors/3d/manipulator_gizmo_size", 80);
  5568. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/3d/manipulator_gizmo_size", PROPERTY_HINT_RANGE, "16,160,1"));
  5569. EDITOR_DEF("editors/3d/manipulator_gizmo_opacity", 0.9);
  5570. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::REAL, "editors/3d/manipulator_gizmo_opacity", PROPERTY_HINT_RANGE, "0,1,0.01"));
  5571. EDITOR_DEF("editors/3d/navigation/show_viewport_rotation_gizmo", true);
  5572. over_gizmo_handle = -1;
  5573. // make sure the portal tools are off by default
  5574. // (when no RoomManager is present)
  5575. update_portal_tools();
  5576. }
  5577. SpatialEditor::~SpatialEditor() {
  5578. memdelete(preview_node);
  5579. }
  5580. void SpatialEditorPlugin::make_visible(bool p_visible) {
  5581. if (p_visible) {
  5582. spatial_editor->show();
  5583. spatial_editor->set_process(true);
  5584. } else {
  5585. spatial_editor->hide();
  5586. spatial_editor->set_process(false);
  5587. }
  5588. }
  5589. void SpatialEditorPlugin::edit(Object *p_object) {
  5590. spatial_editor->edit(Object::cast_to<Spatial>(p_object));
  5591. }
  5592. bool SpatialEditorPlugin::handles(Object *p_object) const {
  5593. return p_object->is_class("Spatial");
  5594. }
  5595. Dictionary SpatialEditorPlugin::get_state() const {
  5596. return spatial_editor->get_state();
  5597. }
  5598. void SpatialEditorPlugin::set_state(const Dictionary &p_state) {
  5599. spatial_editor->set_state(p_state);
  5600. }
  5601. void SpatialEditor::snap_cursor_to_plane(const Plane &p_plane) {
  5602. //cursor.pos=p_plane.project(cursor.pos);
  5603. }
  5604. Vector3 SpatialEditor::snap_point(Vector3 p_target, Vector3 p_start) const {
  5605. if (is_snap_enabled()) {
  5606. p_target.x = Math::snap_scalar(0.0, get_translate_snap(), p_target.x);
  5607. p_target.y = Math::snap_scalar(0.0, get_translate_snap(), p_target.y);
  5608. p_target.z = Math::snap_scalar(0.0, get_translate_snap(), p_target.z);
  5609. }
  5610. return p_target;
  5611. }
  5612. float SpatialEditor::get_translate_snap() const {
  5613. float snap_value;
  5614. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  5615. snap_value = snap_translate->get_text().to_double() / 10.0;
  5616. } else {
  5617. snap_value = snap_translate->get_text().to_double();
  5618. }
  5619. return snap_value;
  5620. }
  5621. float SpatialEditor::get_rotate_snap() const {
  5622. float snap_value;
  5623. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  5624. snap_value = snap_rotate->get_text().to_double() / 3.0;
  5625. } else {
  5626. snap_value = snap_rotate->get_text().to_double();
  5627. }
  5628. return snap_value;
  5629. }
  5630. float SpatialEditor::get_scale_snap() const {
  5631. float snap_value;
  5632. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  5633. snap_value = snap_scale->get_text().to_double() / 2.0;
  5634. } else {
  5635. snap_value = snap_scale->get_text().to_double();
  5636. }
  5637. return snap_value;
  5638. }
  5639. void SpatialEditorPlugin::_bind_methods() {
  5640. ClassDB::bind_method("snap_cursor_to_plane", &SpatialEditorPlugin::snap_cursor_to_plane);
  5641. }
  5642. void SpatialEditorPlugin::snap_cursor_to_plane(const Plane &p_plane) {
  5643. spatial_editor->snap_cursor_to_plane(p_plane);
  5644. }
  5645. struct _GizmoPluginPriorityComparator {
  5646. bool operator()(const Ref<EditorSpatialGizmoPlugin> &p_a, const Ref<EditorSpatialGizmoPlugin> &p_b) const {
  5647. if (p_a->get_priority() == p_b->get_priority()) {
  5648. return p_a->get_name() < p_b->get_name();
  5649. }
  5650. return p_a->get_priority() > p_b->get_priority();
  5651. }
  5652. };
  5653. struct _GizmoPluginNameComparator {
  5654. bool operator()(const Ref<EditorSpatialGizmoPlugin> &p_a, const Ref<EditorSpatialGizmoPlugin> &p_b) const {
  5655. return p_a->get_name() < p_b->get_name();
  5656. }
  5657. };
  5658. void SpatialEditor::add_gizmo_plugin(Ref<EditorSpatialGizmoPlugin> p_plugin) {
  5659. ERR_FAIL_NULL(p_plugin.ptr());
  5660. gizmo_plugins_by_priority.push_back(p_plugin);
  5661. gizmo_plugins_by_priority.sort_custom<_GizmoPluginPriorityComparator>();
  5662. gizmo_plugins_by_name.push_back(p_plugin);
  5663. gizmo_plugins_by_name.sort_custom<_GizmoPluginNameComparator>();
  5664. _update_gizmos_menu();
  5665. SpatialEditor::get_singleton()->update_all_gizmos();
  5666. }
  5667. void SpatialEditor::remove_gizmo_plugin(Ref<EditorSpatialGizmoPlugin> p_plugin) {
  5668. gizmo_plugins_by_priority.erase(p_plugin);
  5669. gizmo_plugins_by_name.erase(p_plugin);
  5670. _update_gizmos_menu();
  5671. }
  5672. SpatialEditorPlugin::SpatialEditorPlugin(EditorNode *p_node) {
  5673. editor = p_node;
  5674. spatial_editor = memnew(SpatialEditor(p_node));
  5675. spatial_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  5676. editor->get_viewport()->add_child(spatial_editor);
  5677. spatial_editor->hide();
  5678. spatial_editor->connect("transform_key_request", editor->get_inspector_dock(), "_transform_keyed");
  5679. }
  5680. SpatialEditorPlugin::~SpatialEditorPlugin() {
  5681. }
  5682. void EditorSpatialGizmoPlugin::create_material(const String &p_name, const Color &p_color, bool p_billboard, bool p_on_top, bool p_use_vertex_color) {
  5683. Color instanced_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/instanced", Color(0.7, 0.7, 0.7, 0.6));
  5684. Vector<Ref<SpatialMaterial>> mats;
  5685. for (int i = 0; i < 4; i++) {
  5686. bool selected = i % 2 == 1;
  5687. bool instanced = i < 2;
  5688. Ref<SpatialMaterial> material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  5689. Color color = instanced ? instanced_color : p_color;
  5690. if (!selected) {
  5691. color.a *= 0.3;
  5692. }
  5693. material->set_albedo(color);
  5694. material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  5695. material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  5696. material->set_render_priority(SpatialMaterial::RENDER_PRIORITY_MIN + 1);
  5697. if (p_use_vertex_color) {
  5698. material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  5699. material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  5700. }
  5701. if (p_billboard) {
  5702. material->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
  5703. }
  5704. if (p_on_top && selected) {
  5705. material->set_on_top_of_alpha();
  5706. }
  5707. mats.push_back(material);
  5708. }
  5709. materials[p_name] = mats;
  5710. }
  5711. void EditorSpatialGizmoPlugin::create_icon_material(const String &p_name, const Ref<Texture> &p_texture, bool p_on_top, const Color &p_albedo) {
  5712. Color instanced_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/instanced", Color(0.7, 0.7, 0.7, 0.6));
  5713. Vector<Ref<SpatialMaterial>> icons;
  5714. for (int i = 0; i < 4; i++) {
  5715. bool selected = i % 2 == 1;
  5716. bool instanced = i < 2;
  5717. Ref<SpatialMaterial> icon = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  5718. Color color = instanced ? instanced_color : p_albedo;
  5719. if (!selected) {
  5720. color.a *= 0.85;
  5721. }
  5722. icon->set_albedo(color);
  5723. icon->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  5724. icon->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  5725. icon->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  5726. icon->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  5727. icon->set_depth_draw_mode(SpatialMaterial::DEPTH_DRAW_DISABLED);
  5728. icon->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  5729. icon->set_texture(SpatialMaterial::TEXTURE_ALBEDO, p_texture);
  5730. icon->set_flag(SpatialMaterial::FLAG_FIXED_SIZE, true);
  5731. icon->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
  5732. icon->set_render_priority(SpatialMaterial::RENDER_PRIORITY_MIN);
  5733. if (p_on_top && selected) {
  5734. icon->set_on_top_of_alpha();
  5735. }
  5736. icons.push_back(icon);
  5737. }
  5738. materials[p_name] = icons;
  5739. }
  5740. void EditorSpatialGizmoPlugin::create_handle_material(const String &p_name, bool p_billboard, const Ref<Texture> &p_icon) {
  5741. Ref<SpatialMaterial> handle_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  5742. handle_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  5743. handle_material->set_flag(SpatialMaterial::FLAG_USE_POINT_SIZE, true);
  5744. Ref<Texture> handle_t = p_icon != nullptr ? p_icon : SpatialEditor::get_singleton()->get_icon("Editor3DHandle", "EditorIcons");
  5745. handle_material->set_point_size(handle_t->get_width());
  5746. handle_material->set_texture(SpatialMaterial::TEXTURE_ALBEDO, handle_t);
  5747. handle_material->set_albedo(Color(1, 1, 1));
  5748. handle_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  5749. handle_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  5750. handle_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  5751. handle_material->set_on_top_of_alpha();
  5752. if (p_billboard) {
  5753. handle_material->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
  5754. handle_material->set_on_top_of_alpha();
  5755. }
  5756. materials[p_name] = Vector<Ref<SpatialMaterial>>();
  5757. materials[p_name].push_back(handle_material);
  5758. }
  5759. void EditorSpatialGizmoPlugin::add_material(const String &p_name, Ref<SpatialMaterial> p_material) {
  5760. materials[p_name] = Vector<Ref<SpatialMaterial>>();
  5761. materials[p_name].push_back(p_material);
  5762. }
  5763. Ref<SpatialMaterial> EditorSpatialGizmoPlugin::get_material(const String &p_name, const Ref<EditorSpatialGizmo> &p_gizmo) {
  5764. ERR_FAIL_COND_V(!materials.has(p_name), Ref<SpatialMaterial>());
  5765. ERR_FAIL_COND_V(materials[p_name].size() == 0, Ref<SpatialMaterial>());
  5766. if (p_gizmo.is_null() || materials[p_name].size() == 1) {
  5767. return materials[p_name][0];
  5768. }
  5769. int index = (p_gizmo->is_selected() ? 1 : 0) + (p_gizmo->is_editable() ? 2 : 0);
  5770. Ref<SpatialMaterial> mat = materials[p_name][index];
  5771. if (current_state == ON_TOP && p_gizmo->is_selected()) {
  5772. mat->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, true);
  5773. } else {
  5774. mat->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, false);
  5775. }
  5776. return mat;
  5777. }
  5778. String EditorSpatialGizmoPlugin::get_name() const {
  5779. if (get_script_instance() && get_script_instance()->has_method("get_name")) {
  5780. return get_script_instance()->call("get_name");
  5781. }
  5782. WARN_PRINT_ONCE("A 3D editor gizmo has no name defined (it will appear as \"Unnamed Gizmo\" in the \"View > Gizmos\" menu). To resolve this, override the `get_name()` function to return a String in the script that extends EditorSpatialGizmoPlugin.");
  5783. return TTR("Unnamed Gizmo");
  5784. }
  5785. int EditorSpatialGizmoPlugin::get_priority() const {
  5786. if (get_script_instance() && get_script_instance()->has_method("get_priority")) {
  5787. return get_script_instance()->call("get_priority");
  5788. }
  5789. return 0;
  5790. }
  5791. Ref<EditorSpatialGizmo> EditorSpatialGizmoPlugin::get_gizmo(Spatial *p_spatial) {
  5792. if (get_script_instance() && get_script_instance()->has_method("get_gizmo")) {
  5793. return get_script_instance()->call("get_gizmo", p_spatial);
  5794. }
  5795. Ref<EditorSpatialGizmo> ref = create_gizmo(p_spatial);
  5796. if (ref.is_null()) {
  5797. return ref;
  5798. }
  5799. ref->set_plugin(this);
  5800. ref->set_spatial_node(p_spatial);
  5801. ref->set_hidden(current_state == HIDDEN);
  5802. current_gizmos.push_back(ref.ptr());
  5803. return ref;
  5804. }
  5805. void EditorSpatialGizmoPlugin::_bind_methods() {
  5806. #define GIZMO_REF PropertyInfo(Variant::OBJECT, "gizmo", PROPERTY_HINT_RESOURCE_TYPE, "EditorSpatialGizmo")
  5807. BIND_VMETHOD(MethodInfo(Variant::BOOL, "has_gizmo", PropertyInfo(Variant::OBJECT, "spatial", PROPERTY_HINT_RESOURCE_TYPE, "Spatial")));
  5808. BIND_VMETHOD(MethodInfo(GIZMO_REF, "create_gizmo", PropertyInfo(Variant::OBJECT, "spatial", PROPERTY_HINT_RESOURCE_TYPE, "Spatial")));
  5809. ClassDB::bind_method(D_METHOD("create_material", "name", "color", "billboard", "on_top", "use_vertex_color"), &EditorSpatialGizmoPlugin::create_material, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  5810. ClassDB::bind_method(D_METHOD("create_icon_material", "name", "texture", "on_top", "color"), &EditorSpatialGizmoPlugin::create_icon_material, DEFVAL(false), DEFVAL(Color(1, 1, 1, 1)));
  5811. ClassDB::bind_method(D_METHOD("create_handle_material", "name", "billboard", "texture"), &EditorSpatialGizmoPlugin::create_handle_material, DEFVAL(false), DEFVAL(Variant()));
  5812. ClassDB::bind_method(D_METHOD("add_material", "name", "material"), &EditorSpatialGizmoPlugin::add_material);
  5813. ClassDB::bind_method(D_METHOD("get_material", "name", "gizmo"), &EditorSpatialGizmoPlugin::get_material, DEFVAL(Ref<EditorSpatialGizmo>()));
  5814. BIND_VMETHOD(MethodInfo(Variant::STRING, "get_name"));
  5815. BIND_VMETHOD(MethodInfo(Variant::INT, "get_priority"));
  5816. BIND_VMETHOD(MethodInfo(Variant::BOOL, "can_be_hidden"));
  5817. BIND_VMETHOD(MethodInfo(Variant::BOOL, "is_selectable_when_hidden"));
  5818. BIND_VMETHOD(MethodInfo("redraw", GIZMO_REF));
  5819. BIND_VMETHOD(MethodInfo(Variant::STRING, "get_handle_name", GIZMO_REF, PropertyInfo(Variant::INT, "index")));
  5820. MethodInfo hvget(Variant::NIL, "get_handle_value", GIZMO_REF, PropertyInfo(Variant::INT, "index"));
  5821. hvget.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  5822. BIND_VMETHOD(hvget);
  5823. BIND_VMETHOD(MethodInfo("set_handle", GIZMO_REF, PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera"), PropertyInfo(Variant::VECTOR2, "point")));
  5824. MethodInfo cm = MethodInfo("commit_handle", GIZMO_REF, PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::NIL, "restore"), PropertyInfo(Variant::BOOL, "cancel"));
  5825. cm.default_arguments.push_back(false);
  5826. BIND_VMETHOD(cm);
  5827. BIND_VMETHOD(MethodInfo(Variant::BOOL, "is_handle_highlighted", GIZMO_REF, PropertyInfo(Variant::INT, "index")));
  5828. #undef GIZMO_REF
  5829. }
  5830. bool EditorSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  5831. if (get_script_instance() && get_script_instance()->has_method("has_gizmo")) {
  5832. return get_script_instance()->call("has_gizmo", p_spatial);
  5833. }
  5834. return false;
  5835. }
  5836. Ref<EditorSpatialGizmo> EditorSpatialGizmoPlugin::create_gizmo(Spatial *p_spatial) {
  5837. if (get_script_instance() && get_script_instance()->has_method("create_gizmo")) {
  5838. return get_script_instance()->call("create_gizmo", p_spatial);
  5839. }
  5840. Ref<EditorSpatialGizmo> ref;
  5841. if (has_gizmo(p_spatial)) {
  5842. ref.instance();
  5843. }
  5844. return ref;
  5845. }
  5846. bool EditorSpatialGizmoPlugin::can_be_hidden() const {
  5847. if (get_script_instance() && get_script_instance()->has_method("can_be_hidden")) {
  5848. return get_script_instance()->call("can_be_hidden");
  5849. }
  5850. return true;
  5851. }
  5852. bool EditorSpatialGizmoPlugin::is_selectable_when_hidden() const {
  5853. if (get_script_instance() && get_script_instance()->has_method("is_selectable_when_hidden")) {
  5854. return get_script_instance()->call("is_selectable_when_hidden");
  5855. }
  5856. return false;
  5857. }
  5858. void EditorSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  5859. if (get_script_instance() && get_script_instance()->has_method("redraw")) {
  5860. Ref<EditorSpatialGizmo> ref(p_gizmo);
  5861. get_script_instance()->call("redraw", ref);
  5862. }
  5863. }
  5864. String EditorSpatialGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  5865. if (get_script_instance() && get_script_instance()->has_method("get_handle_name")) {
  5866. return get_script_instance()->call("get_handle_name", p_gizmo, p_idx);
  5867. }
  5868. return "";
  5869. }
  5870. Variant EditorSpatialGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  5871. if (get_script_instance() && get_script_instance()->has_method("get_handle_value")) {
  5872. return get_script_instance()->call("get_handle_value", p_gizmo, p_idx);
  5873. }
  5874. return Variant();
  5875. }
  5876. void EditorSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  5877. if (get_script_instance() && get_script_instance()->has_method("set_handle")) {
  5878. get_script_instance()->call("set_handle", p_gizmo, p_idx, p_camera, p_point);
  5879. }
  5880. }
  5881. void EditorSpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  5882. if (get_script_instance() && get_script_instance()->has_method("commit_handle")) {
  5883. get_script_instance()->call("commit_handle", p_gizmo, p_idx, p_restore, p_cancel);
  5884. }
  5885. }
  5886. bool EditorSpatialGizmoPlugin::is_handle_highlighted(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  5887. if (get_script_instance() && get_script_instance()->has_method("is_handle_highlighted")) {
  5888. return get_script_instance()->call("is_handle_highlighted", p_gizmo, p_idx);
  5889. }
  5890. return false;
  5891. }
  5892. void EditorSpatialGizmoPlugin::set_state(int p_state) {
  5893. current_state = p_state;
  5894. for (int i = 0; i < current_gizmos.size(); ++i) {
  5895. current_gizmos[i]->set_hidden(current_state == HIDDEN);
  5896. }
  5897. }
  5898. int EditorSpatialGizmoPlugin::get_state() const {
  5899. return current_state;
  5900. }
  5901. void EditorSpatialGizmoPlugin::unregister_gizmo(EditorSpatialGizmo *p_gizmo) {
  5902. current_gizmos.erase(p_gizmo);
  5903. }
  5904. EditorSpatialGizmoPlugin::EditorSpatialGizmoPlugin() {
  5905. current_state = VISIBLE;
  5906. }
  5907. EditorSpatialGizmoPlugin::~EditorSpatialGizmoPlugin() {
  5908. for (int i = 0; i < current_gizmos.size(); ++i) {
  5909. current_gizmos[i]->set_plugin(nullptr);
  5910. current_gizmos[i]->get_spatial_node()->set_gizmo(nullptr);
  5911. }
  5912. if (SpatialEditor::get_singleton()) {
  5913. SpatialEditor::get_singleton()->update_all_gizmos();
  5914. }
  5915. }