spatial_editor_plugin.cpp 212 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282
  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-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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_settings.h"
  39. #include "editor/plugins/animation_player_editor_plugin.h"
  40. #include "editor/plugins/script_editor_plugin.h"
  41. #include "editor/script_editor_debugger.h"
  42. #include "editor/spatial_editor_gizmos.h"
  43. #include "scene/3d/camera.h"
  44. #include "scene/3d/collision_shape.h"
  45. #include "scene/3d/mesh_instance.h"
  46. #include "scene/3d/physics_body.h"
  47. #include "scene/3d/visual_instance.h"
  48. #include "scene/resources/packed_scene.h"
  49. #include "scene/resources/surface_tool.h"
  50. #define DISTANCE_DEFAULT 4
  51. #define GIZMO_ARROW_SIZE 0.35
  52. #define GIZMO_RING_HALF_WIDTH 0.1
  53. #define GIZMO_SCALE_DEFAULT 0.15
  54. #define GIZMO_PLANE_SIZE 0.2
  55. #define GIZMO_PLANE_DST 0.3
  56. #define GIZMO_CIRCLE_SIZE 1.1
  57. #define GIZMO_SCALE_OFFSET (GIZMO_CIRCLE_SIZE + 0.3)
  58. #define GIZMO_ARROW_OFFSET (GIZMO_CIRCLE_SIZE + 0.3)
  59. #define ZOOM_MIN_DISTANCE 0.001
  60. #define ZOOM_MULTIPLIER 1.08
  61. #define ZOOM_INDICATOR_DELAY_S 1.5
  62. #define FREELOOK_MIN_SPEED 0.01
  63. #define FREELOOK_SPEED_MULTIPLIER 1.08
  64. #define MIN_Z 0.01
  65. #define MAX_Z 1000000.0
  66. #define MIN_FOV 0.01
  67. #define MAX_FOV 179
  68. #ifdef TOOLS_ENABLED
  69. #define get_global_gizmo_transform get_global_gizmo_transform
  70. #define get_local_gizmo_transform get_local_gizmo_transform
  71. #else
  72. #define get_global_gizmo_transform get_global_transform
  73. #define get_local_gizmo_transform get_transform
  74. #endif
  75. void SpatialEditorViewport::_update_camera(float p_interp_delta) {
  76. bool is_orthogonal = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL;
  77. Cursor old_camera_cursor = camera_cursor;
  78. camera_cursor = cursor;
  79. if (p_interp_delta > 0) {
  80. //-------
  81. // Perform smoothing
  82. if (is_freelook_active()) {
  83. // Higher inertia should increase "lag" (lerp with factor between 0 and 1)
  84. // 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.
  85. real_t inertia = EDITOR_GET("editors/3d/freelook/freelook_inertia");
  86. inertia = MAX(0.001, inertia);
  87. real_t factor = (1.0 / inertia) * p_interp_delta;
  88. // We interpolate a different point here, because in freelook mode the focus point (cursor.pos) orbits around eye_pos
  89. camera_cursor.eye_pos = old_camera_cursor.eye_pos.linear_interpolate(cursor.eye_pos, CLAMP(factor, 0, 1));
  90. float orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  91. orbit_inertia = MAX(0.0001, orbit_inertia);
  92. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  93. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  94. Vector3 forward = to_camera_transform(camera_cursor).basis.xform(Vector3(0, 0, -1));
  95. camera_cursor.pos = camera_cursor.eye_pos + forward * camera_cursor.distance;
  96. } else {
  97. //when not being manipulated, move softly
  98. float free_orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  99. float free_translation_inertia = EDITOR_GET("editors/3d/navigation_feel/translation_inertia");
  100. //when being manipulated, move more quickly
  101. float manip_orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/manipulation_orbit_inertia");
  102. float manip_translation_inertia = EDITOR_GET("editors/3d/navigation_feel/manipulation_translation_inertia");
  103. float zoom_inertia = EDITOR_GET("editors/3d/navigation_feel/zoom_inertia");
  104. //determine if being manipulated
  105. bool manipulated = Input::get_singleton()->get_mouse_button_mask() & (2 | 4);
  106. manipulated |= Input::get_singleton()->is_key_pressed(KEY_SHIFT);
  107. manipulated |= Input::get_singleton()->is_key_pressed(KEY_ALT);
  108. manipulated |= Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  109. float orbit_inertia = MAX(0.00001, manipulated ? manip_orbit_inertia : free_orbit_inertia);
  110. float translation_inertia = MAX(0.0001, manipulated ? manip_translation_inertia : free_translation_inertia);
  111. zoom_inertia = MAX(0.0001, zoom_inertia);
  112. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  113. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  114. camera_cursor.pos = old_camera_cursor.pos.linear_interpolate(cursor.pos, MIN(1.f, p_interp_delta * (1 / translation_inertia)));
  115. camera_cursor.distance = Math::lerp(old_camera_cursor.distance, cursor.distance, MIN(1.f, p_interp_delta * (1 / zoom_inertia)));
  116. }
  117. }
  118. //-------
  119. // Apply camera transform
  120. float tolerance = 0.001;
  121. bool equal = true;
  122. if (Math::abs(old_camera_cursor.x_rot - camera_cursor.x_rot) > tolerance || Math::abs(old_camera_cursor.y_rot - camera_cursor.y_rot) > tolerance) {
  123. equal = false;
  124. }
  125. if (equal && old_camera_cursor.pos.distance_squared_to(camera_cursor.pos) > tolerance * tolerance) {
  126. equal = false;
  127. }
  128. if (equal && Math::abs(old_camera_cursor.distance - camera_cursor.distance) > tolerance) {
  129. equal = false;
  130. }
  131. if (!equal || p_interp_delta == 0 || is_freelook_active() || is_orthogonal != orthogonal) {
  132. camera->set_global_transform(to_camera_transform(camera_cursor));
  133. if (orthogonal)
  134. camera->set_orthogonal(2 * cursor.distance, 0.1, 8192);
  135. else
  136. camera->set_perspective(get_fov(), get_znear(), get_zfar());
  137. update_transform_gizmo_view();
  138. }
  139. }
  140. Transform SpatialEditorViewport::to_camera_transform(const Cursor &p_cursor) const {
  141. Transform camera_transform;
  142. camera_transform.translate(p_cursor.pos);
  143. camera_transform.basis.rotate(Vector3(1, 0, 0), -p_cursor.x_rot);
  144. camera_transform.basis.rotate(Vector3(0, 1, 0), -p_cursor.y_rot);
  145. if (orthogonal)
  146. camera_transform.translate(0, 0, 4096);
  147. else
  148. camera_transform.translate(0, 0, p_cursor.distance);
  149. return camera_transform;
  150. }
  151. int SpatialEditorViewport::get_selected_count() const {
  152. Map<Node *, Object *> &selection = editor_selection->get_selection();
  153. int count = 0;
  154. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  155. Spatial *sp = Object::cast_to<Spatial>(E->key());
  156. if (!sp)
  157. continue;
  158. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  159. if (!se)
  160. continue;
  161. count++;
  162. }
  163. return count;
  164. }
  165. float SpatialEditorViewport::get_znear() const {
  166. return CLAMP(spatial_editor->get_znear(), MIN_Z, MAX_Z);
  167. }
  168. float SpatialEditorViewport::get_zfar() const {
  169. return CLAMP(spatial_editor->get_zfar(), MIN_Z, MAX_Z);
  170. }
  171. float SpatialEditorViewport::get_fov() const {
  172. return CLAMP(spatial_editor->get_fov(), MIN_FOV, MAX_FOV);
  173. }
  174. Transform SpatialEditorViewport::_get_camera_transform() const {
  175. return camera->get_global_transform();
  176. }
  177. Vector3 SpatialEditorViewport::_get_camera_position() const {
  178. return _get_camera_transform().origin;
  179. }
  180. Point2 SpatialEditorViewport::_point_to_screen(const Vector3 &p_point) {
  181. return camera->unproject_position(p_point) * viewport_container->get_stretch_shrink();
  182. }
  183. Vector3 SpatialEditorViewport::_get_ray_pos(const Vector2 &p_pos) const {
  184. return camera->project_ray_origin(p_pos / viewport_container->get_stretch_shrink());
  185. }
  186. Vector3 SpatialEditorViewport::_get_camera_normal() const {
  187. return -_get_camera_transform().basis.get_axis(2);
  188. }
  189. Vector3 SpatialEditorViewport::_get_ray(const Vector2 &p_pos) const {
  190. return camera->project_ray_normal(p_pos / viewport_container->get_stretch_shrink());
  191. }
  192. void SpatialEditorViewport::_clear_selected() {
  193. editor_selection->clear();
  194. }
  195. void SpatialEditorViewport::_select_clicked(bool p_append, bool p_single) {
  196. if (!clicked)
  197. return;
  198. Node *node = Object::cast_to<Node>(ObjectDB::get_instance(clicked));
  199. Spatial *selected = Object::cast_to<Spatial>(node);
  200. if (!selected)
  201. return;
  202. // Replace the node by the group if grouped
  203. while (node && node != editor->get_edited_scene()->get_parent()) {
  204. Spatial *selected_tmp = Object::cast_to<Spatial>(node);
  205. if (selected_tmp && node->has_meta("_edit_group_")) {
  206. selected = selected_tmp;
  207. }
  208. node = node->get_parent();
  209. }
  210. if (!_is_node_locked(selected))
  211. _select(selected, clicked_wants_append, true);
  212. }
  213. void SpatialEditorViewport::_select(Node *p_node, bool p_append, bool p_single) {
  214. if (!p_append) {
  215. editor_selection->clear();
  216. }
  217. if (editor_selection->is_selected(p_node)) {
  218. //erase
  219. editor_selection->remove_node(p_node);
  220. } else {
  221. editor_selection->add_node(p_node);
  222. }
  223. if (p_single) {
  224. if (Engine::get_singleton()->is_editor_hint())
  225. editor->call("edit_node", p_node);
  226. }
  227. }
  228. ObjectID SpatialEditorViewport::_select_ray(const Point2 &p_pos, bool p_append, bool &r_includes_current, int *r_gizmo_handle, bool p_alt_select) {
  229. if (r_gizmo_handle)
  230. *r_gizmo_handle = -1;
  231. Vector3 ray = _get_ray(p_pos);
  232. Vector3 pos = _get_ray_pos(p_pos);
  233. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(pos, ray, get_tree()->get_root()->get_world()->get_scenario());
  234. Set<Ref<EditorSpatialGizmo> > found_gizmos;
  235. Node *edited_scene = get_tree()->get_edited_scene_root();
  236. ObjectID closest = 0;
  237. Node *item = NULL;
  238. float closest_dist = 1e20;
  239. int selected_handle = -1;
  240. for (int i = 0; i < instances.size(); i++) {
  241. Spatial *spat = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  242. if (!spat)
  243. continue;
  244. Ref<EditorSpatialGizmo> seg = spat->get_gizmo();
  245. if ((!seg.is_valid()) || found_gizmos.has(seg)) {
  246. continue;
  247. }
  248. found_gizmos.insert(seg);
  249. Vector3 point;
  250. Vector3 normal;
  251. int handle = -1;
  252. bool inters = seg->intersect_ray(camera, p_pos, point, normal, &handle, p_alt_select);
  253. if (!inters)
  254. continue;
  255. float dist = pos.distance_to(point);
  256. if (dist < 0)
  257. continue;
  258. if (dist < closest_dist) {
  259. item = Object::cast_to<Node>(spat);
  260. while (item->get_owner() && item->get_owner() != edited_scene && !edited_scene->is_editable_instance(item->get_owner())) {
  261. item = item->get_owner();
  262. }
  263. closest = item->get_instance_id();
  264. closest_dist = dist;
  265. selected_handle = handle;
  266. }
  267. }
  268. if (!item)
  269. return 0;
  270. if (!editor_selection->is_selected(item) || (r_gizmo_handle && selected_handle >= 0)) {
  271. if (r_gizmo_handle)
  272. *r_gizmo_handle = selected_handle;
  273. }
  274. return closest;
  275. }
  276. void SpatialEditorViewport::_find_items_at_pos(const Point2 &p_pos, bool &r_includes_current, Vector<_RayResult> &results, bool p_alt_select) {
  277. Vector3 ray = _get_ray(p_pos);
  278. Vector3 pos = _get_ray_pos(p_pos);
  279. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(pos, ray, get_tree()->get_root()->get_world()->get_scenario());
  280. Set<Ref<EditorSpatialGizmo> > found_gizmos;
  281. r_includes_current = false;
  282. for (int i = 0; i < instances.size(); i++) {
  283. Spatial *spat = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  284. if (!spat)
  285. continue;
  286. Ref<EditorSpatialGizmo> seg = spat->get_gizmo();
  287. if (!seg.is_valid())
  288. continue;
  289. if (found_gizmos.has(seg))
  290. continue;
  291. found_gizmos.insert(seg);
  292. Vector3 point;
  293. Vector3 normal;
  294. int handle = -1;
  295. bool inters = seg->intersect_ray(camera, p_pos, point, normal, NULL, p_alt_select);
  296. if (!inters)
  297. continue;
  298. float dist = pos.distance_to(point);
  299. if (dist < 0)
  300. continue;
  301. if (editor_selection->is_selected(spat))
  302. r_includes_current = true;
  303. _RayResult res;
  304. res.item = spat;
  305. res.depth = dist;
  306. res.handle = handle;
  307. results.push_back(res);
  308. }
  309. if (results.empty())
  310. return;
  311. results.sort();
  312. }
  313. Vector3 SpatialEditorViewport::_get_screen_to_space(const Vector3 &p_vector3) {
  314. CameraMatrix cm;
  315. if (orthogonal) {
  316. cm.set_orthogonal(camera->get_size(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  317. } else {
  318. cm.set_perspective(get_fov(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  319. }
  320. float screen_w, screen_h;
  321. cm.get_viewport_size(screen_w, screen_h);
  322. Transform camera_transform;
  323. camera_transform.translate(cursor.pos);
  324. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  325. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  326. camera_transform.translate(0, 0, cursor.distance);
  327. return camera_transform.xform(Vector3(((p_vector3.x / get_size().width) * 2.0 - 1.0) * screen_w, ((1.0 - (p_vector3.y / get_size().height)) * 2.0 - 1.0) * screen_h, -(get_znear() + p_vector3.z)));
  328. }
  329. void SpatialEditorViewport::_select_region() {
  330. if (cursor.region_begin == cursor.region_end)
  331. return; //nothing really
  332. float z_offset = MAX(0.0, 5.0 - get_znear());
  333. Vector3 box[4] = {
  334. Vector3(
  335. MIN(cursor.region_begin.x, cursor.region_end.x),
  336. MIN(cursor.region_begin.y, cursor.region_end.y),
  337. z_offset),
  338. Vector3(
  339. MAX(cursor.region_begin.x, cursor.region_end.x),
  340. MIN(cursor.region_begin.y, cursor.region_end.y),
  341. z_offset),
  342. Vector3(
  343. MAX(cursor.region_begin.x, cursor.region_end.x),
  344. MAX(cursor.region_begin.y, cursor.region_end.y),
  345. z_offset),
  346. Vector3(
  347. MIN(cursor.region_begin.x, cursor.region_end.x),
  348. MAX(cursor.region_begin.y, cursor.region_end.y),
  349. z_offset)
  350. };
  351. Vector<Plane> frustum;
  352. Vector3 cam_pos = _get_camera_position();
  353. for (int i = 0; i < 4; i++) {
  354. Vector3 a = _get_screen_to_space(box[i]);
  355. Vector3 b = _get_screen_to_space(box[(i + 1) % 4]);
  356. if (orthogonal) {
  357. frustum.push_back(Plane(a, (a - b).normalized()));
  358. } else {
  359. frustum.push_back(Plane(a, b, cam_pos));
  360. }
  361. }
  362. if (!orthogonal) {
  363. Plane near(cam_pos, -_get_camera_normal());
  364. near.d -= get_znear();
  365. frustum.push_back(near);
  366. Plane far = -near;
  367. far.d += get_zfar();
  368. frustum.push_back(far);
  369. }
  370. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_convex(frustum, get_tree()->get_root()->get_world()->get_scenario());
  371. Vector<Node *> selected;
  372. Node *edited_scene = get_tree()->get_edited_scene_root();
  373. for (int i = 0; i < instances.size(); i++) {
  374. Spatial *sp = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  375. if (!sp || _is_node_locked(sp))
  376. continue;
  377. Node *item = Object::cast_to<Node>(sp);
  378. while (item->get_owner() && item->get_owner() != edited_scene && !edited_scene->is_editable_instance(item->get_owner())) {
  379. item = item->get_owner();
  380. }
  381. // Replace the node by the group if grouped
  382. if (item->is_class("Spatial")) {
  383. Spatial *sel = Object::cast_to<Spatial>(item);
  384. while (item && item != editor->get_edited_scene()->get_parent()) {
  385. Spatial *selected_tmp = Object::cast_to<Spatial>(item);
  386. if (selected_tmp && item->has_meta("_edit_group_")) {
  387. sel = selected_tmp;
  388. }
  389. item = item->get_parent();
  390. }
  391. item = sel;
  392. }
  393. if (selected.find(item) != -1) continue;
  394. if (_is_node_locked(Object::cast_to<Spatial>(item))) continue;
  395. Ref<EditorSpatialGizmo> seg = sp->get_gizmo();
  396. if (!seg.is_valid())
  397. continue;
  398. if (seg->intersect_frustum(camera, frustum)) {
  399. selected.push_back(item);
  400. }
  401. }
  402. bool single = selected.size() == 1;
  403. for (int i = 0; i < selected.size(); i++) {
  404. _select(selected[i], true, single);
  405. }
  406. }
  407. void SpatialEditorViewport::_update_name() {
  408. String view_mode = orthogonal ? TTR("Orthogonal") : TTR("Perspective");
  409. if (name != "")
  410. view_menu->set_text(name + " " + view_mode);
  411. else
  412. view_menu->set_text(view_mode);
  413. view_menu->set_size(Vector2(0, 0)); // resets the button size
  414. }
  415. void SpatialEditorViewport::_compute_edit(const Point2 &p_point) {
  416. _edit.click_ray = _get_ray(Vector2(p_point.x, p_point.y));
  417. _edit.click_ray_pos = _get_ray_pos(Vector2(p_point.x, p_point.y));
  418. _edit.plane = TRANSFORM_VIEW;
  419. spatial_editor->update_transform_gizmo();
  420. _edit.center = spatial_editor->get_gizmo_transform().origin;
  421. List<Node *> &selection = editor_selection->get_selected_node_list();
  422. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  423. Spatial *sp = Object::cast_to<Spatial>(E->get());
  424. if (!sp)
  425. continue;
  426. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  427. if (!se)
  428. continue;
  429. se->original = se->sp->get_global_gizmo_transform();
  430. se->original_local = se->sp->get_local_gizmo_transform();
  431. }
  432. }
  433. static int _get_key_modifier_setting(const String &p_property) {
  434. switch (EditorSettings::get_singleton()->get(p_property).operator int()) {
  435. case 0: return 0;
  436. case 1: return KEY_SHIFT;
  437. case 2: return KEY_ALT;
  438. case 3: return KEY_META;
  439. case 4: return KEY_CONTROL;
  440. }
  441. return 0;
  442. }
  443. static int _get_key_modifier(Ref<InputEventWithModifiers> e) {
  444. if (e->get_shift())
  445. return KEY_SHIFT;
  446. if (e->get_alt())
  447. return KEY_ALT;
  448. if (e->get_control())
  449. return KEY_CONTROL;
  450. if (e->get_metakey())
  451. return KEY_META;
  452. return 0;
  453. }
  454. bool SpatialEditorViewport::_gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only) {
  455. if (!spatial_editor->is_gizmo_visible())
  456. return false;
  457. if (get_selected_count() == 0) {
  458. if (p_highlight_only)
  459. spatial_editor->select_gizmo_highlight_axis(-1);
  460. return false;
  461. }
  462. Vector3 ray_pos = _get_ray_pos(Vector2(p_screenpos.x, p_screenpos.y));
  463. Vector3 ray = _get_ray(Vector2(p_screenpos.x, p_screenpos.y));
  464. Transform gt = spatial_editor->get_gizmo_transform();
  465. float gs = gizmo_scale;
  466. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE) {
  467. int col_axis = -1;
  468. float col_d = 1e20;
  469. for (int i = 0; i < 3; i++) {
  470. Vector3 grabber_pos = gt.origin + gt.basis.get_axis(i) * gs * (GIZMO_ARROW_OFFSET + (GIZMO_ARROW_SIZE * 0.5));
  471. float grabber_radius = gs * GIZMO_ARROW_SIZE;
  472. Vector3 r;
  473. if (Geometry::segment_intersects_sphere(ray_pos, ray_pos + ray * MAX_Z, grabber_pos, grabber_radius, &r)) {
  474. float d = r.distance_to(ray_pos);
  475. if (d < col_d) {
  476. col_d = d;
  477. col_axis = i;
  478. }
  479. }
  480. }
  481. bool is_plane_translate = false;
  482. // plane select
  483. if (col_axis == -1) {
  484. col_d = 1e20;
  485. for (int i = 0; i < 3; i++) {
  486. Vector3 ivec2 = gt.basis.get_axis((i + 1) % 3).normalized();
  487. Vector3 ivec3 = gt.basis.get_axis((i + 2) % 3).normalized();
  488. Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gs * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST);
  489. Vector3 r;
  490. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  491. if (plane.intersects_ray(ray_pos, ray, &r)) {
  492. float dist = r.distance_to(grabber_pos);
  493. if (dist < (gs * GIZMO_PLANE_SIZE)) {
  494. float d = ray_pos.distance_to(r);
  495. if (d < col_d) {
  496. col_d = d;
  497. col_axis = i;
  498. is_plane_translate = true;
  499. }
  500. }
  501. }
  502. }
  503. }
  504. if (col_axis != -1) {
  505. if (p_highlight_only) {
  506. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_translate ? 6 : 0));
  507. } else {
  508. //handle plane translate
  509. _edit.mode = TRANSFORM_TRANSLATE;
  510. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  511. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_translate ? 3 : 0));
  512. }
  513. return true;
  514. }
  515. }
  516. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE) {
  517. int col_axis = -1;
  518. float col_d = 1e20;
  519. for (int i = 0; i < 3; i++) {
  520. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  521. Vector3 r;
  522. if (!plane.intersects_ray(ray_pos, ray, &r))
  523. continue;
  524. float dist = r.distance_to(gt.origin);
  525. if (dist > gs * (GIZMO_CIRCLE_SIZE - GIZMO_RING_HALF_WIDTH) && dist < gs * (GIZMO_CIRCLE_SIZE + GIZMO_RING_HALF_WIDTH)) {
  526. float d = ray_pos.distance_to(r);
  527. if (d < col_d) {
  528. col_d = d;
  529. col_axis = i;
  530. }
  531. }
  532. }
  533. if (col_axis != -1) {
  534. if (p_highlight_only) {
  535. spatial_editor->select_gizmo_highlight_axis(col_axis + 3);
  536. } else {
  537. //handle rotate
  538. _edit.mode = TRANSFORM_ROTATE;
  539. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  540. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis);
  541. }
  542. return true;
  543. }
  544. }
  545. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE) {
  546. int col_axis = -1;
  547. float col_d = 1e20;
  548. for (int i = 0; i < 3; i++) {
  549. Vector3 grabber_pos = gt.origin + gt.basis.get_axis(i) * gs * GIZMO_SCALE_OFFSET;
  550. float grabber_radius = gs * GIZMO_ARROW_SIZE;
  551. Vector3 r;
  552. if (Geometry::segment_intersects_sphere(ray_pos, ray_pos + ray * MAX_Z, grabber_pos, grabber_radius, &r)) {
  553. float d = r.distance_to(ray_pos);
  554. if (d < col_d) {
  555. col_d = d;
  556. col_axis = i;
  557. }
  558. }
  559. }
  560. bool is_plane_scale = false;
  561. // plane select
  562. if (col_axis == -1) {
  563. col_d = 1e20;
  564. for (int i = 0; i < 3; i++) {
  565. Vector3 ivec2 = gt.basis.get_axis((i + 1) % 3).normalized();
  566. Vector3 ivec3 = gt.basis.get_axis((i + 2) % 3).normalized();
  567. Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gs * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST);
  568. Vector3 r;
  569. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  570. if (plane.intersects_ray(ray_pos, ray, &r)) {
  571. float dist = r.distance_to(grabber_pos);
  572. if (dist < (gs * GIZMO_PLANE_SIZE)) {
  573. float d = ray_pos.distance_to(r);
  574. if (d < col_d) {
  575. col_d = d;
  576. col_axis = i;
  577. is_plane_scale = true;
  578. }
  579. }
  580. }
  581. }
  582. }
  583. if (col_axis != -1) {
  584. if (p_highlight_only) {
  585. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_scale ? 12 : 9));
  586. } else {
  587. //handle scale
  588. _edit.mode = TRANSFORM_SCALE;
  589. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  590. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_scale ? 3 : 0));
  591. }
  592. return true;
  593. }
  594. }
  595. if (p_highlight_only)
  596. spatial_editor->select_gizmo_highlight_axis(-1);
  597. return false;
  598. }
  599. void SpatialEditorViewport::_surface_mouse_enter() {
  600. if (!surface->has_focus() && (!get_focus_owner() || !get_focus_owner()->is_text_field()))
  601. surface->grab_focus();
  602. }
  603. void SpatialEditorViewport::_surface_mouse_exit() {
  604. _remove_preview();
  605. }
  606. void SpatialEditorViewport::_surface_focus_enter() {
  607. view_menu->set_disable_shortcuts(false);
  608. }
  609. void SpatialEditorViewport::_surface_focus_exit() {
  610. view_menu->set_disable_shortcuts(true);
  611. }
  612. bool SpatialEditorViewport ::_is_node_locked(const Node *p_node) {
  613. return p_node->has_meta("_edit_lock_") && p_node->get_meta("_edit_lock_");
  614. }
  615. void SpatialEditorViewport::_list_select(Ref<InputEventMouseButton> b) {
  616. _find_items_at_pos(b->get_position(), clicked_includes_current, selection_results, b->get_shift());
  617. Node *scene = editor->get_edited_scene();
  618. for (int i = 0; i < selection_results.size(); i++) {
  619. Spatial *item = selection_results[i].item;
  620. if (item != scene && item->get_owner() != scene && !scene->is_editable_instance(item->get_owner())) {
  621. //invalid result
  622. selection_results.remove(i);
  623. i--;
  624. }
  625. }
  626. clicked_wants_append = b->get_shift();
  627. if (selection_results.size() == 1) {
  628. clicked = selection_results[0].item->get_instance_id();
  629. selection_results.clear();
  630. if (clicked) {
  631. _select_clicked(clicked_wants_append, true);
  632. clicked = 0;
  633. }
  634. } else if (!selection_results.empty()) {
  635. NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
  636. StringName root_name = root_path.get_name(root_path.get_name_count() - 1);
  637. for (int i = 0; i < selection_results.size(); i++) {
  638. Spatial *spat = selection_results[i].item;
  639. Ref<Texture> icon = EditorNode::get_singleton()->get_object_icon(spat, "Node");
  640. String node_path = "/" + root_name + "/" + root_path.rel_path_to(spat->get_path());
  641. selection_menu->add_item(spat->get_name());
  642. selection_menu->set_item_icon(i, icon);
  643. selection_menu->set_item_metadata(i, node_path);
  644. selection_menu->set_item_tooltip(i, String(spat->get_name()) + "\nType: " + spat->get_class() + "\nPath: " + node_path);
  645. }
  646. selection_menu->set_global_position(b->get_global_position());
  647. selection_menu->popup();
  648. }
  649. }
  650. void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
  651. if (previewing)
  652. return; //do NONE
  653. {
  654. EditorNode *en = editor;
  655. EditorPluginList *force_input_forwarding_list = en->get_editor_plugins_force_input_forwarding();
  656. if (!force_input_forwarding_list->empty()) {
  657. bool discard = force_input_forwarding_list->forward_spatial_gui_input(camera, p_event, true);
  658. if (discard)
  659. return;
  660. }
  661. }
  662. {
  663. EditorNode *en = editor;
  664. EditorPluginList *over_plugin_list = en->get_editor_plugins_over();
  665. if (!over_plugin_list->empty()) {
  666. bool discard = over_plugin_list->forward_spatial_gui_input(camera, p_event, false);
  667. if (discard)
  668. return;
  669. }
  670. }
  671. Ref<InputEventMouseButton> b = p_event;
  672. if (b.is_valid()) {
  673. float zoom_factor = 1 + (ZOOM_MULTIPLIER - 1) * b->get_factor();
  674. switch (b->get_button_index()) {
  675. case BUTTON_WHEEL_UP: {
  676. if (is_freelook_active())
  677. scale_freelook_speed(zoom_factor);
  678. else
  679. scale_cursor_distance(1.0 / zoom_factor);
  680. } break;
  681. case BUTTON_WHEEL_DOWN: {
  682. if (is_freelook_active())
  683. scale_freelook_speed(1.0 / zoom_factor);
  684. else
  685. scale_cursor_distance(zoom_factor);
  686. } break;
  687. case BUTTON_RIGHT: {
  688. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  689. if (b->is_pressed() && _edit.gizmo.is_valid()) {
  690. //restore
  691. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_initial_value, true);
  692. _edit.gizmo = Ref<EditorSpatialGizmo>();
  693. }
  694. if (_edit.mode == TRANSFORM_NONE && b->is_pressed()) {
  695. if (b->get_alt()) {
  696. if (nav_scheme == NAVIGATION_MAYA)
  697. break;
  698. _list_select(b);
  699. return;
  700. }
  701. }
  702. if (_edit.mode != TRANSFORM_NONE && b->is_pressed()) {
  703. //cancel motion
  704. _edit.mode = TRANSFORM_NONE;
  705. List<Node *> &selection = editor_selection->get_selected_node_list();
  706. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  707. Spatial *sp = Object::cast_to<Spatial>(E->get());
  708. if (!sp)
  709. continue;
  710. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  711. if (!se)
  712. continue;
  713. sp->set_global_transform(se->original);
  714. }
  715. surface->update();
  716. set_message(TTR("Transform Aborted."), 3);
  717. }
  718. if (b->is_pressed()) {
  719. int mod = _get_key_modifier(b);
  720. if (!orthogonal) {
  721. if (mod == _get_key_modifier_setting("editors/3d/freelook/freelook_activation_modifier")) {
  722. set_freelook_active(true);
  723. }
  724. }
  725. } else {
  726. set_freelook_active(false);
  727. }
  728. if (freelook_active && !surface->has_focus()) {
  729. // Focus usually doesn't trigger on right-click, but in case of freelook it should,
  730. // otherwise using keyboard navigation would misbehave
  731. surface->grab_focus();
  732. }
  733. } break;
  734. case BUTTON_MIDDLE: {
  735. if (b->is_pressed() && _edit.mode != TRANSFORM_NONE) {
  736. switch (_edit.plane) {
  737. case TRANSFORM_VIEW: {
  738. _edit.plane = TRANSFORM_X_AXIS;
  739. set_message(TTR("X-Axis Transform."), 2);
  740. name = "";
  741. _update_name();
  742. } break;
  743. case TRANSFORM_X_AXIS: {
  744. _edit.plane = TRANSFORM_Y_AXIS;
  745. set_message(TTR("Y-Axis Transform."), 2);
  746. } break;
  747. case TRANSFORM_Y_AXIS: {
  748. _edit.plane = TRANSFORM_Z_AXIS;
  749. set_message(TTR("Z-Axis Transform."), 2);
  750. } break;
  751. case TRANSFORM_Z_AXIS: {
  752. _edit.plane = TRANSFORM_VIEW;
  753. set_message(TTR("View Plane Transform."), 2);
  754. } break;
  755. case TRANSFORM_YZ:
  756. case TRANSFORM_XZ:
  757. case TRANSFORM_XY: {
  758. } break;
  759. }
  760. }
  761. } break;
  762. case BUTTON_LEFT: {
  763. if (b->is_pressed()) {
  764. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  765. if ((nav_scheme == NAVIGATION_MAYA || nav_scheme == NAVIGATION_MODO) && b->get_alt()) {
  766. break;
  767. }
  768. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_LIST_SELECT) {
  769. _list_select(b);
  770. break;
  771. }
  772. _edit.mouse_pos = b->get_position();
  773. _edit.snap = spatial_editor->is_snap_enabled();
  774. _edit.mode = TRANSFORM_NONE;
  775. //gizmo has priority over everything
  776. bool can_select_gizmos = true;
  777. {
  778. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  779. can_select_gizmos = view_menu->get_popup()->is_item_checked(idx);
  780. }
  781. if (can_select_gizmos && spatial_editor->get_selected()) {
  782. Ref<EditorSpatialGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  783. if (seg.is_valid()) {
  784. int handle = -1;
  785. Vector3 point;
  786. Vector3 normal;
  787. bool inters = seg->intersect_ray(camera, _edit.mouse_pos, point, normal, &handle, b->get_shift());
  788. if (inters && handle != -1) {
  789. _edit.gizmo = seg;
  790. _edit.gizmo_handle = handle;
  791. _edit.gizmo_initial_value = seg->get_handle_value(handle);
  792. break;
  793. }
  794. }
  795. }
  796. if (_gizmo_select(_edit.mouse_pos))
  797. break;
  798. clicked = 0;
  799. clicked_includes_current = false;
  800. if ((spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT && b->get_control()) || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE) {
  801. /* HANDLE ROTATION */
  802. if (get_selected_count() == 0)
  803. break; //bye
  804. //handle rotate
  805. _edit.mode = TRANSFORM_ROTATE;
  806. _compute_edit(b->get_position());
  807. break;
  808. }
  809. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE) {
  810. if (get_selected_count() == 0)
  811. break; //bye
  812. //handle translate
  813. _edit.mode = TRANSFORM_TRANSLATE;
  814. _compute_edit(b->get_position());
  815. break;
  816. }
  817. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE) {
  818. if (get_selected_count() == 0)
  819. break; //bye
  820. //handle scale
  821. _edit.mode = TRANSFORM_SCALE;
  822. _compute_edit(b->get_position());
  823. break;
  824. }
  825. // todo scale
  826. int gizmo_handle = -1;
  827. clicked = _select_ray(b->get_position(), b->get_shift(), clicked_includes_current, &gizmo_handle, b->get_shift());
  828. //clicking is always deferred to either move or release
  829. clicked_wants_append = b->get_shift();
  830. if (!clicked) {
  831. if (!clicked_wants_append)
  832. _clear_selected();
  833. //default to regionselect
  834. cursor.region_select = true;
  835. cursor.region_begin = b->get_position();
  836. cursor.region_end = b->get_position();
  837. }
  838. if (clicked && gizmo_handle >= 0) {
  839. Spatial *spa = Object::cast_to<Spatial>(ObjectDB::get_instance(clicked));
  840. if (spa) {
  841. Ref<EditorSpatialGizmo> seg = spa->get_gizmo();
  842. if (seg.is_valid()) {
  843. _edit.gizmo = seg;
  844. _edit.gizmo_handle = gizmo_handle;
  845. _edit.gizmo_initial_value = seg->get_handle_value(gizmo_handle);
  846. break;
  847. }
  848. }
  849. }
  850. surface->update();
  851. } else {
  852. if (_edit.gizmo.is_valid()) {
  853. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_initial_value, false);
  854. _edit.gizmo = Ref<EditorSpatialGizmo>();
  855. break;
  856. }
  857. if (clicked) {
  858. _select_clicked(clicked_wants_append, true);
  859. // Processing was deferred.
  860. clicked = 0;
  861. }
  862. if (cursor.region_select) {
  863. if (!clicked_wants_append) _clear_selected();
  864. _select_region();
  865. cursor.region_select = false;
  866. surface->update();
  867. }
  868. if (_edit.mode != TRANSFORM_NONE) {
  869. static const char *_transform_name[4] = { "None", "Rotate", "Translate", "Scale" };
  870. undo_redo->create_action(_transform_name[_edit.mode]);
  871. List<Node *> &selection = editor_selection->get_selected_node_list();
  872. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  873. Spatial *sp = Object::cast_to<Spatial>(E->get());
  874. if (!sp)
  875. continue;
  876. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  877. if (!se)
  878. continue;
  879. undo_redo->add_do_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  880. undo_redo->add_undo_method(sp, "set_global_transform", se->original);
  881. }
  882. undo_redo->commit_action();
  883. _edit.mode = TRANSFORM_NONE;
  884. set_message("");
  885. }
  886. surface->update();
  887. }
  888. } break;
  889. }
  890. }
  891. Ref<InputEventMouseMotion> m = p_event;
  892. if (m.is_valid()) {
  893. _edit.mouse_pos = m->get_position();
  894. if (spatial_editor->get_selected()) {
  895. Ref<EditorSpatialGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  896. if (seg.is_valid()) {
  897. int selected_handle = -1;
  898. int handle = -1;
  899. Vector3 point;
  900. Vector3 normal;
  901. bool inters = seg->intersect_ray(camera, _edit.mouse_pos, point, normal, &handle, false);
  902. if (inters && handle != -1) {
  903. selected_handle = handle;
  904. }
  905. if (selected_handle != spatial_editor->get_over_gizmo_handle()) {
  906. spatial_editor->set_over_gizmo_handle(selected_handle);
  907. spatial_editor->get_selected()->update_gizmo();
  908. if (selected_handle != -1)
  909. spatial_editor->select_gizmo_highlight_axis(-1);
  910. }
  911. }
  912. }
  913. if (spatial_editor->get_over_gizmo_handle() == -1 && !(m->get_button_mask() & 1) && !_edit.gizmo.is_valid()) {
  914. _gizmo_select(_edit.mouse_pos, true);
  915. }
  916. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  917. NavigationMode nav_mode = NAVIGATION_NONE;
  918. if (_edit.gizmo.is_valid()) {
  919. _edit.gizmo->set_handle(_edit.gizmo_handle, camera, m->get_position());
  920. Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle);
  921. String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle);
  922. set_message(n + ": " + String(v));
  923. } else if (m->get_button_mask() & BUTTON_MASK_LEFT) {
  924. if (nav_scheme == NAVIGATION_MAYA && m->get_alt()) {
  925. nav_mode = NAVIGATION_ORBIT;
  926. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt() && m->get_shift()) {
  927. nav_mode = NAVIGATION_PAN;
  928. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt() && m->get_control()) {
  929. nav_mode = NAVIGATION_ZOOM;
  930. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt()) {
  931. nav_mode = NAVIGATION_ORBIT;
  932. } else {
  933. if (clicked) {
  934. if (!clicked_includes_current) {
  935. _select_clicked(clicked_wants_append, true);
  936. // Processing was deferred.
  937. }
  938. _compute_edit(_edit.mouse_pos);
  939. clicked = 0;
  940. _edit.mode = TRANSFORM_TRANSLATE;
  941. }
  942. if (cursor.region_select) {
  943. cursor.region_end = m->get_position();
  944. surface->update();
  945. return;
  946. }
  947. if (_edit.mode == TRANSFORM_NONE)
  948. return;
  949. Vector3 ray_pos = _get_ray_pos(m->get_position());
  950. Vector3 ray = _get_ray(m->get_position());
  951. float snap = EDITOR_GET("interface/inspector/default_float_step");
  952. int snap_step_decimals = Math::range_step_decimals(snap);
  953. switch (_edit.mode) {
  954. case TRANSFORM_SCALE: {
  955. Vector3 motion_mask;
  956. Plane plane;
  957. bool plane_mv = false;
  958. switch (_edit.plane) {
  959. case TRANSFORM_VIEW:
  960. motion_mask = Vector3(0, 0, 0);
  961. plane = Plane(_edit.center, _get_camera_normal());
  962. break;
  963. case TRANSFORM_X_AXIS:
  964. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0);
  965. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  966. break;
  967. case TRANSFORM_Y_AXIS:
  968. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(1);
  969. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  970. break;
  971. case TRANSFORM_Z_AXIS:
  972. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2);
  973. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  974. break;
  975. case TRANSFORM_YZ:
  976. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2) + spatial_editor->get_gizmo_transform().basis.get_axis(1);
  977. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  978. plane_mv = true;
  979. break;
  980. case TRANSFORM_XZ:
  981. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2) + spatial_editor->get_gizmo_transform().basis.get_axis(0);
  982. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  983. plane_mv = true;
  984. break;
  985. case TRANSFORM_XY:
  986. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0) + spatial_editor->get_gizmo_transform().basis.get_axis(1);
  987. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  988. plane_mv = true;
  989. break;
  990. }
  991. Vector3 intersection;
  992. if (!plane.intersects_ray(ray_pos, ray, &intersection))
  993. break;
  994. Vector3 click;
  995. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click))
  996. break;
  997. Vector3 motion = intersection - click;
  998. if (_edit.plane != TRANSFORM_VIEW) {
  999. if (!plane_mv) {
  1000. motion = motion_mask.dot(motion) * motion_mask;
  1001. } else {
  1002. // Alternative planar scaling mode
  1003. if (_get_key_modifier(m) != KEY_SHIFT) {
  1004. motion = motion_mask.dot(motion) * motion_mask;
  1005. }
  1006. }
  1007. } else {
  1008. float center_click_dist = click.distance_to(_edit.center);
  1009. float center_inters_dist = intersection.distance_to(_edit.center);
  1010. if (center_click_dist == 0)
  1011. break;
  1012. float scale = center_inters_dist - center_click_dist;
  1013. motion = Vector3(scale, scale, scale);
  1014. }
  1015. List<Node *> &selection = editor_selection->get_selected_node_list();
  1016. // Disable local transformation for TRANSFORM_VIEW
  1017. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  1018. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1019. snap = spatial_editor->get_scale_snap() / 100;
  1020. }
  1021. Vector3 motion_snapped = motion;
  1022. motion_snapped.snap(Vector3(snap, snap, snap));
  1023. // This might not be necessary anymore after issue #288 is solved (in 4.0?).
  1024. set_message(TTR("Scaling: ") + "(" + String::num(motion_snapped.x, snap_step_decimals) + ", " +
  1025. String::num(motion_snapped.y, snap_step_decimals) + ", " + String::num(motion_snapped.z, snap_step_decimals) + ")");
  1026. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1027. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1028. if (!sp) {
  1029. continue;
  1030. }
  1031. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1032. if (!se) {
  1033. continue;
  1034. }
  1035. if (sp->has_meta("_edit_lock_")) {
  1036. continue;
  1037. }
  1038. Transform original = se->original;
  1039. Transform original_local = se->original_local;
  1040. Transform base = Transform(Basis(), _edit.center);
  1041. Transform t;
  1042. Vector3 local_scale;
  1043. if (local_coords) {
  1044. Basis g = original.basis.orthonormalized();
  1045. Vector3 local_motion = g.inverse().xform(motion);
  1046. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1047. local_motion.snap(Vector3(snap, snap, snap));
  1048. }
  1049. local_scale = original_local.basis.get_scale() * (local_motion + Vector3(1, 1, 1));
  1050. // Prevent scaling to 0 it would break the gizmo
  1051. Basis check = original_local.basis;
  1052. check.scale(local_scale);
  1053. if (check.determinant() != 0) {
  1054. // Apply scale
  1055. sp->set_scale(local_scale);
  1056. }
  1057. } else {
  1058. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1059. motion.snap(Vector3(snap, snap, snap));
  1060. }
  1061. Transform r;
  1062. r.basis.scale(motion + Vector3(1, 1, 1));
  1063. t = base * (r * (base.inverse() * original));
  1064. // Apply scale
  1065. sp->set_global_transform(t);
  1066. }
  1067. }
  1068. surface->update();
  1069. } break;
  1070. case TRANSFORM_TRANSLATE: {
  1071. Vector3 motion_mask;
  1072. Plane plane;
  1073. bool plane_mv = false;
  1074. switch (_edit.plane) {
  1075. case TRANSFORM_VIEW:
  1076. plane = Plane(_edit.center, _get_camera_normal());
  1077. break;
  1078. case TRANSFORM_X_AXIS:
  1079. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0);
  1080. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1081. break;
  1082. case TRANSFORM_Y_AXIS:
  1083. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(1);
  1084. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1085. break;
  1086. case TRANSFORM_Z_AXIS:
  1087. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2);
  1088. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1089. break;
  1090. case TRANSFORM_YZ:
  1091. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  1092. plane_mv = true;
  1093. break;
  1094. case TRANSFORM_XZ:
  1095. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  1096. plane_mv = true;
  1097. break;
  1098. case TRANSFORM_XY:
  1099. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  1100. plane_mv = true;
  1101. break;
  1102. }
  1103. Vector3 intersection;
  1104. if (!plane.intersects_ray(ray_pos, ray, &intersection))
  1105. break;
  1106. Vector3 click;
  1107. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click))
  1108. break;
  1109. Vector3 motion = intersection - click;
  1110. if (_edit.plane != TRANSFORM_VIEW) {
  1111. if (!plane_mv) {
  1112. motion = motion_mask.dot(motion) * motion_mask;
  1113. }
  1114. }
  1115. List<Node *> &selection = editor_selection->get_selected_node_list();
  1116. // Disable local transformation for TRANSFORM_VIEW
  1117. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  1118. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1119. snap = spatial_editor->get_translate_snap();
  1120. }
  1121. Vector3 motion_snapped = motion;
  1122. motion_snapped.snap(Vector3(snap, snap, snap));
  1123. set_message(TTR("Translating: ") + "(" + String::num(motion_snapped.x, snap_step_decimals) + ", " +
  1124. String::num(motion_snapped.y, snap_step_decimals) + ", " + String::num(motion_snapped.z, snap_step_decimals) + ")");
  1125. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1126. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1127. if (!sp) {
  1128. continue;
  1129. }
  1130. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1131. if (!se) {
  1132. continue;
  1133. }
  1134. if (sp->has_meta("_edit_lock_")) {
  1135. continue;
  1136. }
  1137. Transform original = se->original;
  1138. Transform t;
  1139. if (local_coords) {
  1140. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1141. Basis g = original.basis.orthonormalized();
  1142. Vector3 local_motion = g.inverse().xform(motion);
  1143. local_motion.snap(Vector3(snap, snap, snap));
  1144. motion = g.xform(local_motion);
  1145. }
  1146. } else {
  1147. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1148. motion.snap(Vector3(snap, snap, snap));
  1149. }
  1150. }
  1151. // Apply translation
  1152. t = original;
  1153. t.origin += motion;
  1154. sp->set_global_transform(t);
  1155. }
  1156. surface->update();
  1157. } break;
  1158. case TRANSFORM_ROTATE: {
  1159. Plane plane;
  1160. Vector3 axis;
  1161. switch (_edit.plane) {
  1162. case TRANSFORM_VIEW:
  1163. plane = Plane(_edit.center, _get_camera_normal());
  1164. break;
  1165. case TRANSFORM_X_AXIS:
  1166. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  1167. axis = Vector3(1, 0, 0);
  1168. break;
  1169. case TRANSFORM_Y_AXIS:
  1170. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  1171. axis = Vector3(0, 1, 0);
  1172. break;
  1173. case TRANSFORM_Z_AXIS:
  1174. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  1175. axis = Vector3(0, 0, 1);
  1176. break;
  1177. case TRANSFORM_YZ:
  1178. case TRANSFORM_XZ:
  1179. case TRANSFORM_XY:
  1180. break;
  1181. }
  1182. Vector3 intersection;
  1183. if (!plane.intersects_ray(ray_pos, ray, &intersection))
  1184. break;
  1185. Vector3 click;
  1186. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click))
  1187. break;
  1188. Vector3 y_axis = (click - _edit.center).normalized();
  1189. Vector3 x_axis = plane.normal.cross(y_axis).normalized();
  1190. float angle = Math::atan2(x_axis.dot(intersection - _edit.center), y_axis.dot(intersection - _edit.center));
  1191. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1192. snap = spatial_editor->get_rotate_snap();
  1193. }
  1194. angle = Math::rad2deg(angle) + snap * 0.5; //else it won't reach +180
  1195. angle -= Math::fmod(angle, snap);
  1196. set_message(vformat(TTR("Rotating %s degrees."), String::num(angle, snap_step_decimals)));
  1197. angle = Math::deg2rad(angle);
  1198. List<Node *> &selection = editor_selection->get_selected_node_list();
  1199. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); // Disable local transformation for TRANSFORM_VIEW
  1200. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1201. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1202. if (!sp)
  1203. continue;
  1204. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1205. if (!se)
  1206. continue;
  1207. if (sp->has_meta("_edit_lock_")) {
  1208. continue;
  1209. }
  1210. Transform t;
  1211. if (local_coords) {
  1212. Transform original_local = se->original_local;
  1213. Basis rot = Basis(axis, angle);
  1214. t.basis = original_local.get_basis().orthonormalized() * rot;
  1215. t.origin = original_local.origin;
  1216. // Apply rotation
  1217. sp->set_transform(t);
  1218. sp->set_scale(original_local.basis.get_scale()); // re-apply original scale
  1219. } else {
  1220. Transform original = se->original;
  1221. Transform r;
  1222. Transform base = Transform(Basis(), _edit.center);
  1223. r.basis.rotate(plane.normal, angle);
  1224. t = base * r * base.inverse() * original;
  1225. // Apply rotation
  1226. sp->set_global_transform(t);
  1227. }
  1228. }
  1229. surface->update();
  1230. } break;
  1231. default: {
  1232. }
  1233. }
  1234. }
  1235. } else if ((m->get_button_mask() & BUTTON_MASK_RIGHT) || freelook_active) {
  1236. if (nav_scheme == NAVIGATION_MAYA && m->get_alt()) {
  1237. nav_mode = NAVIGATION_ZOOM;
  1238. } else if (freelook_active) {
  1239. nav_mode = NAVIGATION_LOOK;
  1240. } else if (orthogonal) {
  1241. nav_mode = NAVIGATION_PAN;
  1242. }
  1243. } else if (m->get_button_mask() & BUTTON_MASK_MIDDLE) {
  1244. if (nav_scheme == NAVIGATION_GODOT) {
  1245. int mod = _get_key_modifier(m);
  1246. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier"))
  1247. nav_mode = NAVIGATION_PAN;
  1248. else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier"))
  1249. nav_mode = NAVIGATION_ZOOM;
  1250. else if (mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier"))
  1251. nav_mode = NAVIGATION_ORBIT;
  1252. } else if (nav_scheme == NAVIGATION_MAYA) {
  1253. if (m->get_alt())
  1254. nav_mode = NAVIGATION_PAN;
  1255. }
  1256. } else if (EditorSettings::get_singleton()->get("editors/3d/navigation/emulate_3_button_mouse")) {
  1257. // Handle trackpad (no external mouse) use case
  1258. int mod = _get_key_modifier(m);
  1259. if (mod) {
  1260. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier"))
  1261. nav_mode = NAVIGATION_PAN;
  1262. else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier"))
  1263. nav_mode = NAVIGATION_ZOOM;
  1264. else if (mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier"))
  1265. nav_mode = NAVIGATION_ORBIT;
  1266. }
  1267. }
  1268. switch (nav_mode) {
  1269. case NAVIGATION_PAN: {
  1270. _nav_pan(m, _get_warped_mouse_motion(m));
  1271. } break;
  1272. case NAVIGATION_ZOOM: {
  1273. _nav_zoom(m, m->get_relative());
  1274. } break;
  1275. case NAVIGATION_ORBIT: {
  1276. _nav_orbit(m, _get_warped_mouse_motion(m));
  1277. } break;
  1278. case NAVIGATION_LOOK: {
  1279. _nav_look(m, _get_warped_mouse_motion(m));
  1280. } break;
  1281. default: {
  1282. }
  1283. }
  1284. }
  1285. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  1286. if (magnify_gesture.is_valid()) {
  1287. if (is_freelook_active())
  1288. scale_freelook_speed(magnify_gesture->get_factor());
  1289. else
  1290. scale_cursor_distance(1.0 / magnify_gesture->get_factor());
  1291. }
  1292. Ref<InputEventPanGesture> pan_gesture = p_event;
  1293. if (pan_gesture.is_valid()) {
  1294. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1295. NavigationMode nav_mode = NAVIGATION_NONE;
  1296. if (nav_scheme == NAVIGATION_GODOT) {
  1297. int mod = _get_key_modifier(pan_gesture);
  1298. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier"))
  1299. nav_mode = NAVIGATION_PAN;
  1300. else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier"))
  1301. nav_mode = NAVIGATION_ZOOM;
  1302. else if (mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier"))
  1303. nav_mode = NAVIGATION_ORBIT;
  1304. } else if (nav_scheme == NAVIGATION_MAYA) {
  1305. if (pan_gesture->get_alt())
  1306. nav_mode = NAVIGATION_PAN;
  1307. }
  1308. switch (nav_mode) {
  1309. case NAVIGATION_PAN: {
  1310. _nav_pan(m, pan_gesture->get_delta());
  1311. } break;
  1312. case NAVIGATION_ZOOM: {
  1313. _nav_zoom(m, pan_gesture->get_delta());
  1314. } break;
  1315. case NAVIGATION_ORBIT: {
  1316. _nav_orbit(m, pan_gesture->get_delta());
  1317. } break;
  1318. case NAVIGATION_LOOK: {
  1319. _nav_look(m, pan_gesture->get_delta());
  1320. } break;
  1321. default: {
  1322. }
  1323. }
  1324. }
  1325. Ref<InputEventKey> k = p_event;
  1326. if (k.is_valid()) {
  1327. if (!k->is_pressed())
  1328. return;
  1329. if (ED_IS_SHORTCUT("spatial_editor/snap", p_event)) {
  1330. if (_edit.mode != TRANSFORM_NONE) {
  1331. _edit.snap = !_edit.snap;
  1332. }
  1333. }
  1334. if (ED_IS_SHORTCUT("spatial_editor/bottom_view", p_event)) {
  1335. _menu_option(VIEW_BOTTOM);
  1336. }
  1337. if (ED_IS_SHORTCUT("spatial_editor/top_view", p_event)) {
  1338. _menu_option(VIEW_TOP);
  1339. }
  1340. if (ED_IS_SHORTCUT("spatial_editor/rear_view", p_event)) {
  1341. _menu_option(VIEW_REAR);
  1342. }
  1343. if (ED_IS_SHORTCUT("spatial_editor/front_view", p_event)) {
  1344. _menu_option(VIEW_FRONT);
  1345. }
  1346. if (ED_IS_SHORTCUT("spatial_editor/left_view", p_event)) {
  1347. _menu_option(VIEW_LEFT);
  1348. }
  1349. if (ED_IS_SHORTCUT("spatial_editor/right_view", p_event)) {
  1350. _menu_option(VIEW_RIGHT);
  1351. }
  1352. if (ED_IS_SHORTCUT("spatial_editor/focus_origin", p_event)) {
  1353. _menu_option(VIEW_CENTER_TO_ORIGIN);
  1354. }
  1355. if (ED_IS_SHORTCUT("spatial_editor/focus_selection", p_event)) {
  1356. _menu_option(VIEW_CENTER_TO_SELECTION);
  1357. }
  1358. // Orthgonal mode doesn't work in freelook.
  1359. if (!freelook_active && ED_IS_SHORTCUT("spatial_editor/switch_perspective_orthogonal", p_event)) {
  1360. _menu_option(orthogonal ? VIEW_PERSPECTIVE : VIEW_ORTHOGONAL);
  1361. _update_name();
  1362. }
  1363. if (ED_IS_SHORTCUT("spatial_editor/align_transform_with_view", p_event)) {
  1364. _menu_option(VIEW_ALIGN_TRANSFORM_WITH_VIEW);
  1365. }
  1366. if (ED_IS_SHORTCUT("spatial_editor/align_rotation_with_view", p_event)) {
  1367. _menu_option(VIEW_ALIGN_ROTATION_WITH_VIEW);
  1368. }
  1369. if (ED_IS_SHORTCUT("spatial_editor/insert_anim_key", p_event)) {
  1370. if (!get_selected_count() || _edit.mode != TRANSFORM_NONE)
  1371. return;
  1372. if (!AnimationPlayerEditor::singleton->get_track_editor()->has_keying()) {
  1373. set_message(TTR("Keying is disabled (no key inserted)."));
  1374. return;
  1375. }
  1376. List<Node *> &selection = editor_selection->get_selected_node_list();
  1377. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1378. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1379. if (!sp)
  1380. continue;
  1381. spatial_editor->emit_signal("transform_key_request", sp, "", sp->get_transform());
  1382. }
  1383. set_message(TTR("Animation Key Inserted."));
  1384. }
  1385. // Freelook doesn't work in orthogonal mode.
  1386. if (!orthogonal && ED_IS_SHORTCUT("spatial_editor/freelook_toggle", p_event)) {
  1387. set_freelook_active(!is_freelook_active());
  1388. } else if (k->get_scancode() == KEY_ESCAPE) {
  1389. set_freelook_active(false);
  1390. }
  1391. if (k->get_scancode() == KEY_SPACE) {
  1392. if (!k->is_pressed()) emit_signal("toggle_maximize_view", this);
  1393. }
  1394. }
  1395. // freelook uses most of the useful shortcuts, like save, so its ok
  1396. // to consider freelook active as end of the line for future events.
  1397. if (freelook_active)
  1398. accept_event();
  1399. }
  1400. void SpatialEditorViewport::_nav_pan(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1401. const NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1402. real_t pan_speed = 1 / 150.0;
  1403. int pan_speed_modifier = 10;
  1404. if (nav_scheme == NAVIGATION_MAYA && p_event->get_shift())
  1405. pan_speed *= pan_speed_modifier;
  1406. Transform camera_transform;
  1407. camera_transform.translate(cursor.pos);
  1408. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  1409. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  1410. Vector3 translation(-p_relative.x * pan_speed, p_relative.y * pan_speed, 0);
  1411. translation *= cursor.distance / DISTANCE_DEFAULT;
  1412. camera_transform.translate(translation);
  1413. cursor.pos = camera_transform.origin;
  1414. }
  1415. void SpatialEditorViewport::_nav_zoom(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1416. const NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1417. real_t zoom_speed = 1 / 80.0;
  1418. int zoom_speed_modifier = 10;
  1419. if (nav_scheme == NAVIGATION_MAYA && p_event->get_shift())
  1420. zoom_speed *= zoom_speed_modifier;
  1421. NavigationZoomStyle zoom_style = (NavigationZoomStyle)EditorSettings::get_singleton()->get("editors/3d/navigation/zoom_style").operator int();
  1422. if (zoom_style == NAVIGATION_ZOOM_HORIZONTAL) {
  1423. if (p_relative.x > 0)
  1424. scale_cursor_distance(1 - p_relative.x * zoom_speed);
  1425. else if (p_relative.x < 0)
  1426. scale_cursor_distance(1.0 / (1 + p_relative.x * zoom_speed));
  1427. } else {
  1428. if (p_relative.y > 0)
  1429. scale_cursor_distance(1 + p_relative.y * zoom_speed);
  1430. else if (p_relative.y < 0)
  1431. scale_cursor_distance(1.0 / (1 - p_relative.y * zoom_speed));
  1432. }
  1433. }
  1434. void SpatialEditorViewport::_nav_orbit(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1435. if (lock_rotation) {
  1436. _nav_pan(p_event, p_relative);
  1437. return;
  1438. }
  1439. real_t degrees_per_pixel = EditorSettings::get_singleton()->get("editors/3d/navigation_feel/orbit_sensitivity");
  1440. real_t radians_per_pixel = Math::deg2rad(degrees_per_pixel);
  1441. bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y_axis");
  1442. if (invert_y_axis) {
  1443. cursor.x_rot -= p_relative.y * radians_per_pixel;
  1444. } else {
  1445. cursor.x_rot += p_relative.y * radians_per_pixel;
  1446. }
  1447. cursor.y_rot += p_relative.x * radians_per_pixel;
  1448. if (cursor.x_rot > Math_PI / 2.0)
  1449. cursor.x_rot = Math_PI / 2.0;
  1450. if (cursor.x_rot < -Math_PI / 2.0)
  1451. cursor.x_rot = -Math_PI / 2.0;
  1452. name = "";
  1453. _update_name();
  1454. }
  1455. void SpatialEditorViewport::_nav_look(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1456. if (orthogonal) {
  1457. _nav_pan(p_event, p_relative);
  1458. return;
  1459. }
  1460. real_t degrees_per_pixel = EditorSettings::get_singleton()->get("editors/3d/navigation_feel/orbit_sensitivity");
  1461. real_t radians_per_pixel = Math::deg2rad(degrees_per_pixel);
  1462. bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y_axis");
  1463. // Note: do NOT assume the camera has the "current" transform, because it is interpolated and may have "lag".
  1464. Transform prev_camera_transform = to_camera_transform(cursor);
  1465. if (invert_y_axis) {
  1466. cursor.x_rot -= p_relative.y * radians_per_pixel;
  1467. } else {
  1468. cursor.x_rot += p_relative.y * radians_per_pixel;
  1469. }
  1470. cursor.y_rot += p_relative.x * radians_per_pixel;
  1471. if (cursor.x_rot > Math_PI / 2.0)
  1472. cursor.x_rot = Math_PI / 2.0;
  1473. if (cursor.x_rot < -Math_PI / 2.0)
  1474. cursor.x_rot = -Math_PI / 2.0;
  1475. // Look is like the opposite of Orbit: the focus point rotates around the camera
  1476. Transform camera_transform = to_camera_transform(cursor);
  1477. Vector3 pos = camera_transform.xform(Vector3(0, 0, 0));
  1478. Vector3 prev_pos = prev_camera_transform.xform(Vector3(0, 0, 0));
  1479. Vector3 diff = prev_pos - pos;
  1480. cursor.pos += diff;
  1481. name = "";
  1482. _update_name();
  1483. }
  1484. void SpatialEditorViewport::set_freelook_active(bool active_now) {
  1485. if (!freelook_active && active_now) {
  1486. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  1487. cursor = camera_cursor;
  1488. // Make sure eye_pos is synced, because freelook referential is eye pos rather than orbit pos
  1489. Vector3 forward = to_camera_transform(cursor).basis.xform(Vector3(0, 0, -1));
  1490. cursor.eye_pos = cursor.pos - cursor.distance * forward;
  1491. // Also sync the camera cursor, otherwise switching to freelook will be trippy if inertia is active
  1492. camera_cursor.eye_pos = cursor.eye_pos;
  1493. if (EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_speed_zoom_link")) {
  1494. // Re-adjust freelook speed from the current zoom level
  1495. real_t base_speed = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_base_speed");
  1496. freelook_speed = base_speed * cursor.distance;
  1497. }
  1498. // Hide mouse like in an FPS (warping doesn't work)
  1499. OS::get_singleton()->set_mouse_mode(OS::MOUSE_MODE_CAPTURED);
  1500. } else if (freelook_active && !active_now) {
  1501. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  1502. cursor = camera_cursor;
  1503. // Restore mouse
  1504. OS::get_singleton()->set_mouse_mode(OS::MOUSE_MODE_VISIBLE);
  1505. }
  1506. freelook_active = active_now;
  1507. }
  1508. void SpatialEditorViewport::scale_cursor_distance(real_t scale) {
  1509. // Prevents zero distance which would short-circuit any scaling
  1510. if (cursor.distance < ZOOM_MIN_DISTANCE)
  1511. cursor.distance = ZOOM_MIN_DISTANCE;
  1512. cursor.distance *= scale;
  1513. if (cursor.distance < ZOOM_MIN_DISTANCE)
  1514. cursor.distance = ZOOM_MIN_DISTANCE;
  1515. zoom_indicator_delay = ZOOM_INDICATOR_DELAY_S;
  1516. surface->update();
  1517. }
  1518. void SpatialEditorViewport::scale_freelook_speed(real_t scale) {
  1519. // Prevents zero distance which would short-circuit any scaling
  1520. if (freelook_speed < FREELOOK_MIN_SPEED)
  1521. freelook_speed = FREELOOK_MIN_SPEED;
  1522. freelook_speed *= scale;
  1523. if (freelook_speed < FREELOOK_MIN_SPEED)
  1524. freelook_speed = FREELOOK_MIN_SPEED;
  1525. zoom_indicator_delay = ZOOM_INDICATOR_DELAY_S;
  1526. surface->update();
  1527. }
  1528. Point2i SpatialEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const {
  1529. Point2i relative;
  1530. if (bool(EDITOR_DEF("editors/3d/navigation/warped_mouse_panning", false))) {
  1531. relative = Input::get_singleton()->warp_mouse_motion(p_ev_mouse_motion, surface->get_global_rect());
  1532. } else {
  1533. relative = p_ev_mouse_motion->get_relative();
  1534. }
  1535. return relative;
  1536. }
  1537. static bool is_shortcut_pressed(const String &p_path) {
  1538. Ref<ShortCut> shortcut = ED_GET_SHORTCUT(p_path);
  1539. if (shortcut.is_null()) {
  1540. return false;
  1541. }
  1542. InputEventKey *k = Object::cast_to<InputEventKey>(shortcut->get_shortcut().ptr());
  1543. if (k == NULL) {
  1544. return false;
  1545. }
  1546. const Input &input = *Input::get_singleton();
  1547. int scancode = k->get_scancode();
  1548. return input.is_key_pressed(scancode);
  1549. }
  1550. void SpatialEditorViewport::_update_freelook(real_t delta) {
  1551. if (!is_freelook_active()) {
  1552. return;
  1553. }
  1554. Vector3 forward = camera->get_transform().basis.xform(Vector3(0, 0, -1));
  1555. Vector3 right = camera->get_transform().basis.xform(Vector3(1, 0, 0));
  1556. Vector3 up = camera->get_transform().basis.xform(Vector3(0, 1, 0));
  1557. Vector3 direction;
  1558. bool speed_modifier = false;
  1559. if (is_shortcut_pressed("spatial_editor/freelook_left")) {
  1560. direction -= right;
  1561. }
  1562. if (is_shortcut_pressed("spatial_editor/freelook_right")) {
  1563. direction += right;
  1564. }
  1565. if (is_shortcut_pressed("spatial_editor/freelook_forward")) {
  1566. direction += forward;
  1567. }
  1568. if (is_shortcut_pressed("spatial_editor/freelook_backwards")) {
  1569. direction -= forward;
  1570. }
  1571. if (is_shortcut_pressed("spatial_editor/freelook_up")) {
  1572. direction += up;
  1573. }
  1574. if (is_shortcut_pressed("spatial_editor/freelook_down")) {
  1575. direction -= up;
  1576. }
  1577. if (is_shortcut_pressed("spatial_editor/freelook_speed_modifier")) {
  1578. speed_modifier = true;
  1579. }
  1580. real_t speed = freelook_speed;
  1581. if (speed_modifier) {
  1582. real_t modifier_speed_factor = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_modifier_speed_factor");
  1583. speed *= modifier_speed_factor;
  1584. }
  1585. Vector3 motion = direction * speed * delta;
  1586. cursor.pos += motion;
  1587. cursor.eye_pos += motion;
  1588. }
  1589. void SpatialEditorViewport::set_message(String p_message, float p_time) {
  1590. message = p_message;
  1591. message_time = p_time;
  1592. }
  1593. void SpatialEditorPlugin::edited_scene_changed() {
  1594. for (uint32_t i = 0; i < SpatialEditor::VIEWPORTS_COUNT; i++) {
  1595. SpatialEditorViewport *viewport = SpatialEditor::get_singleton()->get_editor_viewport(i);
  1596. if (viewport->is_visible()) {
  1597. viewport->notification(Control::NOTIFICATION_VISIBILITY_CHANGED);
  1598. }
  1599. }
  1600. }
  1601. void SpatialEditorViewport::_notification(int p_what) {
  1602. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  1603. bool visible = is_visible_in_tree();
  1604. set_process(visible);
  1605. if (visible) {
  1606. _update_camera(0);
  1607. } else {
  1608. set_freelook_active(false);
  1609. }
  1610. call_deferred("update_transform_gizmo_view");
  1611. }
  1612. if (p_what == NOTIFICATION_RESIZED) {
  1613. call_deferred("update_transform_gizmo_view");
  1614. }
  1615. if (p_what == NOTIFICATION_PROCESS) {
  1616. real_t delta = get_process_delta_time();
  1617. if (zoom_indicator_delay > 0) {
  1618. zoom_indicator_delay -= delta;
  1619. if (zoom_indicator_delay <= 0) {
  1620. surface->update();
  1621. }
  1622. }
  1623. _update_freelook(delta);
  1624. Node *scene_root = editor->get_scene_tree_dock()->get_editor_data()->get_edited_scene_root();
  1625. if (previewing_cinema && scene_root != NULL) {
  1626. Camera *cam = scene_root->get_viewport()->get_camera();
  1627. if (cam != NULL && cam != previewing) {
  1628. //then switch the viewport's camera to the scene's viewport camera
  1629. if (previewing != NULL) {
  1630. previewing->disconnect("tree_exited", this, "_preview_exited_scene");
  1631. }
  1632. previewing = cam;
  1633. previewing->connect("tree_exited", this, "_preview_exited_scene");
  1634. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), cam->get_camera());
  1635. surface->update();
  1636. }
  1637. }
  1638. _update_camera(delta);
  1639. Map<Node *, Object *> &selection = editor_selection->get_selection();
  1640. bool changed = false;
  1641. bool exist = false;
  1642. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  1643. Spatial *sp = Object::cast_to<Spatial>(E->key());
  1644. if (!sp)
  1645. continue;
  1646. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1647. if (!se)
  1648. continue;
  1649. VisualInstance *vi = Object::cast_to<VisualInstance>(sp);
  1650. se->aabb = vi ? vi->get_aabb() : AABB(Vector3(-0.2, -0.2, -0.2), Vector3(0.4, 0.4, 0.4));
  1651. Transform t = sp->get_global_gizmo_transform();
  1652. t.translate(se->aabb.position);
  1653. // apply AABB scaling before item's global transform
  1654. Basis aabb_s;
  1655. aabb_s.scale(se->aabb.size);
  1656. t.basis = t.basis * aabb_s;
  1657. exist = true;
  1658. if (se->last_xform == t)
  1659. continue;
  1660. changed = true;
  1661. se->last_xform = t;
  1662. VisualServer::get_singleton()->instance_set_transform(se->sbox_instance, t);
  1663. }
  1664. if (changed || (spatial_editor->is_gizmo_visible() && !exist)) {
  1665. spatial_editor->update_transform_gizmo();
  1666. }
  1667. if (message_time > 0) {
  1668. if (message != last_message) {
  1669. surface->update();
  1670. last_message = message;
  1671. }
  1672. message_time -= get_physics_process_delta_time();
  1673. if (message_time < 0)
  1674. surface->update();
  1675. }
  1676. //update shadow atlas if changed
  1677. int shadowmap_size = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/size");
  1678. int atlas_q0 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_0_subdiv");
  1679. int atlas_q1 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_1_subdiv");
  1680. int atlas_q2 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_2_subdiv");
  1681. int atlas_q3 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_3_subdiv");
  1682. viewport->set_shadow_atlas_size(shadowmap_size);
  1683. viewport->set_shadow_atlas_quadrant_subdiv(0, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q0));
  1684. viewport->set_shadow_atlas_quadrant_subdiv(1, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q1));
  1685. viewport->set_shadow_atlas_quadrant_subdiv(2, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q2));
  1686. viewport->set_shadow_atlas_quadrant_subdiv(3, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q3));
  1687. bool shrink = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION));
  1688. if (shrink != (viewport_container->get_stretch_shrink() > 1)) {
  1689. viewport_container->set_stretch_shrink(shrink ? 2 : 1);
  1690. }
  1691. //update msaa if changed
  1692. int msaa_mode = ProjectSettings::get_singleton()->get("rendering/quality/filters/msaa");
  1693. viewport->set_msaa(Viewport::MSAA(msaa_mode));
  1694. bool hdr = ProjectSettings::get_singleton()->get("rendering/quality/depth/hdr");
  1695. viewport->set_hdr(hdr);
  1696. bool show_info = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  1697. info_label->set_visible(show_info);
  1698. Camera *current_camera;
  1699. if (previewing) {
  1700. current_camera = previewing;
  1701. } else {
  1702. current_camera = camera;
  1703. }
  1704. if (show_info) {
  1705. String text;
  1706. text += "X: " + rtos(current_camera->get_translation().x).pad_decimals(1) + "\n";
  1707. text += "Y: " + rtos(current_camera->get_translation().y).pad_decimals(1) + "\n";
  1708. text += "Z: " + rtos(current_camera->get_translation().z).pad_decimals(1) + "\n";
  1709. text += TTR("Pitch") + ": " + itos(Math::round(current_camera->get_rotation_degrees().x)) + "\n";
  1710. text += TTR("Yaw") + ": " + itos(Math::round(current_camera->get_rotation_degrees().y)) + "\n\n";
  1711. text += TTR("Objects Drawn") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_OBJECTS_IN_FRAME)) + "\n";
  1712. text += TTR("Material Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_MATERIAL_CHANGES_IN_FRAME)) + "\n";
  1713. text += TTR("Shader Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_SHADER_CHANGES_IN_FRAME)) + "\n";
  1714. text += TTR("Surface Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_SURFACE_CHANGES_IN_FRAME)) + "\n";
  1715. text += TTR("Draw Calls") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_DRAW_CALLS_IN_FRAME)) + "\n";
  1716. text += TTR("Vertices") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_VERTICES_IN_FRAME));
  1717. info_label->set_text(text);
  1718. }
  1719. // FPS Counter.
  1720. bool show_fps = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FPS));
  1721. fps_label->set_visible(show_fps);
  1722. if (show_fps) {
  1723. String text;
  1724. const float temp_fps = Engine::get_singleton()->get_frames_per_second();
  1725. text += TTR(vformat("FPS: %d (%s ms)", temp_fps, String::num(1000.0f / temp_fps, 2)));
  1726. fps_label->set_text(text);
  1727. }
  1728. bool show_cinema = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW));
  1729. cinema_label->set_visible(show_cinema);
  1730. if (show_cinema) {
  1731. float cinema_half_width = cinema_label->get_size().width / 2.0f;
  1732. cinema_label->set_anchor_and_margin(MARGIN_LEFT, 0.5f, -cinema_half_width);
  1733. }
  1734. if (lock_rotation) {
  1735. float locked_half_width = locked_label->get_size().width / 2.0f;
  1736. locked_label->set_anchor_and_margin(MARGIN_LEFT, 0.5f, -locked_half_width);
  1737. }
  1738. }
  1739. if (p_what == NOTIFICATION_ENTER_TREE) {
  1740. surface->connect("draw", this, "_draw");
  1741. surface->connect("gui_input", this, "_sinput");
  1742. surface->connect("mouse_entered", this, "_surface_mouse_enter");
  1743. surface->connect("mouse_exited", this, "_surface_mouse_exit");
  1744. surface->connect("focus_entered", this, "_surface_focus_enter");
  1745. surface->connect("focus_exited", this, "_surface_focus_exit");
  1746. _init_gizmo_instance(index);
  1747. }
  1748. if (p_what == NOTIFICATION_EXIT_TREE) {
  1749. _finish_gizmo_instances();
  1750. }
  1751. if (p_what == NOTIFICATION_THEME_CHANGED) {
  1752. view_menu->set_icon(get_icon("GuiMiniTabMenu", "EditorIcons"));
  1753. preview_camera->set_icon(get_icon("Camera", "EditorIcons"));
  1754. view_menu->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1755. view_menu->add_style_override("hover", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1756. view_menu->add_style_override("pressed", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1757. view_menu->add_style_override("focus", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1758. view_menu->add_style_override("disabled", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1759. preview_camera->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1760. preview_camera->add_style_override("hover", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1761. preview_camera->add_style_override("pressed", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1762. preview_camera->add_style_override("focus", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1763. preview_camera->add_style_override("disabled", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1764. info_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1765. fps_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1766. cinema_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1767. locked_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1768. }
  1769. }
  1770. static void draw_indicator_bar(Control &surface, real_t fill, Ref<Texture> icon) {
  1771. // Adjust bar size from control height
  1772. Vector2 surface_size = surface.get_size();
  1773. real_t h = surface_size.y / 2.0;
  1774. real_t y = (surface_size.y - h) / 2.0;
  1775. Rect2 r(10, y, 6, h);
  1776. real_t sy = r.size.y * fill;
  1777. // Note: because this bar appears over the viewport, it has to stay readable for any background color
  1778. // Draw both neutral dark and bright colors to account this
  1779. surface.draw_rect(r, Color(1, 1, 1, 0.2));
  1780. surface.draw_rect(Rect2(r.position.x, r.position.y + r.size.y - sy, r.size.x, sy), Color(1, 1, 1, 0.6));
  1781. surface.draw_rect(r.grow(1), Color(0, 0, 0, 0.7), false, Math::round(EDSCALE));
  1782. Vector2 icon_size = icon->get_size();
  1783. Vector2 icon_pos = Vector2(r.position.x - (icon_size.x - r.size.x) / 2, r.position.y + r.size.y + 2);
  1784. surface.draw_texture(icon, icon_pos);
  1785. }
  1786. void SpatialEditorViewport::_draw() {
  1787. EditorPluginList *over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_over();
  1788. if (!over_plugin_list->empty()) {
  1789. over_plugin_list->forward_spatial_draw_over_viewport(surface);
  1790. }
  1791. EditorPluginList *force_over_plugin_list = editor->get_editor_plugins_force_over();
  1792. if (!force_over_plugin_list->empty()) {
  1793. force_over_plugin_list->forward_spatial_force_draw_over_viewport(surface);
  1794. }
  1795. if (surface->has_focus()) {
  1796. Size2 size = surface->get_size();
  1797. Rect2 r = Rect2(Point2(), size);
  1798. get_stylebox("Focus", "EditorStyles")->draw(surface->get_canvas_item(), r);
  1799. }
  1800. if (cursor.region_select) {
  1801. const Rect2 selection_rect = Rect2(cursor.region_begin, cursor.region_end - cursor.region_begin);
  1802. surface->draw_rect(
  1803. selection_rect,
  1804. get_color("box_selection_fill_color", "Editor"));
  1805. surface->draw_rect(
  1806. selection_rect,
  1807. get_color("box_selection_stroke_color", "Editor"),
  1808. false,
  1809. Math::round(EDSCALE));
  1810. }
  1811. RID ci = surface->get_canvas_item();
  1812. if (message_time > 0) {
  1813. Ref<Font> font = get_font("font", "Label");
  1814. Point2 msgpos = Point2(5, get_size().y - 20);
  1815. font->draw(ci, msgpos + Point2(1, 1), message, Color(0, 0, 0, 0.8));
  1816. font->draw(ci, msgpos + Point2(-1, -1), message, Color(0, 0, 0, 0.8));
  1817. font->draw(ci, msgpos, message, Color(1, 1, 1, 1));
  1818. }
  1819. if (_edit.mode == TRANSFORM_ROTATE) {
  1820. Point2 center = _point_to_screen(_edit.center);
  1821. VisualServer::get_singleton()->canvas_item_add_line(
  1822. ci,
  1823. _edit.mouse_pos,
  1824. center,
  1825. get_color("accent_color", "Editor") * Color(1, 1, 1, 0.6),
  1826. Math::round(2 * EDSCALE),
  1827. true);
  1828. }
  1829. if (previewing) {
  1830. Size2 ss = Size2(ProjectSettings::get_singleton()->get("display/window/size/width"), ProjectSettings::get_singleton()->get("display/window/size/height"));
  1831. float aspect = ss.aspect();
  1832. Size2 s = get_size();
  1833. Rect2 draw_rect;
  1834. switch (previewing->get_keep_aspect_mode()) {
  1835. case Camera::KEEP_WIDTH: {
  1836. draw_rect.size = Size2(s.width, s.width / aspect);
  1837. draw_rect.position.x = 0;
  1838. draw_rect.position.y = (s.height - draw_rect.size.y) * 0.5;
  1839. } break;
  1840. case Camera::KEEP_HEIGHT: {
  1841. draw_rect.size = Size2(s.height * aspect, s.height);
  1842. draw_rect.position.y = 0;
  1843. draw_rect.position.x = (s.width - draw_rect.size.x) * 0.5;
  1844. } break;
  1845. }
  1846. draw_rect = Rect2(Vector2(), s).clip(draw_rect);
  1847. surface->draw_rect(draw_rect, Color(0.6, 0.6, 0.1, 0.5), false, Math::round(2 * EDSCALE));
  1848. } else {
  1849. if (zoom_indicator_delay > 0.0) {
  1850. if (is_freelook_active()) {
  1851. // Show speed
  1852. real_t min_speed = FREELOOK_MIN_SPEED;
  1853. real_t max_speed = camera->get_zfar();
  1854. real_t scale_length = (max_speed - min_speed);
  1855. if (!Math::is_zero_approx(scale_length)) {
  1856. real_t logscale_t = 1.0 - Math::log(1 + freelook_speed - min_speed) / Math::log(1 + scale_length);
  1857. // There is no real maximum speed so that factor can become negative,
  1858. // Let's make it look asymptotic instead (will decrease slower and slower).
  1859. if (logscale_t < 0.25)
  1860. logscale_t = 0.25 * Math::exp(4.0 * logscale_t - 1.0);
  1861. draw_indicator_bar(*surface, 1.0 - logscale_t, get_icon("ViewportSpeed", "EditorIcons"));
  1862. }
  1863. } else {
  1864. // Show zoom
  1865. real_t min_distance = ZOOM_MIN_DISTANCE; // TODO Why not pick znear to limit zoom?
  1866. real_t max_distance = camera->get_zfar();
  1867. real_t scale_length = (max_distance - min_distance);
  1868. if (!Math::is_zero_approx(scale_length)) {
  1869. real_t logscale_t = 1.0 - Math::log(1 + cursor.distance - min_distance) / Math::log(1 + scale_length);
  1870. // There is no real maximum distance so that factor can become negative,
  1871. // Let's make it look asymptotic instead (will decrease slower and slower).
  1872. if (logscale_t < 0.25)
  1873. logscale_t = 0.25 * Math::exp(4.0 * logscale_t - 1.0);
  1874. draw_indicator_bar(*surface, logscale_t, get_icon("ViewportZoom", "EditorIcons"));
  1875. }
  1876. }
  1877. }
  1878. }
  1879. }
  1880. void SpatialEditorViewport::_menu_option(int p_option) {
  1881. switch (p_option) {
  1882. case VIEW_TOP: {
  1883. cursor.y_rot = 0;
  1884. cursor.x_rot = Math_PI / 2.0;
  1885. set_message(TTR("Top View."), 2);
  1886. name = TTR("Top");
  1887. _update_name();
  1888. } break;
  1889. case VIEW_BOTTOM: {
  1890. cursor.y_rot = 0;
  1891. cursor.x_rot = -Math_PI / 2.0;
  1892. set_message(TTR("Bottom View."), 2);
  1893. name = TTR("Bottom");
  1894. _update_name();
  1895. } break;
  1896. case VIEW_LEFT: {
  1897. cursor.x_rot = 0;
  1898. cursor.y_rot = Math_PI / 2.0;
  1899. set_message(TTR("Left View."), 2);
  1900. name = TTR("Left");
  1901. _update_name();
  1902. } break;
  1903. case VIEW_RIGHT: {
  1904. cursor.x_rot = 0;
  1905. cursor.y_rot = -Math_PI / 2.0;
  1906. set_message(TTR("Right View."), 2);
  1907. name = TTR("Right");
  1908. _update_name();
  1909. } break;
  1910. case VIEW_FRONT: {
  1911. cursor.x_rot = 0;
  1912. cursor.y_rot = 0;
  1913. set_message(TTR("Front View."), 2);
  1914. name = TTR("Front");
  1915. _update_name();
  1916. } break;
  1917. case VIEW_REAR: {
  1918. cursor.x_rot = 0;
  1919. cursor.y_rot = Math_PI;
  1920. set_message(TTR("Rear View."), 2);
  1921. name = TTR("Rear");
  1922. _update_name();
  1923. } break;
  1924. case VIEW_CENTER_TO_ORIGIN: {
  1925. cursor.pos = Vector3(0, 0, 0);
  1926. } break;
  1927. case VIEW_CENTER_TO_SELECTION: {
  1928. focus_selection();
  1929. } break;
  1930. case VIEW_ALIGN_TRANSFORM_WITH_VIEW: {
  1931. if (!get_selected_count())
  1932. break;
  1933. Transform camera_transform = camera->get_global_transform();
  1934. List<Node *> &selection = editor_selection->get_selected_node_list();
  1935. undo_redo->create_action(TTR("Align Transform with View"));
  1936. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1937. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1938. if (!sp)
  1939. continue;
  1940. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1941. if (!se)
  1942. continue;
  1943. Transform xform;
  1944. if (orthogonal) {
  1945. xform = sp->get_global_transform();
  1946. xform.basis.set_euler(camera_transform.basis.get_euler());
  1947. } else {
  1948. xform = camera_transform;
  1949. xform.scale_basis(sp->get_scale());
  1950. }
  1951. undo_redo->add_do_method(sp, "set_global_transform", xform);
  1952. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  1953. }
  1954. undo_redo->commit_action();
  1955. focus_selection();
  1956. } break;
  1957. case VIEW_ALIGN_ROTATION_WITH_VIEW: {
  1958. if (!get_selected_count())
  1959. break;
  1960. Transform camera_transform = camera->get_global_transform();
  1961. List<Node *> &selection = editor_selection->get_selected_node_list();
  1962. undo_redo->create_action(TTR("Align Rotation with View"));
  1963. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1964. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1965. if (!sp)
  1966. continue;
  1967. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1968. if (!se)
  1969. continue;
  1970. undo_redo->add_do_method(sp, "set_rotation", camera_transform.basis.get_rotation());
  1971. undo_redo->add_undo_method(sp, "set_rotation", sp->get_rotation());
  1972. }
  1973. undo_redo->commit_action();
  1974. } break;
  1975. case VIEW_ENVIRONMENT: {
  1976. int idx = view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT);
  1977. bool current = view_menu->get_popup()->is_item_checked(idx);
  1978. current = !current;
  1979. if (current) {
  1980. camera->set_environment(RES());
  1981. } else {
  1982. camera->set_environment(SpatialEditor::get_singleton()->get_viewport_environment());
  1983. }
  1984. view_menu->get_popup()->set_item_checked(idx, current);
  1985. } break;
  1986. case VIEW_PERSPECTIVE: {
  1987. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  1988. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), false);
  1989. orthogonal = false;
  1990. call_deferred("update_transform_gizmo_view");
  1991. _update_name();
  1992. } break;
  1993. case VIEW_ORTHOGONAL: {
  1994. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), false);
  1995. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), true);
  1996. orthogonal = true;
  1997. call_deferred("update_transform_gizmo_view");
  1998. _update_name();
  1999. } break;
  2000. case VIEW_LOCK_ROTATION: {
  2001. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  2002. bool current = view_menu->get_popup()->is_item_checked(idx);
  2003. lock_rotation = !current;
  2004. view_menu->get_popup()->set_item_checked(idx, !current);
  2005. if (lock_rotation) {
  2006. locked_label->show();
  2007. } else {
  2008. locked_label->hide();
  2009. }
  2010. } break;
  2011. case VIEW_AUDIO_LISTENER: {
  2012. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  2013. bool current = view_menu->get_popup()->is_item_checked(idx);
  2014. current = !current;
  2015. viewport->set_as_audio_listener(current);
  2016. view_menu->get_popup()->set_item_checked(idx, current);
  2017. } break;
  2018. case VIEW_AUDIO_DOPPLER: {
  2019. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  2020. bool current = view_menu->get_popup()->is_item_checked(idx);
  2021. current = !current;
  2022. camera->set_doppler_tracking(current ? Camera::DOPPLER_TRACKING_IDLE_STEP : Camera::DOPPLER_TRACKING_DISABLED);
  2023. view_menu->get_popup()->set_item_checked(idx, current);
  2024. } break;
  2025. case VIEW_CINEMATIC_PREVIEW: {
  2026. int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW);
  2027. bool current = view_menu->get_popup()->is_item_checked(idx);
  2028. current = !current;
  2029. view_menu->get_popup()->set_item_checked(idx, current);
  2030. previewing_cinema = true;
  2031. _toggle_cinema_preview(current);
  2032. if (current) {
  2033. preview_camera->hide();
  2034. } else {
  2035. if (previewing != NULL)
  2036. preview_camera->show();
  2037. }
  2038. } break;
  2039. case VIEW_GIZMOS: {
  2040. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  2041. bool current = view_menu->get_popup()->is_item_checked(idx);
  2042. current = !current;
  2043. if (current)
  2044. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + index)) | (1 << GIZMO_EDIT_LAYER) | (1 << GIZMO_GRID_LAYER));
  2045. else
  2046. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + index)) | (1 << GIZMO_GRID_LAYER));
  2047. view_menu->get_popup()->set_item_checked(idx, current);
  2048. } break;
  2049. case VIEW_HALF_RESOLUTION: {
  2050. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  2051. bool current = view_menu->get_popup()->is_item_checked(idx);
  2052. current = !current;
  2053. view_menu->get_popup()->set_item_checked(idx, current);
  2054. } break;
  2055. case VIEW_INFORMATION: {
  2056. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  2057. bool current = view_menu->get_popup()->is_item_checked(idx);
  2058. view_menu->get_popup()->set_item_checked(idx, !current);
  2059. } break;
  2060. case VIEW_FPS: {
  2061. int idx = view_menu->get_popup()->get_item_index(VIEW_FPS);
  2062. bool current = view_menu->get_popup()->is_item_checked(idx);
  2063. view_menu->get_popup()->set_item_checked(idx, !current);
  2064. } break;
  2065. case VIEW_DISPLAY_NORMAL: {
  2066. viewport->set_debug_draw(Viewport::DEBUG_DRAW_DISABLED);
  2067. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true);
  2068. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  2069. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  2070. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  2071. } break;
  2072. case VIEW_DISPLAY_WIREFRAME: {
  2073. viewport->set_debug_draw(Viewport::DEBUG_DRAW_WIREFRAME);
  2074. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  2075. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), true);
  2076. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  2077. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  2078. } break;
  2079. case VIEW_DISPLAY_OVERDRAW: {
  2080. viewport->set_debug_draw(Viewport::DEBUG_DRAW_OVERDRAW);
  2081. VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_OVERDRAW);
  2082. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  2083. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  2084. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), true);
  2085. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  2086. } break;
  2087. case VIEW_DISPLAY_SHADELESS: {
  2088. viewport->set_debug_draw(Viewport::DEBUG_DRAW_UNSHADED);
  2089. VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_SHADELESS);
  2090. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  2091. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  2092. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  2093. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), true);
  2094. } break;
  2095. }
  2096. }
  2097. void SpatialEditorViewport::_preview_exited_scene() {
  2098. preview_camera->set_pressed(false);
  2099. _toggle_camera_preview(false);
  2100. view_menu->show();
  2101. }
  2102. void SpatialEditorViewport::_init_gizmo_instance(int p_idx) {
  2103. uint32_t layer = 1 << (GIZMO_BASE_LAYER + p_idx);
  2104. for (int i = 0; i < 3; i++) {
  2105. move_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2106. VS::get_singleton()->instance_set_base(move_gizmo_instance[i], spatial_editor->get_move_gizmo(i)->get_rid());
  2107. VS::get_singleton()->instance_set_scenario(move_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2108. VS::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  2109. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2110. VS::get_singleton()->instance_set_layer_mask(move_gizmo_instance[i], layer);
  2111. move_plane_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2112. VS::get_singleton()->instance_set_base(move_plane_gizmo_instance[i], spatial_editor->get_move_plane_gizmo(i)->get_rid());
  2113. VS::get_singleton()->instance_set_scenario(move_plane_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2114. VS::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  2115. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_plane_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2116. VS::get_singleton()->instance_set_layer_mask(move_plane_gizmo_instance[i], layer);
  2117. rotate_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2118. VS::get_singleton()->instance_set_base(rotate_gizmo_instance[i], spatial_editor->get_rotate_gizmo(i)->get_rid());
  2119. VS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2120. VS::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  2121. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2122. VS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[i], layer);
  2123. scale_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2124. VS::get_singleton()->instance_set_base(scale_gizmo_instance[i], spatial_editor->get_scale_gizmo(i)->get_rid());
  2125. VS::get_singleton()->instance_set_scenario(scale_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2126. VS::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  2127. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2128. VS::get_singleton()->instance_set_layer_mask(scale_gizmo_instance[i], layer);
  2129. scale_plane_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2130. VS::get_singleton()->instance_set_base(scale_plane_gizmo_instance[i], spatial_editor->get_scale_plane_gizmo(i)->get_rid());
  2131. VS::get_singleton()->instance_set_scenario(scale_plane_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2132. VS::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  2133. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_plane_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2134. VS::get_singleton()->instance_set_layer_mask(scale_plane_gizmo_instance[i], layer);
  2135. }
  2136. }
  2137. void SpatialEditorViewport::_finish_gizmo_instances() {
  2138. for (int i = 0; i < 3; i++) {
  2139. VS::get_singleton()->free(move_gizmo_instance[i]);
  2140. VS::get_singleton()->free(move_plane_gizmo_instance[i]);
  2141. VS::get_singleton()->free(rotate_gizmo_instance[i]);
  2142. VS::get_singleton()->free(scale_gizmo_instance[i]);
  2143. VS::get_singleton()->free(scale_plane_gizmo_instance[i]);
  2144. }
  2145. }
  2146. void SpatialEditorViewport::_toggle_camera_preview(bool p_activate) {
  2147. ERR_FAIL_COND(p_activate && !preview);
  2148. ERR_FAIL_COND(!p_activate && !previewing);
  2149. if (!p_activate) {
  2150. previewing->disconnect("tree_exiting", this, "_preview_exited_scene");
  2151. previewing = NULL;
  2152. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  2153. if (!preview)
  2154. preview_camera->hide();
  2155. view_menu->set_disabled(false);
  2156. surface->update();
  2157. } else {
  2158. previewing = preview;
  2159. previewing->connect("tree_exiting", this, "_preview_exited_scene");
  2160. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), preview->get_camera()); //replace
  2161. view_menu->set_disabled(true);
  2162. surface->update();
  2163. }
  2164. }
  2165. void SpatialEditorViewport::_toggle_cinema_preview(bool p_activate) {
  2166. previewing_cinema = p_activate;
  2167. if (!previewing_cinema) {
  2168. if (previewing != NULL)
  2169. previewing->disconnect("tree_exited", this, "_preview_exited_scene");
  2170. previewing = NULL;
  2171. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  2172. preview_camera->set_pressed(false);
  2173. if (!preview) {
  2174. preview_camera->hide();
  2175. } else {
  2176. preview_camera->show();
  2177. }
  2178. view_menu->show();
  2179. surface->update();
  2180. }
  2181. }
  2182. void SpatialEditorViewport::_selection_result_pressed(int p_result) {
  2183. if (selection_results.size() <= p_result)
  2184. return;
  2185. clicked = selection_results[p_result].item->get_instance_id();
  2186. if (clicked) {
  2187. _select_clicked(clicked_wants_append, true);
  2188. clicked = 0;
  2189. }
  2190. }
  2191. void SpatialEditorViewport::_selection_menu_hide() {
  2192. selection_results.clear();
  2193. selection_menu->clear();
  2194. selection_menu->set_size(Vector2(0, 0));
  2195. }
  2196. void SpatialEditorViewport::set_can_preview(Camera *p_preview) {
  2197. preview = p_preview;
  2198. if (!preview_camera->is_pressed() && !previewing_cinema)
  2199. preview_camera->set_visible(p_preview);
  2200. }
  2201. void SpatialEditorViewport::update_transform_gizmo_view() {
  2202. if (!is_visible_in_tree())
  2203. return;
  2204. Transform xform = spatial_editor->get_gizmo_transform();
  2205. Transform camera_xform = camera->get_transform();
  2206. if (xform.origin.distance_squared_to(camera_xform.origin) < 0.01) {
  2207. for (int i = 0; i < 3; i++) {
  2208. VisualServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  2209. VisualServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  2210. VisualServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  2211. VisualServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  2212. VisualServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  2213. }
  2214. return;
  2215. }
  2216. Vector3 camz = -camera_xform.get_basis().get_axis(2).normalized();
  2217. Vector3 camy = -camera_xform.get_basis().get_axis(1).normalized();
  2218. Plane p(camera_xform.origin, camz);
  2219. float gizmo_d = Math::abs(p.distance_to(xform.origin));
  2220. float d0 = camera->unproject_position(camera_xform.origin + camz * gizmo_d).y;
  2221. float d1 = camera->unproject_position(camera_xform.origin + camz * gizmo_d + camy).y;
  2222. float dd = Math::abs(d0 - d1);
  2223. if (dd == 0)
  2224. dd = 0.0001;
  2225. float gizmo_size = EditorSettings::get_singleton()->get("editors/3d/manipulator_gizmo_size");
  2226. // At low viewport heights, multiply the gizmo scale based on the viewport height.
  2227. // This prevents the gizmo from growing very large and going outside the viewport.
  2228. const int viewport_base_height = 400 * MAX(1, EDSCALE);
  2229. gizmo_scale =
  2230. (gizmo_size / Math::abs(dd)) * MAX(1, EDSCALE) *
  2231. MIN(viewport_base_height, viewport_container->get_size().height) / viewport_base_height /
  2232. viewport_container->get_stretch_shrink();
  2233. Vector3 scale = Vector3(1, 1, 1) * gizmo_scale;
  2234. xform.basis.scale(scale);
  2235. for (int i = 0; i < 3; i++) {
  2236. VisualServer::get_singleton()->instance_set_transform(move_gizmo_instance[i], xform);
  2237. 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));
  2238. VisualServer::get_singleton()->instance_set_transform(move_plane_gizmo_instance[i], xform);
  2239. 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));
  2240. VisualServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[i], xform);
  2241. 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));
  2242. VisualServer::get_singleton()->instance_set_transform(scale_gizmo_instance[i], xform);
  2243. VisualServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE));
  2244. VisualServer::get_singleton()->instance_set_transform(scale_plane_gizmo_instance[i], xform);
  2245. 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));
  2246. }
  2247. }
  2248. void SpatialEditorViewport::set_state(const Dictionary &p_state) {
  2249. if (p_state.has("position"))
  2250. cursor.pos = p_state["position"];
  2251. if (p_state.has("x_rotation"))
  2252. cursor.x_rot = p_state["x_rotation"];
  2253. if (p_state.has("y_rotation"))
  2254. cursor.y_rot = p_state["y_rotation"];
  2255. if (p_state.has("distance"))
  2256. cursor.distance = p_state["distance"];
  2257. if (p_state.has("use_orthogonal")) {
  2258. bool orth = p_state["use_orthogonal"];
  2259. if (orth)
  2260. _menu_option(VIEW_ORTHOGONAL);
  2261. else
  2262. _menu_option(VIEW_PERSPECTIVE);
  2263. }
  2264. if (p_state.has("display_mode")) {
  2265. int display = p_state["display_mode"];
  2266. int idx = view_menu->get_popup()->get_item_index(display);
  2267. if (!view_menu->get_popup()->is_item_checked(idx))
  2268. _menu_option(display);
  2269. }
  2270. if (p_state.has("lock_rotation")) {
  2271. lock_rotation = p_state["lock_rotation"];
  2272. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  2273. view_menu->get_popup()->set_item_checked(idx, lock_rotation);
  2274. }
  2275. if (p_state.has("use_environment")) {
  2276. bool env = p_state["use_environment"];
  2277. if (env != camera->get_environment().is_valid())
  2278. _menu_option(VIEW_ENVIRONMENT);
  2279. }
  2280. if (p_state.has("listener")) {
  2281. bool listener = p_state["listener"];
  2282. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  2283. viewport->set_as_audio_listener(listener);
  2284. view_menu->get_popup()->set_item_checked(idx, listener);
  2285. }
  2286. if (p_state.has("doppler")) {
  2287. bool doppler = p_state["doppler"];
  2288. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  2289. camera->set_doppler_tracking(doppler ? Camera::DOPPLER_TRACKING_IDLE_STEP : Camera::DOPPLER_TRACKING_DISABLED);
  2290. view_menu->get_popup()->set_item_checked(idx, doppler);
  2291. }
  2292. if (p_state.has("gizmos")) {
  2293. bool gizmos = p_state["gizmos"];
  2294. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  2295. if (view_menu->get_popup()->is_item_checked(idx) != gizmos)
  2296. _menu_option(VIEW_GIZMOS);
  2297. }
  2298. if (p_state.has("information")) {
  2299. bool information = p_state["information"];
  2300. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  2301. if (view_menu->get_popup()->is_item_checked(idx) != information)
  2302. _menu_option(VIEW_INFORMATION);
  2303. }
  2304. if (p_state.has("fps")) {
  2305. bool fps = p_state["fps"];
  2306. int idx = view_menu->get_popup()->get_item_index(VIEW_FPS);
  2307. if (view_menu->get_popup()->is_item_checked(idx) != fps)
  2308. _menu_option(VIEW_FPS);
  2309. }
  2310. if (p_state.has("half_res")) {
  2311. bool half_res = p_state["half_res"];
  2312. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  2313. view_menu->get_popup()->set_item_checked(idx, half_res);
  2314. }
  2315. if (p_state.has("cinematic_preview")) {
  2316. previewing_cinema = p_state["cinematic_preview"];
  2317. int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW);
  2318. view_menu->get_popup()->set_item_checked(idx, previewing_cinema);
  2319. }
  2320. if (p_state.has("previewing")) {
  2321. Node *pv = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["previewing"]);
  2322. if (Object::cast_to<Camera>(pv)) {
  2323. previewing = Object::cast_to<Camera>(pv);
  2324. previewing->connect("tree_exiting", this, "_preview_exited_scene");
  2325. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), previewing->get_camera()); //replace
  2326. view_menu->set_disabled(true);
  2327. surface->update();
  2328. preview_camera->set_pressed(true);
  2329. preview_camera->show();
  2330. }
  2331. }
  2332. }
  2333. Dictionary SpatialEditorViewport::get_state() const {
  2334. Dictionary d;
  2335. d["position"] = cursor.pos;
  2336. d["x_rotation"] = cursor.x_rot;
  2337. d["y_rotation"] = cursor.y_rot;
  2338. d["distance"] = cursor.distance;
  2339. d["use_environment"] = camera->get_environment().is_valid();
  2340. d["use_orthogonal"] = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL;
  2341. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL)))
  2342. d["display_mode"] = VIEW_DISPLAY_NORMAL;
  2343. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME)))
  2344. d["display_mode"] = VIEW_DISPLAY_WIREFRAME;
  2345. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW)))
  2346. d["display_mode"] = VIEW_DISPLAY_OVERDRAW;
  2347. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS)))
  2348. d["display_mode"] = VIEW_DISPLAY_SHADELESS;
  2349. d["listener"] = viewport->is_audio_listener();
  2350. d["doppler"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER));
  2351. d["gizmos"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS));
  2352. d["information"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  2353. d["fps"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FPS));
  2354. d["half_res"] = viewport_container->get_stretch_shrink() > 1;
  2355. d["cinematic_preview"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW));
  2356. if (previewing)
  2357. d["previewing"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(previewing);
  2358. if (lock_rotation)
  2359. d["lock_rotation"] = lock_rotation;
  2360. return d;
  2361. }
  2362. void SpatialEditorViewport::_bind_methods() {
  2363. ClassDB::bind_method(D_METHOD("_draw"), &SpatialEditorViewport::_draw);
  2364. ClassDB::bind_method(D_METHOD("_surface_mouse_enter"), &SpatialEditorViewport::_surface_mouse_enter);
  2365. ClassDB::bind_method(D_METHOD("_surface_mouse_exit"), &SpatialEditorViewport::_surface_mouse_exit);
  2366. ClassDB::bind_method(D_METHOD("_surface_focus_enter"), &SpatialEditorViewport::_surface_focus_enter);
  2367. ClassDB::bind_method(D_METHOD("_surface_focus_exit"), &SpatialEditorViewport::_surface_focus_exit);
  2368. ClassDB::bind_method(D_METHOD("_sinput"), &SpatialEditorViewport::_sinput);
  2369. ClassDB::bind_method(D_METHOD("_menu_option"), &SpatialEditorViewport::_menu_option);
  2370. ClassDB::bind_method(D_METHOD("_toggle_camera_preview"), &SpatialEditorViewport::_toggle_camera_preview);
  2371. ClassDB::bind_method(D_METHOD("_preview_exited_scene"), &SpatialEditorViewport::_preview_exited_scene);
  2372. ClassDB::bind_method(D_METHOD("update_transform_gizmo_view"), &SpatialEditorViewport::update_transform_gizmo_view);
  2373. ClassDB::bind_method(D_METHOD("_selection_result_pressed"), &SpatialEditorViewport::_selection_result_pressed);
  2374. ClassDB::bind_method(D_METHOD("_selection_menu_hide"), &SpatialEditorViewport::_selection_menu_hide);
  2375. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SpatialEditorViewport::can_drop_data_fw);
  2376. ClassDB::bind_method(D_METHOD("drop_data_fw"), &SpatialEditorViewport::drop_data_fw);
  2377. ADD_SIGNAL(MethodInfo("toggle_maximize_view", PropertyInfo(Variant::OBJECT, "viewport")));
  2378. }
  2379. void SpatialEditorViewport::reset() {
  2380. orthogonal = false;
  2381. lock_rotation = false;
  2382. message_time = 0;
  2383. message = "";
  2384. last_message = "";
  2385. name = "";
  2386. cursor.x_rot = 0.5;
  2387. cursor.y_rot = 0.5;
  2388. cursor.distance = 4;
  2389. cursor.region_select = false;
  2390. cursor.pos = Vector3();
  2391. _update_name();
  2392. }
  2393. void SpatialEditorViewport::focus_selection() {
  2394. if (!get_selected_count())
  2395. return;
  2396. Vector3 center;
  2397. int count = 0;
  2398. List<Node *> &selection = editor_selection->get_selected_node_list();
  2399. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2400. Spatial *sp = Object::cast_to<Spatial>(E->get());
  2401. if (!sp)
  2402. continue;
  2403. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  2404. if (!se)
  2405. continue;
  2406. center += sp->get_global_gizmo_transform().origin;
  2407. count++;
  2408. }
  2409. if (count != 0) {
  2410. center /= float(count);
  2411. }
  2412. cursor.pos = center;
  2413. }
  2414. void SpatialEditorViewport::assign_pending_data_pointers(Spatial *p_preview_node, AABB *p_preview_bounds, AcceptDialog *p_accept) {
  2415. preview_node = p_preview_node;
  2416. preview_bounds = p_preview_bounds;
  2417. accept = p_accept;
  2418. }
  2419. Vector3 SpatialEditorViewport::_get_instance_position(const Point2 &p_pos) const {
  2420. const float MAX_DISTANCE = 10;
  2421. Vector3 world_ray = _get_ray(p_pos);
  2422. Vector3 world_pos = _get_ray_pos(p_pos);
  2423. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(world_pos, world_ray, get_tree()->get_root()->get_world()->get_scenario());
  2424. Set<Ref<EditorSpatialGizmo> > found_gizmos;
  2425. float closest_dist = MAX_DISTANCE;
  2426. Vector3 point = world_pos + world_ray * MAX_DISTANCE;
  2427. Vector3 normal = Vector3(0.0, 0.0, 0.0);
  2428. for (int i = 0; i < instances.size(); i++) {
  2429. MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(ObjectDB::get_instance(instances[i]));
  2430. if (!mesh_instance)
  2431. continue;
  2432. Ref<EditorSpatialGizmo> seg = mesh_instance->get_gizmo();
  2433. if ((!seg.is_valid()) || found_gizmos.has(seg)) {
  2434. continue;
  2435. }
  2436. found_gizmos.insert(seg);
  2437. Vector3 hit_point;
  2438. Vector3 hit_normal;
  2439. bool inters = seg->intersect_ray(camera, p_pos, hit_point, hit_normal, NULL, false);
  2440. if (!inters)
  2441. continue;
  2442. float dist = world_pos.distance_to(hit_point);
  2443. if (dist < 0)
  2444. continue;
  2445. if (dist < closest_dist) {
  2446. closest_dist = dist;
  2447. point = hit_point;
  2448. normal = hit_normal;
  2449. }
  2450. }
  2451. Vector3 offset = Vector3();
  2452. for (int i = 0; i < 3; i++) {
  2453. if (normal[i] > 0.0)
  2454. offset[i] = (preview_bounds->get_size()[i] - (preview_bounds->get_size()[i] + preview_bounds->get_position()[i]));
  2455. else if (normal[i] < 0.0)
  2456. offset[i] = -(preview_bounds->get_size()[i] + preview_bounds->get_position()[i]);
  2457. }
  2458. return point + offset;
  2459. }
  2460. AABB SpatialEditorViewport::_calculate_spatial_bounds(const Spatial *p_parent, const AABB &p_bounds) {
  2461. AABB bounds = p_bounds;
  2462. for (int i = 0; i < p_parent->get_child_count(); i++) {
  2463. Spatial *child = Object::cast_to<Spatial>(p_parent->get_child(i));
  2464. if (child) {
  2465. MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(child);
  2466. if (mesh_instance) {
  2467. AABB mesh_instance_bounds = mesh_instance->get_aabb();
  2468. mesh_instance_bounds.position += mesh_instance->get_global_gizmo_transform().origin - p_parent->get_global_gizmo_transform().origin;
  2469. bounds.merge_with(mesh_instance_bounds);
  2470. }
  2471. bounds = _calculate_spatial_bounds(child, bounds);
  2472. }
  2473. }
  2474. return bounds;
  2475. }
  2476. void SpatialEditorViewport::_create_preview(const Vector<String> &files) const {
  2477. for (int i = 0; i < files.size(); i++) {
  2478. String path = files[i];
  2479. RES res = ResourceLoader::load(path);
  2480. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  2481. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  2482. if (mesh != NULL || scene != NULL) {
  2483. if (mesh != NULL) {
  2484. MeshInstance *mesh_instance = memnew(MeshInstance);
  2485. mesh_instance->set_mesh(mesh);
  2486. preview_node->add_child(mesh_instance);
  2487. } else {
  2488. if (scene.is_valid()) {
  2489. Node *instance = scene->instance();
  2490. if (instance) {
  2491. preview_node->add_child(instance);
  2492. }
  2493. }
  2494. }
  2495. editor->get_scene_root()->add_child(preview_node);
  2496. }
  2497. }
  2498. *preview_bounds = _calculate_spatial_bounds(preview_node, AABB());
  2499. }
  2500. void SpatialEditorViewport::_remove_preview() {
  2501. if (preview_node->get_parent()) {
  2502. for (int i = preview_node->get_child_count() - 1; i >= 0; i--) {
  2503. Node *node = preview_node->get_child(i);
  2504. node->queue_delete();
  2505. preview_node->remove_child(node);
  2506. }
  2507. editor->get_scene_root()->remove_child(preview_node);
  2508. }
  2509. }
  2510. bool SpatialEditorViewport::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
  2511. if (p_desired_node->get_filename() == p_target_scene_path) {
  2512. return true;
  2513. }
  2514. int childCount = p_desired_node->get_child_count();
  2515. for (int i = 0; i < childCount; i++) {
  2516. Node *child = p_desired_node->get_child(i);
  2517. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  2518. return true;
  2519. }
  2520. }
  2521. return false;
  2522. }
  2523. bool SpatialEditorViewport::_create_instance(Node *parent, String &path, const Point2 &p_point) {
  2524. RES res = ResourceLoader::load(path);
  2525. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  2526. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  2527. Node *instanced_scene = NULL;
  2528. if (mesh != NULL || scene != NULL) {
  2529. if (mesh != NULL) {
  2530. MeshInstance *mesh_instance = memnew(MeshInstance);
  2531. mesh_instance->set_mesh(mesh);
  2532. mesh_instance->set_name(path.get_file().get_basename());
  2533. instanced_scene = mesh_instance;
  2534. } else {
  2535. if (!scene.is_valid()) { // invalid scene
  2536. return false;
  2537. } else {
  2538. instanced_scene = scene->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  2539. }
  2540. }
  2541. }
  2542. if (instanced_scene == NULL) {
  2543. return false;
  2544. }
  2545. if (editor->get_edited_scene()->get_filename() != "") { // cyclical instancing
  2546. if (_cyclical_dependency_exists(editor->get_edited_scene()->get_filename(), instanced_scene)) {
  2547. memdelete(instanced_scene);
  2548. return false;
  2549. }
  2550. }
  2551. if (scene != NULL) {
  2552. instanced_scene->set_filename(ProjectSettings::get_singleton()->localize_path(path));
  2553. }
  2554. editor_data->get_undo_redo().add_do_method(parent, "add_child", instanced_scene);
  2555. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_owner", editor->get_edited_scene());
  2556. editor_data->get_undo_redo().add_do_reference(instanced_scene);
  2557. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instanced_scene);
  2558. String new_name = parent->validate_child_name(instanced_scene);
  2559. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  2560. editor_data->get_undo_redo().add_do_method(sed, "live_debug_instance_node", editor->get_edited_scene()->get_path_to(parent), path, new_name);
  2561. 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));
  2562. Transform global_transform;
  2563. Spatial *parent_spatial = Object::cast_to<Spatial>(parent);
  2564. if (parent_spatial)
  2565. global_transform = parent_spatial->get_global_gizmo_transform();
  2566. global_transform.origin = spatial_editor->snap_point(_get_instance_position(p_point));
  2567. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_global_transform", global_transform);
  2568. return true;
  2569. }
  2570. void SpatialEditorViewport::_perform_drop_data() {
  2571. _remove_preview();
  2572. Vector<String> error_files;
  2573. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  2574. for (int i = 0; i < selected_files.size(); i++) {
  2575. String path = selected_files[i];
  2576. RES res = ResourceLoader::load(path);
  2577. if (res.is_null()) {
  2578. continue;
  2579. }
  2580. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  2581. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  2582. if (mesh != NULL || scene != NULL) {
  2583. bool success = _create_instance(target_node, path, drop_pos);
  2584. if (!success) {
  2585. error_files.push_back(path);
  2586. }
  2587. }
  2588. }
  2589. editor_data->get_undo_redo().commit_action();
  2590. if (error_files.size() > 0) {
  2591. String files_str;
  2592. for (int i = 0; i < error_files.size(); i++) {
  2593. files_str += error_files[i].get_file().get_basename() + ",";
  2594. }
  2595. files_str = files_str.substr(0, files_str.length() - 1);
  2596. accept->set_text(vformat(TTR("Error instancing scene from %s"), files_str.c_str()));
  2597. accept->popup_centered_minsize();
  2598. }
  2599. }
  2600. bool SpatialEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2601. bool can_instance = false;
  2602. if (!preview_node->is_inside_tree()) {
  2603. Dictionary d = p_data;
  2604. if (d.has("type") && (String(d["type"]) == "files")) {
  2605. Vector<String> files = d["files"];
  2606. List<String> scene_extensions;
  2607. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  2608. List<String> mesh_extensions;
  2609. ResourceLoader::get_recognized_extensions_for_type("Mesh", &mesh_extensions);
  2610. for (int i = 0; i < files.size(); i++) {
  2611. if (mesh_extensions.find(files[i].get_extension()) || scene_extensions.find(files[i].get_extension())) {
  2612. RES res = ResourceLoader::load(files[i]);
  2613. if (res.is_null()) {
  2614. continue;
  2615. }
  2616. String type = res->get_class();
  2617. if (type == "PackedScene") {
  2618. Ref<PackedScene> sdata = ResourceLoader::load(files[i]);
  2619. Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  2620. if (!instanced_scene) {
  2621. continue;
  2622. }
  2623. memdelete(instanced_scene);
  2624. } else if (type == "Mesh" || type == "ArrayMesh" || type == "PrimitiveMesh") {
  2625. Ref<Mesh> mesh = ResourceLoader::load(files[i]);
  2626. if (!mesh.is_valid()) {
  2627. continue;
  2628. }
  2629. } else {
  2630. continue;
  2631. }
  2632. can_instance = true;
  2633. break;
  2634. }
  2635. }
  2636. if (can_instance) {
  2637. _create_preview(files);
  2638. }
  2639. }
  2640. } else {
  2641. can_instance = true;
  2642. }
  2643. if (can_instance) {
  2644. Transform global_transform = Transform(Basis(), _get_instance_position(p_point));
  2645. preview_node->set_global_transform(global_transform);
  2646. }
  2647. return can_instance;
  2648. }
  2649. void SpatialEditorViewport::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2650. if (!can_drop_data_fw(p_point, p_data, p_from))
  2651. return;
  2652. bool is_shift = Input::get_singleton()->is_key_pressed(KEY_SHIFT);
  2653. selected_files.clear();
  2654. Dictionary d = p_data;
  2655. if (d.has("type") && String(d["type"]) == "files") {
  2656. selected_files = d["files"];
  2657. }
  2658. List<Node *> list = editor->get_editor_selection()->get_selected_node_list();
  2659. if (list.size() == 0) {
  2660. Node *root_node = editor->get_edited_scene();
  2661. if (root_node) {
  2662. list.push_back(root_node);
  2663. } else {
  2664. accept->set_text(TTR("No parent to instance a child at."));
  2665. accept->popup_centered_minsize();
  2666. _remove_preview();
  2667. return;
  2668. }
  2669. }
  2670. if (list.size() != 1) {
  2671. accept->set_text(TTR("This operation requires a single selected node."));
  2672. accept->popup_centered_minsize();
  2673. _remove_preview();
  2674. return;
  2675. }
  2676. target_node = list[0];
  2677. if (is_shift && target_node != editor->get_edited_scene()) {
  2678. target_node = target_node->get_parent();
  2679. }
  2680. drop_pos = p_point;
  2681. _perform_drop_data();
  2682. }
  2683. SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, EditorNode *p_editor, int p_index) {
  2684. _edit.mode = TRANSFORM_NONE;
  2685. _edit.plane = TRANSFORM_VIEW;
  2686. _edit.edited_gizmo = 0;
  2687. _edit.snap = 1;
  2688. _edit.gizmo_handle = 0;
  2689. index = p_index;
  2690. editor = p_editor;
  2691. editor_data = editor->get_scene_tree_dock()->get_editor_data();
  2692. editor_selection = editor->get_editor_selection();
  2693. undo_redo = editor->get_undo_redo();
  2694. clicked = 0;
  2695. clicked_includes_current = false;
  2696. orthogonal = false;
  2697. lock_rotation = false;
  2698. message_time = 0;
  2699. zoom_indicator_delay = 0.0;
  2700. spatial_editor = p_spatial_editor;
  2701. ViewportContainer *c = memnew(ViewportContainer);
  2702. viewport_container = c;
  2703. c->set_stretch(true);
  2704. add_child(c);
  2705. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2706. viewport = memnew(Viewport);
  2707. viewport->set_disable_input(true);
  2708. c->add_child(viewport);
  2709. surface = memnew(Control);
  2710. surface->set_drag_forwarding(this);
  2711. add_child(surface);
  2712. surface->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2713. surface->set_clip_contents(true);
  2714. camera = memnew(Camera);
  2715. camera->set_disable_gizmo(true);
  2716. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + p_index)) | (1 << GIZMO_EDIT_LAYER) | (1 << GIZMO_GRID_LAYER));
  2717. viewport->add_child(camera);
  2718. camera->make_current();
  2719. surface->set_focus_mode(FOCUS_ALL);
  2720. VBoxContainer *vbox = memnew(VBoxContainer);
  2721. surface->add_child(vbox);
  2722. vbox->set_position(Point2(10, 10) * EDSCALE);
  2723. view_menu = memnew(MenuButton);
  2724. view_menu->set_flat(false);
  2725. vbox->add_child(view_menu);
  2726. view_menu->set_h_size_flags(0);
  2727. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/top_view"), VIEW_TOP);
  2728. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/bottom_view"), VIEW_BOTTOM);
  2729. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/left_view"), VIEW_LEFT);
  2730. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/right_view"), VIEW_RIGHT);
  2731. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/front_view"), VIEW_FRONT);
  2732. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/rear_view"), VIEW_REAR);
  2733. view_menu->get_popup()->add_separator();
  2734. view_menu->get_popup()->add_radio_check_item(TTR("Perspective") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_PERSPECTIVE);
  2735. view_menu->get_popup()->add_radio_check_item(TTR("Orthogonal") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_ORTHOGONAL);
  2736. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  2737. view_menu->get_popup()->add_separator();
  2738. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_lock_rotation", TTR("Lock View Rotation")), VIEW_LOCK_ROTATION);
  2739. view_menu->get_popup()->add_separator();
  2740. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_normal", TTR("Display Normal")), VIEW_DISPLAY_NORMAL);
  2741. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_wireframe", TTR("Display Wireframe")), VIEW_DISPLAY_WIREFRAME);
  2742. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_overdraw", TTR("Display Overdraw")), VIEW_DISPLAY_OVERDRAW);
  2743. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_unshaded", TTR("Display Unshaded")), VIEW_DISPLAY_SHADELESS);
  2744. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true);
  2745. view_menu->get_popup()->add_separator();
  2746. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment", TTR("View Environment")), VIEW_ENVIRONMENT);
  2747. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos", TTR("View Gizmos")), VIEW_GIZMOS);
  2748. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_information", TTR("View Information")), VIEW_INFORMATION);
  2749. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_fps", TTR("View FPS")), VIEW_FPS);
  2750. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT), true);
  2751. view_menu->get_popup()->add_separator();
  2752. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_half_resolution", TTR("Half Resolution")), VIEW_HALF_RESOLUTION);
  2753. view_menu->get_popup()->add_separator();
  2754. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_listener", TTR("Audio Listener")), VIEW_AUDIO_LISTENER);
  2755. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_doppler", TTR("Enable Doppler")), VIEW_AUDIO_DOPPLER);
  2756. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS), true);
  2757. view_menu->get_popup()->add_separator();
  2758. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_cinematic_preview", TTR("Cinematic Preview")), VIEW_CINEMATIC_PREVIEW);
  2759. view_menu->get_popup()->add_separator();
  2760. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_origin"), VIEW_CENTER_TO_ORIGIN);
  2761. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_selection"), VIEW_CENTER_TO_SELECTION);
  2762. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_transform_with_view"), VIEW_ALIGN_TRANSFORM_WITH_VIEW);
  2763. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_rotation_with_view"), VIEW_ALIGN_ROTATION_WITH_VIEW);
  2764. view_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2765. view_menu->set_disable_shortcuts(true);
  2766. ED_SHORTCUT("spatial_editor/freelook_left", TTR("Freelook Left"), KEY_A);
  2767. ED_SHORTCUT("spatial_editor/freelook_right", TTR("Freelook Right"), KEY_D);
  2768. ED_SHORTCUT("spatial_editor/freelook_forward", TTR("Freelook Forward"), KEY_W);
  2769. ED_SHORTCUT("spatial_editor/freelook_backwards", TTR("Freelook Backwards"), KEY_S);
  2770. ED_SHORTCUT("spatial_editor/freelook_up", TTR("Freelook Up"), KEY_E);
  2771. ED_SHORTCUT("spatial_editor/freelook_down", TTR("Freelook Down"), KEY_Q);
  2772. ED_SHORTCUT("spatial_editor/freelook_speed_modifier", TTR("Freelook Speed Modifier"), KEY_SHIFT);
  2773. preview_camera = memnew(CheckBox);
  2774. preview_camera->set_text(TTR("Preview"));
  2775. vbox->add_child(preview_camera);
  2776. preview_camera->set_h_size_flags(0);
  2777. preview_camera->hide();
  2778. preview_camera->connect("toggled", this, "_toggle_camera_preview");
  2779. previewing = NULL;
  2780. gizmo_scale = 1.0;
  2781. preview_node = NULL;
  2782. info_label = memnew(Label);
  2783. info_label->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -90 * EDSCALE);
  2784. info_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -90 * EDSCALE);
  2785. info_label->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -10 * EDSCALE);
  2786. info_label->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  2787. info_label->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  2788. info_label->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  2789. surface->add_child(info_label);
  2790. info_label->hide();
  2791. fps_label = memnew(Label);
  2792. fps_label->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -90 * EDSCALE);
  2793. fps_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  2794. fps_label->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -10 * EDSCALE);
  2795. fps_label->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  2796. 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."));
  2797. fps_label->set_mouse_filter(MOUSE_FILTER_PASS); // Otherwise tooltip doesn't show.
  2798. surface->add_child(fps_label);
  2799. fps_label->hide();
  2800. cinema_label = memnew(Label);
  2801. cinema_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  2802. cinema_label->set_h_grow_direction(GROW_DIRECTION_END);
  2803. cinema_label->set_align(Label::ALIGN_CENTER);
  2804. surface->add_child(cinema_label);
  2805. cinema_label->set_text(TTR("Cinematic Preview"));
  2806. cinema_label->hide();
  2807. previewing_cinema = false;
  2808. locked_label = memnew(Label);
  2809. locked_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -20 * EDSCALE);
  2810. locked_label->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  2811. locked_label->set_h_grow_direction(GROW_DIRECTION_END);
  2812. locked_label->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  2813. locked_label->set_align(Label::ALIGN_CENTER);
  2814. surface->add_child(locked_label);
  2815. locked_label->set_text(TTR("View Rotation Locked"));
  2816. locked_label->hide();
  2817. accept = NULL;
  2818. freelook_active = false;
  2819. freelook_speed = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_base_speed");
  2820. selection_menu = memnew(PopupMenu);
  2821. add_child(selection_menu);
  2822. selection_menu->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  2823. selection_menu->connect("id_pressed", this, "_selection_result_pressed");
  2824. selection_menu->connect("popup_hide", this, "_selection_menu_hide");
  2825. if (p_index == 0) {
  2826. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER), true);
  2827. viewport->set_as_audio_listener(true);
  2828. }
  2829. name = "";
  2830. _update_name();
  2831. EditorSettings::get_singleton()->connect("settings_changed", this, "update_transform_gizmo_view");
  2832. }
  2833. //////////////////////////////////////////////////////////////
  2834. void SpatialEditorViewportContainer::_gui_input(const Ref<InputEvent> &p_event) {
  2835. Ref<InputEventMouseButton> mb = p_event;
  2836. if (mb.is_valid() && mb->get_button_index() == BUTTON_LEFT) {
  2837. if (mb->is_pressed()) {
  2838. Vector2 size = get_size();
  2839. int h_sep = get_constant("separation", "HSplitContainer");
  2840. int v_sep = get_constant("separation", "VSplitContainer");
  2841. int mid_w = size.width * ratio_h;
  2842. int mid_h = size.height * ratio_v;
  2843. dragging_h = mb->get_position().x > (mid_w - h_sep / 2) && mb->get_position().x < (mid_w + h_sep / 2);
  2844. dragging_v = mb->get_position().y > (mid_h - v_sep / 2) && mb->get_position().y < (mid_h + v_sep / 2);
  2845. drag_begin_pos = mb->get_position();
  2846. drag_begin_ratio.x = ratio_h;
  2847. drag_begin_ratio.y = ratio_v;
  2848. switch (view) {
  2849. case VIEW_USE_1_VIEWPORT: {
  2850. dragging_h = false;
  2851. dragging_v = false;
  2852. } break;
  2853. case VIEW_USE_2_VIEWPORTS: {
  2854. dragging_h = false;
  2855. } break;
  2856. case VIEW_USE_2_VIEWPORTS_ALT: {
  2857. dragging_v = false;
  2858. } break;
  2859. case VIEW_USE_3_VIEWPORTS:
  2860. case VIEW_USE_3_VIEWPORTS_ALT:
  2861. case VIEW_USE_4_VIEWPORTS: {
  2862. // Do nothing.
  2863. } break;
  2864. }
  2865. } else {
  2866. dragging_h = false;
  2867. dragging_v = false;
  2868. }
  2869. }
  2870. Ref<InputEventMouseMotion> mm = p_event;
  2871. if (mm.is_valid()) {
  2872. if (view == VIEW_USE_3_VIEWPORTS || view == VIEW_USE_3_VIEWPORTS_ALT || view == VIEW_USE_4_VIEWPORTS) {
  2873. Vector2 size = get_size();
  2874. int h_sep = get_constant("separation", "HSplitContainer");
  2875. int v_sep = get_constant("separation", "VSplitContainer");
  2876. int mid_w = size.width * ratio_h;
  2877. int mid_h = size.height * ratio_v;
  2878. bool was_hovering_h = hovering_h;
  2879. bool was_hovering_v = hovering_v;
  2880. hovering_h = mm->get_position().x > (mid_w - h_sep / 2) && mm->get_position().x < (mid_w + h_sep / 2);
  2881. hovering_v = mm->get_position().y > (mid_h - v_sep / 2) && mm->get_position().y < (mid_h + v_sep / 2);
  2882. if (was_hovering_h != hovering_h || was_hovering_v != hovering_v) {
  2883. update();
  2884. }
  2885. }
  2886. if (dragging_h) {
  2887. float new_ratio = drag_begin_ratio.x + (mm->get_position().x - drag_begin_pos.x) / get_size().width;
  2888. new_ratio = CLAMP(new_ratio, 40 / get_size().width, (get_size().width - 40) / get_size().width);
  2889. ratio_h = new_ratio;
  2890. queue_sort();
  2891. update();
  2892. }
  2893. if (dragging_v) {
  2894. float new_ratio = drag_begin_ratio.y + (mm->get_position().y - drag_begin_pos.y) / get_size().height;
  2895. new_ratio = CLAMP(new_ratio, 40 / get_size().height, (get_size().height - 40) / get_size().height);
  2896. ratio_v = new_ratio;
  2897. queue_sort();
  2898. update();
  2899. }
  2900. }
  2901. }
  2902. void SpatialEditorViewportContainer::_notification(int p_what) {
  2903. if (p_what == NOTIFICATION_MOUSE_ENTER || p_what == NOTIFICATION_MOUSE_EXIT) {
  2904. mouseover = (p_what == NOTIFICATION_MOUSE_ENTER);
  2905. update();
  2906. }
  2907. if (p_what == NOTIFICATION_DRAW && mouseover) {
  2908. Ref<Texture> h_grabber = get_icon("grabber", "HSplitContainer");
  2909. Ref<Texture> v_grabber = get_icon("grabber", "VSplitContainer");
  2910. Ref<Texture> hdiag_grabber = get_icon("GuiViewportHdiagsplitter", "EditorIcons");
  2911. Ref<Texture> vdiag_grabber = get_icon("GuiViewportVdiagsplitter", "EditorIcons");
  2912. Ref<Texture> vh_grabber = get_icon("GuiViewportVhsplitter", "EditorIcons");
  2913. Vector2 size = get_size();
  2914. int h_sep = get_constant("separation", "HSplitContainer");
  2915. int v_sep = get_constant("separation", "VSplitContainer");
  2916. int mid_w = size.width * ratio_h;
  2917. int mid_h = size.height * ratio_v;
  2918. int size_left = mid_w - h_sep / 2;
  2919. int size_bottom = size.height - mid_h - v_sep / 2;
  2920. switch (view) {
  2921. case VIEW_USE_1_VIEWPORT: {
  2922. // Nothing to show.
  2923. } break;
  2924. case VIEW_USE_2_VIEWPORTS: {
  2925. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  2926. set_default_cursor_shape(CURSOR_VSPLIT);
  2927. } break;
  2928. case VIEW_USE_2_VIEWPORTS_ALT: {
  2929. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  2930. set_default_cursor_shape(CURSOR_HSPLIT);
  2931. } break;
  2932. case VIEW_USE_3_VIEWPORTS: {
  2933. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  2934. draw_texture(hdiag_grabber, Vector2(mid_w - hdiag_grabber->get_width() / 2, mid_h - v_grabber->get_height() / 4));
  2935. set_default_cursor_shape(CURSOR_DRAG);
  2936. } else if ((hovering_v && !dragging_h) || dragging_v) {
  2937. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  2938. set_default_cursor_shape(CURSOR_VSPLIT);
  2939. } else if (hovering_h || dragging_h) {
  2940. 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));
  2941. set_default_cursor_shape(CURSOR_HSPLIT);
  2942. }
  2943. } break;
  2944. case VIEW_USE_3_VIEWPORTS_ALT: {
  2945. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  2946. draw_texture(vdiag_grabber, Vector2(mid_w - vdiag_grabber->get_width() + v_grabber->get_height() / 4, mid_h - vdiag_grabber->get_height() / 2));
  2947. set_default_cursor_shape(CURSOR_DRAG);
  2948. } else if ((hovering_v && !dragging_h) || dragging_v) {
  2949. draw_texture(v_grabber, Vector2((size_left - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  2950. set_default_cursor_shape(CURSOR_VSPLIT);
  2951. } else if (hovering_h || dragging_h) {
  2952. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  2953. set_default_cursor_shape(CURSOR_HSPLIT);
  2954. }
  2955. } break;
  2956. case VIEW_USE_4_VIEWPORTS: {
  2957. Vector2 half(mid_w, mid_h);
  2958. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  2959. draw_texture(vh_grabber, half - vh_grabber->get_size() / 2.0);
  2960. set_default_cursor_shape(CURSOR_DRAG);
  2961. } else if ((hovering_v && !dragging_h) || dragging_v) {
  2962. draw_texture(v_grabber, half - v_grabber->get_size() / 2.0);
  2963. set_default_cursor_shape(CURSOR_VSPLIT);
  2964. } else if (hovering_h || dragging_h) {
  2965. draw_texture(h_grabber, half - h_grabber->get_size() / 2.0);
  2966. set_default_cursor_shape(CURSOR_HSPLIT);
  2967. }
  2968. } break;
  2969. }
  2970. }
  2971. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  2972. SpatialEditorViewport *viewports[4];
  2973. int vc = 0;
  2974. for (int i = 0; i < get_child_count(); i++) {
  2975. viewports[vc] = Object::cast_to<SpatialEditorViewport>(get_child(i));
  2976. if (viewports[vc]) {
  2977. vc++;
  2978. }
  2979. }
  2980. ERR_FAIL_COND(vc != 4);
  2981. Size2 size = get_size();
  2982. if (size.x < 10 || size.y < 10) {
  2983. for (int i = 0; i < 4; i++) {
  2984. viewports[i]->hide();
  2985. }
  2986. return;
  2987. }
  2988. int h_sep = get_constant("separation", "HSplitContainer");
  2989. int v_sep = get_constant("separation", "VSplitContainer");
  2990. int mid_w = size.width * ratio_h;
  2991. int mid_h = size.height * ratio_v;
  2992. int size_left = mid_w - h_sep / 2;
  2993. int size_right = size.width - mid_w - h_sep / 2;
  2994. int size_top = mid_h - v_sep / 2;
  2995. int size_bottom = size.height - mid_h - v_sep / 2;
  2996. switch (view) {
  2997. case VIEW_USE_1_VIEWPORT: {
  2998. viewports[0]->show();
  2999. for (int i = 1; i < 4; i++) {
  3000. viewports[i]->hide();
  3001. }
  3002. fit_child_in_rect(viewports[0], Rect2(Vector2(), size));
  3003. } break;
  3004. case VIEW_USE_2_VIEWPORTS: {
  3005. for (int i = 0; i < 4; i++) {
  3006. if (i == 1 || i == 3)
  3007. viewports[i]->hide();
  3008. else
  3009. viewports[i]->show();
  3010. }
  3011. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  3012. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size.width, size_bottom)));
  3013. } break;
  3014. case VIEW_USE_2_VIEWPORTS_ALT: {
  3015. for (int i = 0; i < 4; i++) {
  3016. if (i == 1 || i == 3)
  3017. viewports[i]->hide();
  3018. else
  3019. viewports[i]->show();
  3020. }
  3021. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size.height)));
  3022. fit_child_in_rect(viewports[2], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  3023. } break;
  3024. case VIEW_USE_3_VIEWPORTS: {
  3025. for (int i = 0; i < 4; i++) {
  3026. if (i == 1)
  3027. viewports[i]->hide();
  3028. else
  3029. viewports[i]->show();
  3030. }
  3031. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  3032. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  3033. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  3034. } break;
  3035. case VIEW_USE_3_VIEWPORTS_ALT: {
  3036. for (int i = 0; i < 4; i++) {
  3037. if (i == 1)
  3038. viewports[i]->hide();
  3039. else
  3040. viewports[i]->show();
  3041. }
  3042. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  3043. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  3044. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  3045. } break;
  3046. case VIEW_USE_4_VIEWPORTS: {
  3047. for (int i = 0; i < 4; i++) {
  3048. viewports[i]->show();
  3049. }
  3050. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  3051. fit_child_in_rect(viewports[1], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size_top)));
  3052. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  3053. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  3054. } break;
  3055. }
  3056. }
  3057. }
  3058. void SpatialEditorViewportContainer::set_view(View p_view) {
  3059. view = p_view;
  3060. queue_sort();
  3061. }
  3062. SpatialEditorViewportContainer::View SpatialEditorViewportContainer::get_view() {
  3063. return view;
  3064. }
  3065. void SpatialEditorViewportContainer::_bind_methods() {
  3066. ClassDB::bind_method("_gui_input", &SpatialEditorViewportContainer::_gui_input);
  3067. }
  3068. SpatialEditorViewportContainer::SpatialEditorViewportContainer() {
  3069. set_clip_contents(true);
  3070. view = VIEW_USE_1_VIEWPORT;
  3071. mouseover = false;
  3072. ratio_h = 0.5;
  3073. ratio_v = 0.5;
  3074. hovering_v = false;
  3075. hovering_h = false;
  3076. dragging_v = false;
  3077. dragging_h = false;
  3078. }
  3079. ///////////////////////////////////////////////////////////////////
  3080. SpatialEditor *SpatialEditor::singleton = NULL;
  3081. SpatialEditorSelectedItem::~SpatialEditorSelectedItem() {
  3082. if (sbox_instance.is_valid())
  3083. VisualServer::get_singleton()->free(sbox_instance);
  3084. }
  3085. void SpatialEditor::select_gizmo_highlight_axis(int p_axis) {
  3086. for (int i = 0; i < 3; i++) {
  3087. move_gizmo[i]->surface_set_material(0, i == p_axis ? gizmo_color_hl[i] : gizmo_color[i]);
  3088. move_plane_gizmo[i]->surface_set_material(0, (i + 6) == p_axis ? plane_gizmo_color_hl[i] : plane_gizmo_color[i]);
  3089. rotate_gizmo[i]->surface_set_material(0, (i + 3) == p_axis ? gizmo_color_hl[i] : gizmo_color[i]);
  3090. scale_gizmo[i]->surface_set_material(0, (i + 9) == p_axis ? gizmo_color_hl[i] : gizmo_color[i]);
  3091. scale_plane_gizmo[i]->surface_set_material(0, (i + 12) == p_axis ? plane_gizmo_color_hl[i] : plane_gizmo_color[i]);
  3092. }
  3093. }
  3094. void SpatialEditor::update_transform_gizmo() {
  3095. List<Node *> &selection = editor_selection->get_selected_node_list();
  3096. AABB center;
  3097. bool first = true;
  3098. Basis gizmo_basis;
  3099. bool local_gizmo_coords = are_local_coords_enabled();
  3100. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3101. Spatial *sp = Object::cast_to<Spatial>(E->get());
  3102. if (!sp)
  3103. continue;
  3104. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  3105. if (!se)
  3106. continue;
  3107. Transform xf = se->sp->get_global_gizmo_transform();
  3108. if (first) {
  3109. center.position = xf.origin;
  3110. first = false;
  3111. if (local_gizmo_coords) {
  3112. gizmo_basis = xf.basis;
  3113. gizmo_basis.orthonormalize();
  3114. }
  3115. } else {
  3116. center.expand_to(xf.origin);
  3117. gizmo_basis = Basis();
  3118. }
  3119. }
  3120. Vector3 pcenter = center.position + center.size * 0.5;
  3121. gizmo.visible = !first;
  3122. gizmo.transform.origin = pcenter;
  3123. gizmo.transform.basis = gizmo_basis;
  3124. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  3125. viewports[i]->update_transform_gizmo_view();
  3126. }
  3127. }
  3128. void _update_all_gizmos(Node *p_node) {
  3129. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  3130. Spatial *spatial_node = Object::cast_to<Spatial>(p_node->get_child(i));
  3131. if (spatial_node) {
  3132. spatial_node->update_gizmo();
  3133. }
  3134. _update_all_gizmos(p_node->get_child(i));
  3135. }
  3136. }
  3137. void SpatialEditor::update_all_gizmos(Node *p_node) {
  3138. if (!p_node) p_node = SceneTree::get_singleton()->get_root();
  3139. _update_all_gizmos(p_node);
  3140. }
  3141. Object *SpatialEditor::_get_editor_data(Object *p_what) {
  3142. Spatial *sp = Object::cast_to<Spatial>(p_what);
  3143. if (!sp)
  3144. return NULL;
  3145. SpatialEditorSelectedItem *si = memnew(SpatialEditorSelectedItem);
  3146. si->sp = sp;
  3147. si->sbox_instance = VisualServer::get_singleton()->instance_create2(selection_box->get_rid(), sp->get_world()->get_scenario());
  3148. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(si->sbox_instance, VS::SHADOW_CASTING_SETTING_OFF);
  3149. return si;
  3150. }
  3151. Color SpatialEditor::_get_axis_color(int axis) {
  3152. switch (axis) {
  3153. case 0:
  3154. // X axis
  3155. return Color(0.96, 0.20, 0.32);
  3156. case 1:
  3157. // Y axis
  3158. return Color(0.53, 0.84, 0.01);
  3159. case 2:
  3160. // Z axis
  3161. return Color(0.16, 0.55, 0.96);
  3162. default:
  3163. return Color(0, 0, 0);
  3164. }
  3165. }
  3166. void SpatialEditor::_generate_selection_box() {
  3167. AABB aabb(Vector3(), Vector3(1, 1, 1));
  3168. aabb.grow_by(aabb.get_longest_axis_size() / 20.0);
  3169. Ref<SurfaceTool> st = memnew(SurfaceTool);
  3170. st->begin(Mesh::PRIMITIVE_LINES);
  3171. for (int i = 0; i < 12; i++) {
  3172. Vector3 a, b;
  3173. aabb.get_edge(i, a, b);
  3174. st->add_color(Color(1.0, 1.0, 0.8, 0.8));
  3175. st->add_vertex(a);
  3176. st->add_color(Color(1.0, 1.0, 0.8, 0.4));
  3177. st->add_vertex(a.linear_interpolate(b, 0.2));
  3178. st->add_color(Color(1.0, 1.0, 0.8, 0.4));
  3179. st->add_vertex(a.linear_interpolate(b, 0.8));
  3180. st->add_color(Color(1.0, 1.0, 0.8, 0.8));
  3181. st->add_vertex(b);
  3182. }
  3183. Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
  3184. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3185. mat->set_albedo(Color(1, 1, 1));
  3186. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3187. mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  3188. mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  3189. st->set_material(mat);
  3190. selection_box = st->commit();
  3191. }
  3192. Dictionary SpatialEditor::get_state() const {
  3193. Dictionary d;
  3194. d["snap_enabled"] = snap_enabled;
  3195. d["translate_snap"] = get_translate_snap();
  3196. d["rotate_snap"] = get_rotate_snap();
  3197. d["scale_snap"] = get_scale_snap();
  3198. d["local_coords"] = tool_option_button[TOOL_OPT_LOCAL_COORDS]->is_pressed();
  3199. int vc = 0;
  3200. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT)))
  3201. vc = 1;
  3202. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS)))
  3203. vc = 2;
  3204. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS)))
  3205. vc = 3;
  3206. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS)))
  3207. vc = 4;
  3208. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT)))
  3209. vc = 5;
  3210. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT)))
  3211. vc = 6;
  3212. d["viewport_mode"] = vc;
  3213. Array vpdata;
  3214. for (int i = 0; i < 4; i++) {
  3215. vpdata.push_back(viewports[i]->get_state());
  3216. }
  3217. d["viewports"] = vpdata;
  3218. d["show_grid"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID));
  3219. d["show_origin"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN));
  3220. d["fov"] = get_fov();
  3221. d["znear"] = get_znear();
  3222. d["zfar"] = get_zfar();
  3223. Dictionary gizmos_status;
  3224. for (int i = 0; i < gizmo_plugins_by_name.size(); i++) {
  3225. if (!gizmo_plugins_by_name[i]->can_be_hidden()) continue;
  3226. int state = gizmos_menu->get_item_state(gizmos_menu->get_item_index(i));
  3227. String name = gizmo_plugins_by_name[i]->get_name();
  3228. gizmos_status[name] = state;
  3229. }
  3230. d["gizmos_status"] = gizmos_status;
  3231. return d;
  3232. }
  3233. void SpatialEditor::set_state(const Dictionary &p_state) {
  3234. Dictionary d = p_state;
  3235. if (d.has("snap_enabled")) {
  3236. snap_enabled = d["snap_enabled"];
  3237. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(d["snap_enabled"]);
  3238. }
  3239. if (d.has("translate_snap"))
  3240. snap_translate->set_text(d["translate_snap"]);
  3241. if (d.has("rotate_snap"))
  3242. snap_rotate->set_text(d["rotate_snap"]);
  3243. if (d.has("scale_snap"))
  3244. snap_scale->set_text(d["scale_snap"]);
  3245. if (d.has("local_coords")) {
  3246. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(d["local_coords"]);
  3247. update_transform_gizmo();
  3248. }
  3249. if (d.has("viewport_mode")) {
  3250. int vc = d["viewport_mode"];
  3251. if (vc == 1)
  3252. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  3253. else if (vc == 2)
  3254. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  3255. else if (vc == 3)
  3256. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  3257. else if (vc == 4)
  3258. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  3259. else if (vc == 5)
  3260. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  3261. else if (vc == 6)
  3262. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  3263. }
  3264. if (d.has("viewports")) {
  3265. Array vp = d["viewports"];
  3266. uint32_t vp_size = static_cast<uint32_t>(vp.size());
  3267. if (vp_size > VIEWPORTS_COUNT) {
  3268. WARN_PRINT("Ignoring superfluous viewport settings from spatial editor state.");
  3269. vp_size = VIEWPORTS_COUNT;
  3270. }
  3271. for (uint32_t i = 0; i < vp_size; i++) {
  3272. viewports[i]->set_state(vp[i]);
  3273. }
  3274. }
  3275. if (d.has("zfar"))
  3276. settings_zfar->set_value(float(d["zfar"]));
  3277. if (d.has("znear"))
  3278. settings_znear->set_value(float(d["znear"]));
  3279. if (d.has("fov"))
  3280. settings_fov->set_value(float(d["fov"]));
  3281. if (d.has("show_grid")) {
  3282. bool use = d["show_grid"];
  3283. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID))) {
  3284. _menu_item_pressed(MENU_VIEW_GRID);
  3285. }
  3286. }
  3287. if (d.has("show_origin")) {
  3288. bool use = d["show_origin"];
  3289. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN))) {
  3290. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), use);
  3291. VisualServer::get_singleton()->instance_set_visible(origin_instance, use);
  3292. }
  3293. }
  3294. if (d.has("gizmos_status")) {
  3295. Dictionary gizmos_status = d["gizmos_status"];
  3296. List<Variant> keys;
  3297. gizmos_status.get_key_list(&keys);
  3298. for (int j = 0; j < gizmo_plugins_by_name.size(); ++j) {
  3299. if (!gizmo_plugins_by_name[j]->can_be_hidden()) continue;
  3300. int state = EditorSpatialGizmoPlugin::VISIBLE;
  3301. for (int i = 0; i < keys.size(); i++) {
  3302. if (gizmo_plugins_by_name.write[j]->get_name() == keys[i]) {
  3303. state = gizmos_status[keys[i]];
  3304. break;
  3305. }
  3306. }
  3307. gizmo_plugins_by_name.write[j]->set_state(state);
  3308. }
  3309. _update_gizmos_menu();
  3310. }
  3311. }
  3312. void SpatialEditor::edit(Spatial *p_spatial) {
  3313. if (p_spatial != selected) {
  3314. if (selected) {
  3315. Ref<EditorSpatialGizmo> seg = selected->get_gizmo();
  3316. if (seg.is_valid()) {
  3317. seg->set_selected(false);
  3318. selected->update_gizmo();
  3319. }
  3320. }
  3321. selected = p_spatial;
  3322. over_gizmo_handle = -1;
  3323. if (selected) {
  3324. Ref<EditorSpatialGizmo> seg = selected->get_gizmo();
  3325. if (seg.is_valid()) {
  3326. seg->set_selected(true);
  3327. selected->update_gizmo();
  3328. }
  3329. }
  3330. }
  3331. }
  3332. void SpatialEditor::_xform_dialog_action() {
  3333. Transform t;
  3334. //translation
  3335. Vector3 scale;
  3336. Vector3 rotate;
  3337. Vector3 translate;
  3338. for (int i = 0; i < 3; i++) {
  3339. translate[i] = xform_translate[i]->get_text().to_double();
  3340. rotate[i] = Math::deg2rad(xform_rotate[i]->get_text().to_double());
  3341. scale[i] = xform_scale[i]->get_text().to_double();
  3342. }
  3343. t.basis.scale(scale);
  3344. t.basis.rotate(rotate);
  3345. t.origin = translate;
  3346. undo_redo->create_action(TTR("XForm Dialog"));
  3347. List<Node *> &selection = editor_selection->get_selected_node_list();
  3348. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3349. Spatial *sp = Object::cast_to<Spatial>(E->get());
  3350. if (!sp)
  3351. continue;
  3352. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  3353. if (!se)
  3354. continue;
  3355. bool post = xform_type->get_selected() > 0;
  3356. Transform tr = sp->get_global_gizmo_transform();
  3357. if (post)
  3358. tr = tr * t;
  3359. else {
  3360. tr.basis = t.basis * tr.basis;
  3361. tr.origin += t.origin;
  3362. }
  3363. undo_redo->add_do_method(sp, "set_global_transform", tr);
  3364. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  3365. }
  3366. undo_redo->commit_action();
  3367. }
  3368. void SpatialEditor::_menu_item_toggled(bool pressed, int p_option) {
  3369. switch (p_option) {
  3370. case MENU_TOOL_LOCAL_COORDS: {
  3371. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(pressed);
  3372. update_transform_gizmo();
  3373. } break;
  3374. case MENU_TOOL_USE_SNAP: {
  3375. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(pressed);
  3376. snap_enabled = pressed;
  3377. } break;
  3378. }
  3379. }
  3380. void SpatialEditor::_menu_gizmo_toggled(int p_option) {
  3381. const int idx = gizmos_menu->get_item_index(p_option);
  3382. gizmos_menu->toggle_item_multistate(idx);
  3383. // Change icon
  3384. const int state = gizmos_menu->get_item_state(idx);
  3385. switch (state) {
  3386. case EditorSpatialGizmoPlugin::VISIBLE:
  3387. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_visible"));
  3388. break;
  3389. case EditorSpatialGizmoPlugin::ON_TOP:
  3390. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_xray"));
  3391. break;
  3392. case EditorSpatialGizmoPlugin::HIDDEN:
  3393. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_hidden"));
  3394. break;
  3395. }
  3396. gizmo_plugins_by_name.write[p_option]->set_state(state);
  3397. update_all_gizmos();
  3398. }
  3399. void SpatialEditor::_menu_item_pressed(int p_option) {
  3400. switch (p_option) {
  3401. case MENU_TOOL_SELECT:
  3402. case MENU_TOOL_MOVE:
  3403. case MENU_TOOL_ROTATE:
  3404. case MENU_TOOL_SCALE:
  3405. case MENU_TOOL_LIST_SELECT: {
  3406. for (int i = 0; i < TOOL_MAX; i++)
  3407. tool_button[i]->set_pressed(i == p_option);
  3408. tool_mode = (ToolMode)p_option;
  3409. update_transform_gizmo();
  3410. } break;
  3411. case MENU_TRANSFORM_CONFIGURE_SNAP: {
  3412. snap_dialog->popup_centered(Size2(200, 180));
  3413. } break;
  3414. case MENU_TRANSFORM_DIALOG: {
  3415. for (int i = 0; i < 3; i++) {
  3416. xform_translate[i]->set_text("0");
  3417. xform_rotate[i]->set_text("0");
  3418. xform_scale[i]->set_text("1");
  3419. }
  3420. xform_dialog->popup_centered(Size2(320, 240) * EDSCALE);
  3421. } break;
  3422. case MENU_VIEW_USE_1_VIEWPORT: {
  3423. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_1_VIEWPORT);
  3424. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true);
  3425. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3426. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3427. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3428. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3429. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3430. } break;
  3431. case MENU_VIEW_USE_2_VIEWPORTS: {
  3432. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_2_VIEWPORTS);
  3433. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3434. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true);
  3435. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3436. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3437. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3438. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3439. } break;
  3440. case MENU_VIEW_USE_2_VIEWPORTS_ALT: {
  3441. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_2_VIEWPORTS_ALT);
  3442. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3443. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3444. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3445. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3446. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), true);
  3447. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3448. } break;
  3449. case MENU_VIEW_USE_3_VIEWPORTS: {
  3450. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_3_VIEWPORTS);
  3451. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3452. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3453. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), true);
  3454. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3455. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3456. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3457. } break;
  3458. case MENU_VIEW_USE_3_VIEWPORTS_ALT: {
  3459. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_3_VIEWPORTS_ALT);
  3460. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3461. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3462. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3463. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3464. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3465. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), true);
  3466. } break;
  3467. case MENU_VIEW_USE_4_VIEWPORTS: {
  3468. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_4_VIEWPORTS);
  3469. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3470. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3471. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3472. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), true);
  3473. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3474. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3475. } break;
  3476. case MENU_VIEW_ORIGIN: {
  3477. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  3478. origin_enabled = !is_checked;
  3479. VisualServer::get_singleton()->instance_set_visible(origin_instance, origin_enabled);
  3480. // Update the grid since its appearance depends on whether the origin is enabled
  3481. _finish_grid();
  3482. _init_grid();
  3483. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), origin_enabled);
  3484. } break;
  3485. case MENU_VIEW_GRID: {
  3486. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  3487. grid_enabled = !is_checked;
  3488. for (int i = 0; i < 3; ++i) {
  3489. if (grid_enable[i]) {
  3490. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], grid_enabled);
  3491. grid_visible[i] = grid_enabled;
  3492. }
  3493. }
  3494. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), grid_enabled);
  3495. } break;
  3496. case MENU_VIEW_CAMERA_SETTINGS: {
  3497. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50));
  3498. } break;
  3499. case MENU_SNAP_TO_FLOOR: {
  3500. snap_selected_nodes_to_floor();
  3501. } break;
  3502. case MENU_LOCK_SELECTED: {
  3503. undo_redo->create_action(TTR("Lock Selected"));
  3504. List<Node *> &selection = editor_selection->get_selected_node_list();
  3505. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3506. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  3507. if (!spatial || !spatial->is_visible_in_tree())
  3508. continue;
  3509. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root())
  3510. continue;
  3511. undo_redo->add_do_method(spatial, "set_meta", "_edit_lock_", true);
  3512. undo_redo->add_undo_method(spatial, "remove_meta", "_edit_lock_");
  3513. undo_redo->add_do_method(this, "emit_signal", "item_lock_status_changed");
  3514. undo_redo->add_undo_method(this, "emit_signal", "item_lock_status_changed");
  3515. }
  3516. undo_redo->add_do_method(this, "_refresh_menu_icons", Variant());
  3517. undo_redo->add_undo_method(this, "_refresh_menu_icons", Variant());
  3518. undo_redo->commit_action();
  3519. } break;
  3520. case MENU_UNLOCK_SELECTED: {
  3521. undo_redo->create_action(TTR("Unlock Selected"));
  3522. List<Node *> &selection = editor_selection->get_selected_node_list();
  3523. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3524. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  3525. if (!spatial || !spatial->is_visible_in_tree())
  3526. continue;
  3527. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root())
  3528. continue;
  3529. undo_redo->add_do_method(spatial, "remove_meta", "_edit_lock_");
  3530. undo_redo->add_undo_method(spatial, "set_meta", "_edit_lock_", true);
  3531. undo_redo->add_do_method(this, "emit_signal", "item_lock_status_changed");
  3532. undo_redo->add_undo_method(this, "emit_signal", "item_lock_status_changed");
  3533. }
  3534. undo_redo->add_do_method(this, "_refresh_menu_icons", Variant());
  3535. undo_redo->add_undo_method(this, "_refresh_menu_icons", Variant());
  3536. undo_redo->commit_action();
  3537. } break;
  3538. case MENU_GROUP_SELECTED: {
  3539. undo_redo->create_action(TTR("Group Selected"));
  3540. List<Node *> &selection = editor_selection->get_selected_node_list();
  3541. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3542. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  3543. if (!spatial || !spatial->is_visible_in_tree())
  3544. continue;
  3545. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root())
  3546. continue;
  3547. undo_redo->add_do_method(spatial, "set_meta", "_edit_group_", true);
  3548. undo_redo->add_undo_method(spatial, "remove_meta", "_edit_group_");
  3549. undo_redo->add_do_method(this, "emit_signal", "item_group_status_changed");
  3550. undo_redo->add_undo_method(this, "emit_signal", "item_group_status_changed");
  3551. }
  3552. undo_redo->add_do_method(this, "_refresh_menu_icons", Variant());
  3553. undo_redo->add_undo_method(this, "_refresh_menu_icons", Variant());
  3554. undo_redo->commit_action();
  3555. } break;
  3556. case MENU_UNGROUP_SELECTED: {
  3557. undo_redo->create_action(TTR("Ungroup Selected"));
  3558. List<Node *> &selection = editor_selection->get_selected_node_list();
  3559. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3560. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  3561. if (!spatial || !spatial->is_visible_in_tree())
  3562. continue;
  3563. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root())
  3564. continue;
  3565. undo_redo->add_do_method(spatial, "remove_meta", "_edit_group_");
  3566. undo_redo->add_undo_method(spatial, "set_meta", "_edit_group_", true);
  3567. undo_redo->add_do_method(this, "emit_signal", "item_group_status_changed");
  3568. undo_redo->add_undo_method(this, "emit_signal", "item_group_status_changed");
  3569. }
  3570. undo_redo->add_do_method(this, "_refresh_menu_icons", Variant());
  3571. undo_redo->add_undo_method(this, "_refresh_menu_icons", Variant());
  3572. undo_redo->commit_action();
  3573. } break;
  3574. }
  3575. }
  3576. void SpatialEditor::_init_indicators() {
  3577. {
  3578. origin_enabled = true;
  3579. grid_enabled = true;
  3580. indicator_mat.instance();
  3581. indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3582. indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  3583. indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  3584. Vector<Color> origin_colors;
  3585. Vector<Vector3> origin_points;
  3586. for (int i = 0; i < 3; i++) {
  3587. Vector3 axis;
  3588. axis[i] = 1;
  3589. Color origin_color = _get_axis_color(i);
  3590. grid_enable[i] = false;
  3591. grid_visible[i] = false;
  3592. origin_colors.push_back(origin_color);
  3593. origin_colors.push_back(origin_color);
  3594. origin_points.push_back(axis * 4096);
  3595. origin_points.push_back(axis * -4096);
  3596. }
  3597. grid_enable[1] = true;
  3598. grid_visible[1] = true;
  3599. _init_grid();
  3600. origin = VisualServer::get_singleton()->mesh_create();
  3601. Array d;
  3602. d.resize(VS::ARRAY_MAX);
  3603. d[VisualServer::ARRAY_VERTEX] = origin_points;
  3604. d[VisualServer::ARRAY_COLOR] = origin_colors;
  3605. VisualServer::get_singleton()->mesh_add_surface_from_arrays(origin, VisualServer::PRIMITIVE_LINES, d);
  3606. VisualServer::get_singleton()->mesh_surface_set_material(origin, 0, indicator_mat->get_rid());
  3607. origin_instance = VisualServer::get_singleton()->instance_create2(origin, get_tree()->get_root()->get_world()->get_scenario());
  3608. VS::get_singleton()->instance_set_layer_mask(origin_instance, 1 << SpatialEditorViewport::GIZMO_GRID_LAYER);
  3609. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(origin_instance, VS::SHADOW_CASTING_SETTING_OFF);
  3610. }
  3611. {
  3612. //move gizmo
  3613. for (int i = 0; i < 3; i++) {
  3614. Color col = _get_axis_color(i);
  3615. col.a = EditorSettings::get_singleton()->get("editors/3d/manipulator_gizmo_opacity");
  3616. move_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3617. move_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3618. rotate_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3619. scale_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3620. scale_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3621. Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
  3622. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3623. mat->set_on_top_of_alpha();
  3624. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3625. mat->set_albedo(col);
  3626. gizmo_color[i] = mat;
  3627. Ref<SpatialMaterial> mat_hl = mat->duplicate();
  3628. mat_hl->set_albedo(Color(col.r, col.g, col.b, 1.0));
  3629. gizmo_color_hl[i] = mat_hl;
  3630. Vector3 ivec;
  3631. ivec[i] = 1;
  3632. Vector3 nivec;
  3633. nivec[(i + 1) % 3] = 1;
  3634. nivec[(i + 2) % 3] = 1;
  3635. Vector3 ivec2;
  3636. ivec2[(i + 1) % 3] = 1;
  3637. Vector3 ivec3;
  3638. ivec3[(i + 2) % 3] = 1;
  3639. //translate
  3640. {
  3641. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3642. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3643. // Arrow profile
  3644. const int arrow_points = 5;
  3645. Vector3 arrow[5] = {
  3646. nivec * 0.0 + ivec * 0.0,
  3647. nivec * 0.01 + ivec * 0.0,
  3648. nivec * 0.01 + ivec * GIZMO_ARROW_OFFSET,
  3649. nivec * 0.065 + ivec * GIZMO_ARROW_OFFSET,
  3650. nivec * 0.0 + ivec * (GIZMO_ARROW_OFFSET + GIZMO_ARROW_SIZE),
  3651. };
  3652. int arrow_sides = 16;
  3653. for (int k = 0; k < arrow_sides; k++) {
  3654. Basis ma(ivec, Math_PI * 2 * float(k) / arrow_sides);
  3655. Basis mb(ivec, Math_PI * 2 * float(k + 1) / arrow_sides);
  3656. for (int j = 0; j < arrow_points - 1; j++) {
  3657. Vector3 points[4] = {
  3658. ma.xform(arrow[j]),
  3659. mb.xform(arrow[j]),
  3660. mb.xform(arrow[j + 1]),
  3661. ma.xform(arrow[j + 1]),
  3662. };
  3663. surftool->add_vertex(points[0]);
  3664. surftool->add_vertex(points[1]);
  3665. surftool->add_vertex(points[2]);
  3666. surftool->add_vertex(points[0]);
  3667. surftool->add_vertex(points[2]);
  3668. surftool->add_vertex(points[3]);
  3669. }
  3670. }
  3671. surftool->set_material(mat);
  3672. surftool->commit(move_gizmo[i]);
  3673. }
  3674. // Plane Translation
  3675. {
  3676. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3677. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3678. Vector3 vec = ivec2 - ivec3;
  3679. Vector3 plane[4] = {
  3680. vec * GIZMO_PLANE_DST,
  3681. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  3682. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  3683. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  3684. };
  3685. Basis ma(ivec, Math_PI / 2);
  3686. Vector3 points[4] = {
  3687. ma.xform(plane[0]),
  3688. ma.xform(plane[1]),
  3689. ma.xform(plane[2]),
  3690. ma.xform(plane[3]),
  3691. };
  3692. surftool->add_vertex(points[0]);
  3693. surftool->add_vertex(points[1]);
  3694. surftool->add_vertex(points[2]);
  3695. surftool->add_vertex(points[0]);
  3696. surftool->add_vertex(points[2]);
  3697. surftool->add_vertex(points[3]);
  3698. Ref<SpatialMaterial> plane_mat = memnew(SpatialMaterial);
  3699. plane_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3700. plane_mat->set_on_top_of_alpha();
  3701. plane_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3702. plane_mat->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  3703. plane_mat->set_albedo(col);
  3704. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  3705. surftool->set_material(plane_mat);
  3706. surftool->commit(move_plane_gizmo[i]);
  3707. Ref<SpatialMaterial> plane_mat_hl = plane_mat->duplicate();
  3708. plane_mat_hl->set_albedo(Color(col.r, col.g, col.b, 1.0));
  3709. plane_gizmo_color_hl[i] = plane_mat_hl; // needed, so we can draw planes from both sides
  3710. }
  3711. // Rotate
  3712. {
  3713. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3714. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3715. Vector3 circle[5] = {
  3716. ivec * 0.02 + ivec2 * 0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3717. ivec * -0.02 + ivec2 * 0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3718. ivec * -0.02 + ivec2 * -0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3719. ivec * 0.02 + ivec2 * -0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3720. ivec * 0.02 + ivec2 * 0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3721. };
  3722. for (int k = 0; k < 64; k++) {
  3723. Basis ma(ivec, Math_PI * 2 * float(k) / 64);
  3724. Basis mb(ivec, Math_PI * 2 * float(k + 1) / 64);
  3725. for (int j = 0; j < 4; j++) {
  3726. Vector3 points[4] = {
  3727. ma.xform(circle[j]),
  3728. mb.xform(circle[j]),
  3729. mb.xform(circle[j + 1]),
  3730. ma.xform(circle[j + 1]),
  3731. };
  3732. surftool->add_vertex(points[0]);
  3733. surftool->add_vertex(points[1]);
  3734. surftool->add_vertex(points[2]);
  3735. surftool->add_vertex(points[0]);
  3736. surftool->add_vertex(points[2]);
  3737. surftool->add_vertex(points[3]);
  3738. }
  3739. }
  3740. surftool->set_material(mat);
  3741. surftool->commit(rotate_gizmo[i]);
  3742. }
  3743. // Scale
  3744. {
  3745. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3746. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3747. // Cube arrow profile
  3748. const int arrow_points = 6;
  3749. Vector3 arrow[6] = {
  3750. nivec * 0.0 + ivec * 0.0,
  3751. nivec * 0.01 + ivec * 0.0,
  3752. nivec * 0.01 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  3753. nivec * 0.07 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  3754. nivec * 0.07 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  3755. nivec * 0.0 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  3756. };
  3757. int arrow_sides = 4;
  3758. for (int k = 0; k < 4; k++) {
  3759. Basis ma(ivec, Math_PI * 2 * float(k) / arrow_sides);
  3760. Basis mb(ivec, Math_PI * 2 * float(k + 1) / arrow_sides);
  3761. for (int j = 0; j < arrow_points - 1; j++) {
  3762. Vector3 points[4] = {
  3763. ma.xform(arrow[j]),
  3764. mb.xform(arrow[j]),
  3765. mb.xform(arrow[j + 1]),
  3766. ma.xform(arrow[j + 1]),
  3767. };
  3768. surftool->add_vertex(points[0]);
  3769. surftool->add_vertex(points[1]);
  3770. surftool->add_vertex(points[2]);
  3771. surftool->add_vertex(points[0]);
  3772. surftool->add_vertex(points[2]);
  3773. surftool->add_vertex(points[3]);
  3774. }
  3775. }
  3776. surftool->set_material(mat);
  3777. surftool->commit(scale_gizmo[i]);
  3778. }
  3779. // Plane Scale
  3780. {
  3781. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3782. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3783. Vector3 vec = ivec2 - ivec3;
  3784. Vector3 plane[4] = {
  3785. vec * GIZMO_PLANE_DST,
  3786. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  3787. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  3788. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  3789. };
  3790. Basis ma(ivec, Math_PI / 2);
  3791. Vector3 points[4] = {
  3792. ma.xform(plane[0]),
  3793. ma.xform(plane[1]),
  3794. ma.xform(plane[2]),
  3795. ma.xform(plane[3]),
  3796. };
  3797. surftool->add_vertex(points[0]);
  3798. surftool->add_vertex(points[1]);
  3799. surftool->add_vertex(points[2]);
  3800. surftool->add_vertex(points[0]);
  3801. surftool->add_vertex(points[2]);
  3802. surftool->add_vertex(points[3]);
  3803. Ref<SpatialMaterial> plane_mat = memnew(SpatialMaterial);
  3804. plane_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3805. plane_mat->set_on_top_of_alpha();
  3806. plane_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3807. plane_mat->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  3808. plane_mat->set_albedo(col);
  3809. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  3810. surftool->set_material(plane_mat);
  3811. surftool->commit(scale_plane_gizmo[i]);
  3812. Ref<SpatialMaterial> plane_mat_hl = plane_mat->duplicate();
  3813. plane_mat_hl->set_albedo(Color(col.r, col.g, col.b, 1.0));
  3814. plane_gizmo_color_hl[i] = plane_mat_hl; // needed, so we can draw planes from both sides
  3815. }
  3816. }
  3817. }
  3818. _generate_selection_box();
  3819. }
  3820. void SpatialEditor::_update_gizmos_menu() {
  3821. gizmos_menu->clear();
  3822. for (int i = 0; i < gizmo_plugins_by_name.size(); ++i) {
  3823. if (!gizmo_plugins_by_name[i]->can_be_hidden()) continue;
  3824. String plugin_name = gizmo_plugins_by_name[i]->get_name();
  3825. const int plugin_state = gizmo_plugins_by_name[i]->get_state();
  3826. gizmos_menu->add_multistate_item(TTR(plugin_name), 3, plugin_state, i);
  3827. const int idx = gizmos_menu->get_item_index(i);
  3828. switch (plugin_state) {
  3829. case EditorSpatialGizmoPlugin::VISIBLE:
  3830. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_visible"));
  3831. break;
  3832. case EditorSpatialGizmoPlugin::ON_TOP:
  3833. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_xray"));
  3834. break;
  3835. case EditorSpatialGizmoPlugin::HIDDEN:
  3836. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_hidden"));
  3837. break;
  3838. }
  3839. }
  3840. }
  3841. void SpatialEditor::_update_gizmos_menu_theme() {
  3842. for (int i = 0; i < gizmo_plugins_by_name.size(); ++i) {
  3843. if (!gizmo_plugins_by_name[i]->can_be_hidden()) continue;
  3844. const int plugin_state = gizmo_plugins_by_name[i]->get_state();
  3845. const int idx = gizmos_menu->get_item_index(i);
  3846. switch (plugin_state) {
  3847. case EditorSpatialGizmoPlugin::VISIBLE:
  3848. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_visible"));
  3849. break;
  3850. case EditorSpatialGizmoPlugin::ON_TOP:
  3851. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_xray"));
  3852. break;
  3853. case EditorSpatialGizmoPlugin::HIDDEN:
  3854. gizmos_menu->set_item_icon(idx, gizmos_menu->get_icon("visibility_hidden"));
  3855. break;
  3856. }
  3857. }
  3858. }
  3859. void SpatialEditor::_init_grid() {
  3860. PoolVector<Color> grid_colors[3];
  3861. PoolVector<Vector3> grid_points[3];
  3862. Color primary_grid_color = EditorSettings::get_singleton()->get("editors/3d/primary_grid_color");
  3863. Color secondary_grid_color = EditorSettings::get_singleton()->get("editors/3d/secondary_grid_color");
  3864. int grid_size = EditorSettings::get_singleton()->get("editors/3d/grid_size");
  3865. int primary_grid_steps = EditorSettings::get_singleton()->get("editors/3d/primary_grid_steps");
  3866. for (int i = 0; i < 3; i++) {
  3867. Vector3 axis;
  3868. axis[i] = 1;
  3869. Vector3 axis_n1;
  3870. axis_n1[(i + 1) % 3] = 1;
  3871. Vector3 axis_n2;
  3872. axis_n2[(i + 2) % 3] = 1;
  3873. for (int j = -grid_size; j <= grid_size; j++) {
  3874. Vector3 p1 = axis_n1 * j + axis_n2 * -grid_size;
  3875. Vector3 p1_dest = p1 * (-axis_n2 + axis_n1);
  3876. Vector3 p2 = axis_n2 * j + axis_n1 * -grid_size;
  3877. Vector3 p2_dest = p2 * (-axis_n1 + axis_n2);
  3878. Color line_color = secondary_grid_color;
  3879. if (origin_enabled && j == 0) {
  3880. // Don't draw the center lines of the grid if the origin is enabled
  3881. // The origin would overlap the grid lines in this case, causing flickering
  3882. continue;
  3883. } else if (j % primary_grid_steps == 0) {
  3884. line_color = primary_grid_color;
  3885. }
  3886. grid_points[i].push_back(p1);
  3887. grid_points[i].push_back(p1_dest);
  3888. grid_colors[i].push_back(line_color);
  3889. grid_colors[i].push_back(line_color);
  3890. grid_points[i].push_back(p2);
  3891. grid_points[i].push_back(p2_dest);
  3892. grid_colors[i].push_back(line_color);
  3893. grid_colors[i].push_back(line_color);
  3894. }
  3895. grid[i] = VisualServer::get_singleton()->mesh_create();
  3896. Array d;
  3897. d.resize(VS::ARRAY_MAX);
  3898. d[VisualServer::ARRAY_VERTEX] = grid_points[i];
  3899. d[VisualServer::ARRAY_COLOR] = grid_colors[i];
  3900. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
  3901. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  3902. grid_instance[i] = VisualServer::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  3903. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], grid_visible[i]);
  3904. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(grid_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  3905. VS::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << SpatialEditorViewport::GIZMO_GRID_LAYER);
  3906. }
  3907. }
  3908. void SpatialEditor::_finish_indicators() {
  3909. VisualServer::get_singleton()->free(origin_instance);
  3910. VisualServer::get_singleton()->free(origin);
  3911. _finish_grid();
  3912. }
  3913. void SpatialEditor::_finish_grid() {
  3914. for (int i = 0; i < 3; i++) {
  3915. VisualServer::get_singleton()->free(grid_instance[i]);
  3916. VisualServer::get_singleton()->free(grid[i]);
  3917. }
  3918. }
  3919. bool SpatialEditor::is_any_freelook_active() const {
  3920. for (unsigned int i = 0; i < VIEWPORTS_COUNT; ++i) {
  3921. if (viewports[i]->is_freelook_active())
  3922. return true;
  3923. }
  3924. return false;
  3925. }
  3926. void SpatialEditor::_refresh_menu_icons() {
  3927. bool all_locked = true;
  3928. bool all_grouped = true;
  3929. List<Node *> &selection = editor_selection->get_selected_node_list();
  3930. if (selection.empty()) {
  3931. all_locked = false;
  3932. all_grouped = false;
  3933. } else {
  3934. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3935. if (Object::cast_to<Spatial>(E->get()) && !Object::cast_to<Spatial>(E->get())->has_meta("_edit_lock_")) {
  3936. all_locked = false;
  3937. break;
  3938. }
  3939. }
  3940. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3941. if (Object::cast_to<Spatial>(E->get()) && !Object::cast_to<Spatial>(E->get())->has_meta("_edit_group_")) {
  3942. all_grouped = false;
  3943. break;
  3944. }
  3945. }
  3946. }
  3947. tool_button[TOOL_LOCK_SELECTED]->set_visible(!all_locked);
  3948. tool_button[TOOL_LOCK_SELECTED]->set_disabled(selection.empty());
  3949. tool_button[TOOL_UNLOCK_SELECTED]->set_visible(all_locked);
  3950. tool_button[TOOL_GROUP_SELECTED]->set_visible(!all_grouped);
  3951. tool_button[TOOL_GROUP_SELECTED]->set_disabled(selection.empty());
  3952. tool_button[TOOL_UNGROUP_SELECTED]->set_visible(all_grouped);
  3953. }
  3954. template <typename T>
  3955. Set<T *> _get_child_nodes(Node *parent_node) {
  3956. Set<T *> nodes = Set<T *>();
  3957. T *node = Node::cast_to<T>(parent_node);
  3958. if (node) {
  3959. nodes.insert(node);
  3960. }
  3961. for (int i = 0; i < parent_node->get_child_count(); i++) {
  3962. Node *child_node = parent_node->get_child(i);
  3963. Set<T *> child_nodes = _get_child_nodes<T>(child_node);
  3964. for (typename Set<T *>::Element *I = child_nodes.front(); I; I = I->next()) {
  3965. nodes.insert(I->get());
  3966. }
  3967. }
  3968. return nodes;
  3969. }
  3970. Set<RID> _get_physics_bodies_rid(Node *node) {
  3971. Set<RID> rids = Set<RID>();
  3972. PhysicsBody *pb = Node::cast_to<PhysicsBody>(node);
  3973. if (pb) {
  3974. rids.insert(pb->get_rid());
  3975. }
  3976. Set<PhysicsBody *> child_nodes = _get_child_nodes<PhysicsBody>(node);
  3977. for (Set<PhysicsBody *>::Element *I = child_nodes.front(); I; I = I->next()) {
  3978. rids.insert(I->get()->get_rid());
  3979. }
  3980. return rids;
  3981. }
  3982. void SpatialEditor::snap_selected_nodes_to_floor() {
  3983. List<Node *> &selection = editor_selection->get_selected_node_list();
  3984. Dictionary snap_data;
  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. Vector3 from = Vector3();
  3989. Vector3 position_offset = Vector3();
  3990. // Priorities for snapping to floor are CollisionShapes, VisualInstances and then origin
  3991. Set<VisualInstance *> vi = _get_child_nodes<VisualInstance>(sp);
  3992. Set<CollisionShape *> cs = _get_child_nodes<CollisionShape>(sp);
  3993. if (cs.size()) {
  3994. AABB aabb = sp->get_global_transform().xform(cs.front()->get()->get_shape()->get_debug_mesh()->get_aabb());
  3995. for (Set<CollisionShape *>::Element *I = cs.front(); I; I = I->next()) {
  3996. aabb.merge_with(sp->get_global_transform().xform(I->get()->get_shape()->get_debug_mesh()->get_aabb()));
  3997. }
  3998. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  3999. from = aabb.position + size;
  4000. position_offset.y = from.y - sp->get_global_transform().origin.y;
  4001. } else if (vi.size()) {
  4002. AABB aabb = vi.front()->get()->get_transformed_aabb();
  4003. for (Set<VisualInstance *>::Element *I = vi.front(); I; I = I->next()) {
  4004. aabb.merge_with(I->get()->get_transformed_aabb());
  4005. }
  4006. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  4007. from = aabb.position + size;
  4008. position_offset.y = from.y - sp->get_global_transform().origin.y;
  4009. } else {
  4010. from = sp->get_global_transform().origin;
  4011. }
  4012. // We add a bit of margin to the from position to avoid it from snapping
  4013. // when the spatial is already on a floor and there's another floor under
  4014. // it
  4015. from = from + Vector3(0.0, 0.1, 0.0);
  4016. Dictionary d;
  4017. d["from"] = from;
  4018. d["position_offset"] = position_offset;
  4019. snap_data[sp] = d;
  4020. }
  4021. }
  4022. PhysicsDirectSpaceState *ss = get_tree()->get_root()->get_world()->get_direct_space_state();
  4023. PhysicsDirectSpaceState::RayResult result;
  4024. Array keys = snap_data.keys();
  4025. if (keys.size()) {
  4026. undo_redo->create_action(TTR("Snap Nodes To Floor"));
  4027. for (int i = 0; i < keys.size(); i++) {
  4028. Node *node = keys[i];
  4029. Spatial *sp = Object::cast_to<Spatial>(node);
  4030. Dictionary d = snap_data[node];
  4031. Vector3 from = d["from"];
  4032. Vector3 position_offset = d["position_offset"];
  4033. Vector3 to = from - Vector3(0.0, 10.0, 0.0);
  4034. Set<RID> excluded = _get_physics_bodies_rid(sp);
  4035. if (ss->intersect_ray(from, to, result, excluded)) {
  4036. Transform new_transform = sp->get_global_transform();
  4037. new_transform.origin.y = result.position.y;
  4038. new_transform.origin = new_transform.origin - position_offset;
  4039. undo_redo->add_do_method(sp, "set_global_transform", new_transform);
  4040. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_transform());
  4041. }
  4042. }
  4043. undo_redo->commit_action();
  4044. }
  4045. }
  4046. void SpatialEditor::_unhandled_key_input(Ref<InputEvent> p_event) {
  4047. if (!is_visible_in_tree() || get_viewport()->gui_has_modal_stack())
  4048. return;
  4049. snap_key_enabled = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  4050. Ref<InputEventKey> k = p_event;
  4051. if (k.is_valid()) {
  4052. // Note: need to check is_echo because first person movement keys might still be held
  4053. if (!is_any_freelook_active() && !p_event->is_echo()) {
  4054. if (!k->is_pressed())
  4055. return;
  4056. if (ED_IS_SHORTCUT("spatial_editor/tool_select", p_event)) {
  4057. _menu_item_pressed(MENU_TOOL_SELECT);
  4058. } else if (ED_IS_SHORTCUT("spatial_editor/tool_move", p_event)) {
  4059. _menu_item_pressed(MENU_TOOL_MOVE);
  4060. } else if (ED_IS_SHORTCUT("spatial_editor/tool_rotate", p_event)) {
  4061. _menu_item_pressed(MENU_TOOL_ROTATE);
  4062. } else if (ED_IS_SHORTCUT("spatial_editor/tool_scale", p_event)) {
  4063. _menu_item_pressed(MENU_TOOL_SCALE);
  4064. } else if (ED_IS_SHORTCUT("spatial_editor/snap_to_floor", p_event)) {
  4065. snap_selected_nodes_to_floor();
  4066. } else if (ED_IS_SHORTCUT("spatial_editor/local_coords", p_event)) {
  4067. if (are_local_coords_enabled()) {
  4068. _menu_item_toggled(false, MENU_TOOL_LOCAL_COORDS);
  4069. } else {
  4070. _menu_item_toggled(true, MENU_TOOL_LOCAL_COORDS);
  4071. }
  4072. } else if (ED_IS_SHORTCUT("spatial_editor/snap", p_event)) {
  4073. if (is_snap_enabled()) {
  4074. _menu_item_toggled(false, MENU_TOOL_USE_SNAP);
  4075. } else {
  4076. _menu_item_toggled(true, MENU_TOOL_USE_SNAP);
  4077. }
  4078. }
  4079. }
  4080. }
  4081. }
  4082. void SpatialEditor::_notification(int p_what) {
  4083. if (p_what == NOTIFICATION_READY) {
  4084. tool_button[SpatialEditor::TOOL_MODE_SELECT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  4085. tool_button[SpatialEditor::TOOL_MODE_MOVE]->set_icon(get_icon("ToolMove", "EditorIcons"));
  4086. tool_button[SpatialEditor::TOOL_MODE_ROTATE]->set_icon(get_icon("ToolRotate", "EditorIcons"));
  4087. tool_button[SpatialEditor::TOOL_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons"));
  4088. tool_button[SpatialEditor::TOOL_MODE_LIST_SELECT]->set_icon(get_icon("ListSelect", "EditorIcons"));
  4089. tool_button[SpatialEditor::TOOL_LOCK_SELECTED]->set_icon(get_icon("Lock", "EditorIcons"));
  4090. tool_button[SpatialEditor::TOOL_UNLOCK_SELECTED]->set_icon(get_icon("Unlock", "EditorIcons"));
  4091. tool_button[SpatialEditor::TOOL_GROUP_SELECTED]->set_icon(get_icon("Group", "EditorIcons"));
  4092. tool_button[SpatialEditor::TOOL_UNGROUP_SELECTED]->set_icon(get_icon("Ungroup", "EditorIcons"));
  4093. tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->set_icon(get_icon("Object", "EditorIcons"));
  4094. tool_option_button[SpatialEditor::TOOL_OPT_USE_SNAP]->set_icon(get_icon("Snap", "EditorIcons"));
  4095. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_icon("Panels1", "EditorIcons"));
  4096. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_icon("Panels2", "EditorIcons"));
  4097. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_icon("Panels2Alt", "EditorIcons"));
  4098. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_icon("Panels3", "EditorIcons"));
  4099. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_icon("Panels3Alt", "EditorIcons"));
  4100. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_icon("Panels4", "EditorIcons"));
  4101. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  4102. _refresh_menu_icons();
  4103. get_tree()->connect("node_removed", this, "_node_removed");
  4104. EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor()->connect("node_changed", this, "_refresh_menu_icons");
  4105. editor_selection->connect("selection_changed", this, "_refresh_menu_icons");
  4106. } else if (p_what == NOTIFICATION_ENTER_TREE) {
  4107. _register_all_gizmos();
  4108. _update_gizmos_menu();
  4109. _init_indicators();
  4110. } else if (p_what == NOTIFICATION_THEME_CHANGED) {
  4111. _update_gizmos_menu_theme();
  4112. } else if (p_what == NOTIFICATION_EXIT_TREE) {
  4113. _finish_indicators();
  4114. } else if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  4115. tool_button[SpatialEditor::TOOL_MODE_SELECT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  4116. tool_button[SpatialEditor::TOOL_MODE_MOVE]->set_icon(get_icon("ToolMove", "EditorIcons"));
  4117. tool_button[SpatialEditor::TOOL_MODE_ROTATE]->set_icon(get_icon("ToolRotate", "EditorIcons"));
  4118. tool_button[SpatialEditor::TOOL_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons"));
  4119. tool_button[SpatialEditor::TOOL_MODE_LIST_SELECT]->set_icon(get_icon("ListSelect", "EditorIcons"));
  4120. tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->set_icon(get_icon("Object", "EditorIcons"));
  4121. tool_option_button[SpatialEditor::TOOL_OPT_USE_SNAP]->set_icon(get_icon("Snap", "EditorIcons"));
  4122. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_icon("Panels1", "EditorIcons"));
  4123. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_icon("Panels2", "EditorIcons"));
  4124. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_icon("Panels2Alt", "EditorIcons"));
  4125. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_icon("Panels3", "EditorIcons"));
  4126. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_icon("Panels3Alt", "EditorIcons"));
  4127. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_icon("Panels4", "EditorIcons"));
  4128. // Update grid color by rebuilding grid.
  4129. _finish_grid();
  4130. _init_grid();
  4131. }
  4132. }
  4133. void SpatialEditor::add_control_to_menu_panel(Control *p_control) {
  4134. hbc_menu->add_child(p_control);
  4135. }
  4136. void SpatialEditor::remove_control_from_menu_panel(Control *p_control) {
  4137. hbc_menu->remove_child(p_control);
  4138. }
  4139. void SpatialEditor::set_can_preview(Camera *p_preview) {
  4140. for (int i = 0; i < 4; i++) {
  4141. viewports[i]->set_can_preview(p_preview);
  4142. }
  4143. }
  4144. VSplitContainer *SpatialEditor::get_shader_split() {
  4145. return shader_split;
  4146. }
  4147. HSplitContainer *SpatialEditor::get_palette_split() {
  4148. return palette_split;
  4149. }
  4150. void SpatialEditor::_request_gizmo(Object *p_obj) {
  4151. Spatial *sp = Object::cast_to<Spatial>(p_obj);
  4152. if (!sp)
  4153. return;
  4154. if (editor->get_edited_scene() && (sp == editor->get_edited_scene() || (sp->get_owner() && editor->get_edited_scene()->is_a_parent_of(sp)))) {
  4155. Ref<EditorSpatialGizmo> seg;
  4156. for (int i = 0; i < gizmo_plugins_by_priority.size(); ++i) {
  4157. seg = gizmo_plugins_by_priority.write[i]->get_gizmo(sp);
  4158. if (seg.is_valid()) {
  4159. sp->set_gizmo(seg);
  4160. if (sp == selected) {
  4161. seg->set_selected(true);
  4162. selected->update_gizmo();
  4163. }
  4164. break;
  4165. }
  4166. }
  4167. }
  4168. }
  4169. void SpatialEditor::_toggle_maximize_view(Object *p_viewport) {
  4170. if (!p_viewport) return;
  4171. SpatialEditorViewport *current_viewport = Object::cast_to<SpatialEditorViewport>(p_viewport);
  4172. if (!current_viewport) return;
  4173. int index = -1;
  4174. bool maximized = false;
  4175. for (int i = 0; i < 4; i++) {
  4176. if (viewports[i] == current_viewport) {
  4177. index = i;
  4178. if (current_viewport->get_global_rect() == viewport_base->get_global_rect())
  4179. maximized = true;
  4180. break;
  4181. }
  4182. }
  4183. if (index == -1) return;
  4184. if (!maximized) {
  4185. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  4186. if (i == (uint32_t)index)
  4187. viewports[i]->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  4188. else
  4189. viewports[i]->hide();
  4190. }
  4191. } else {
  4192. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++)
  4193. viewports[i]->show();
  4194. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT)))
  4195. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  4196. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS)))
  4197. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  4198. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT)))
  4199. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  4200. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS)))
  4201. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  4202. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT)))
  4203. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  4204. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS)))
  4205. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  4206. }
  4207. }
  4208. void SpatialEditor::_node_removed(Node *p_node) {
  4209. if (p_node == selected)
  4210. selected = NULL;
  4211. }
  4212. void SpatialEditor::_register_all_gizmos() {
  4213. add_gizmo_plugin(Ref<CameraSpatialGizmoPlugin>(memnew(CameraSpatialGizmoPlugin)));
  4214. add_gizmo_plugin(Ref<LightSpatialGizmoPlugin>(memnew(LightSpatialGizmoPlugin)));
  4215. add_gizmo_plugin(Ref<AudioStreamPlayer3DSpatialGizmoPlugin>(memnew(AudioStreamPlayer3DSpatialGizmoPlugin)));
  4216. add_gizmo_plugin(Ref<MeshInstanceSpatialGizmoPlugin>(memnew(MeshInstanceSpatialGizmoPlugin)));
  4217. add_gizmo_plugin(Ref<SoftBodySpatialGizmoPlugin>(memnew(SoftBodySpatialGizmoPlugin)));
  4218. add_gizmo_plugin(Ref<Sprite3DSpatialGizmoPlugin>(memnew(Sprite3DSpatialGizmoPlugin)));
  4219. add_gizmo_plugin(Ref<SkeletonSpatialGizmoPlugin>(memnew(SkeletonSpatialGizmoPlugin)));
  4220. add_gizmo_plugin(Ref<Position3DSpatialGizmoPlugin>(memnew(Position3DSpatialGizmoPlugin)));
  4221. add_gizmo_plugin(Ref<RayCastSpatialGizmoPlugin>(memnew(RayCastSpatialGizmoPlugin)));
  4222. add_gizmo_plugin(Ref<SpringArmSpatialGizmoPlugin>(memnew(SpringArmSpatialGizmoPlugin)));
  4223. add_gizmo_plugin(Ref<VehicleWheelSpatialGizmoPlugin>(memnew(VehicleWheelSpatialGizmoPlugin)));
  4224. add_gizmo_plugin(Ref<VisibilityNotifierGizmoPlugin>(memnew(VisibilityNotifierGizmoPlugin)));
  4225. add_gizmo_plugin(Ref<ParticlesGizmoPlugin>(memnew(ParticlesGizmoPlugin)));
  4226. add_gizmo_plugin(Ref<CPUParticlesGizmoPlugin>(memnew(CPUParticlesGizmoPlugin)));
  4227. add_gizmo_plugin(Ref<ReflectionProbeGizmoPlugin>(memnew(ReflectionProbeGizmoPlugin)));
  4228. add_gizmo_plugin(Ref<GIProbeGizmoPlugin>(memnew(GIProbeGizmoPlugin)));
  4229. add_gizmo_plugin(Ref<BakedIndirectLightGizmoPlugin>(memnew(BakedIndirectLightGizmoPlugin)));
  4230. add_gizmo_plugin(Ref<CollisionShapeSpatialGizmoPlugin>(memnew(CollisionShapeSpatialGizmoPlugin)));
  4231. add_gizmo_plugin(Ref<CollisionPolygonSpatialGizmoPlugin>(memnew(CollisionPolygonSpatialGizmoPlugin)));
  4232. add_gizmo_plugin(Ref<NavigationMeshSpatialGizmoPlugin>(memnew(NavigationMeshSpatialGizmoPlugin)));
  4233. add_gizmo_plugin(Ref<JointSpatialGizmoPlugin>(memnew(JointSpatialGizmoPlugin)));
  4234. add_gizmo_plugin(Ref<PhysicalBoneSpatialGizmoPlugin>(memnew(PhysicalBoneSpatialGizmoPlugin)));
  4235. }
  4236. void SpatialEditor::_bind_methods() {
  4237. ClassDB::bind_method("_unhandled_key_input", &SpatialEditor::_unhandled_key_input);
  4238. ClassDB::bind_method("_node_removed", &SpatialEditor::_node_removed);
  4239. ClassDB::bind_method("_menu_item_pressed", &SpatialEditor::_menu_item_pressed);
  4240. ClassDB::bind_method("_menu_gizmo_toggled", &SpatialEditor::_menu_gizmo_toggled);
  4241. ClassDB::bind_method("_menu_item_toggled", &SpatialEditor::_menu_item_toggled);
  4242. ClassDB::bind_method("_xform_dialog_action", &SpatialEditor::_xform_dialog_action);
  4243. ClassDB::bind_method("_get_editor_data", &SpatialEditor::_get_editor_data);
  4244. ClassDB::bind_method("_request_gizmo", &SpatialEditor::_request_gizmo);
  4245. ClassDB::bind_method("_toggle_maximize_view", &SpatialEditor::_toggle_maximize_view);
  4246. ClassDB::bind_method("_refresh_menu_icons", &SpatialEditor::_refresh_menu_icons);
  4247. ADD_SIGNAL(MethodInfo("transform_key_request"));
  4248. ADD_SIGNAL(MethodInfo("item_lock_status_changed"));
  4249. ADD_SIGNAL(MethodInfo("item_group_status_changed"));
  4250. }
  4251. void SpatialEditor::clear() {
  4252. settings_fov->set_value(EDITOR_DEF("editors/3d/default_fov", 70.0));
  4253. settings_znear->set_value(EDITOR_DEF("editors/3d/default_z_near", 0.05));
  4254. settings_zfar->set_value(EDITOR_DEF("editors/3d/default_z_far", 1500.0));
  4255. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  4256. viewports[i]->reset();
  4257. }
  4258. VisualServer::get_singleton()->instance_set_visible(origin_instance, true);
  4259. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), true);
  4260. for (int i = 0; i < 3; ++i) {
  4261. if (grid_enable[i]) {
  4262. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], true);
  4263. grid_visible[i] = true;
  4264. }
  4265. }
  4266. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  4267. viewports[i]->view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(SpatialEditorViewport::VIEW_AUDIO_LISTENER), i == 0);
  4268. viewports[i]->viewport->set_as_audio_listener(i == 0);
  4269. }
  4270. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID), true);
  4271. }
  4272. SpatialEditor::SpatialEditor(EditorNode *p_editor) {
  4273. gizmo.visible = true;
  4274. gizmo.scale = 1.0;
  4275. viewport_environment = Ref<Environment>(memnew(Environment));
  4276. undo_redo = p_editor->get_undo_redo();
  4277. VBoxContainer *vbc = this;
  4278. custom_camera = NULL;
  4279. singleton = this;
  4280. editor = p_editor;
  4281. editor_selection = editor->get_editor_selection();
  4282. editor_selection->add_editor_plugin(this);
  4283. snap_enabled = false;
  4284. snap_key_enabled = false;
  4285. tool_mode = TOOL_MODE_SELECT;
  4286. hbc_menu = memnew(HBoxContainer);
  4287. vbc->add_child(hbc_menu);
  4288. Vector<Variant> button_binds;
  4289. button_binds.resize(1);
  4290. String sct;
  4291. tool_button[TOOL_MODE_SELECT] = memnew(ToolButton);
  4292. hbc_menu->add_child(tool_button[TOOL_MODE_SELECT]);
  4293. tool_button[TOOL_MODE_SELECT]->set_toggle_mode(true);
  4294. tool_button[TOOL_MODE_SELECT]->set_flat(true);
  4295. tool_button[TOOL_MODE_SELECT]->set_pressed(true);
  4296. button_binds.write[0] = MENU_TOOL_SELECT;
  4297. tool_button[TOOL_MODE_SELECT]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4298. tool_button[TOOL_MODE_SELECT]->set_tooltip(TTR("Select Mode (Q)") + "\n" + keycode_get_string(KEY_MASK_CMD) + TTR("Drag: Rotate\nAlt+Drag: Move\nAlt+RMB: Depth list selection"));
  4299. hbc_menu->add_child(memnew(VSeparator));
  4300. tool_button[TOOL_MODE_MOVE] = memnew(ToolButton);
  4301. hbc_menu->add_child(tool_button[TOOL_MODE_MOVE]);
  4302. tool_button[TOOL_MODE_MOVE]->set_toggle_mode(true);
  4303. tool_button[TOOL_MODE_MOVE]->set_flat(true);
  4304. button_binds.write[0] = MENU_TOOL_MOVE;
  4305. tool_button[TOOL_MODE_MOVE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4306. tool_button[TOOL_MODE_MOVE]->set_tooltip(TTR("Move Mode (W)"));
  4307. tool_button[TOOL_MODE_ROTATE] = memnew(ToolButton);
  4308. hbc_menu->add_child(tool_button[TOOL_MODE_ROTATE]);
  4309. tool_button[TOOL_MODE_ROTATE]->set_toggle_mode(true);
  4310. tool_button[TOOL_MODE_ROTATE]->set_flat(true);
  4311. button_binds.write[0] = MENU_TOOL_ROTATE;
  4312. tool_button[TOOL_MODE_ROTATE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4313. tool_button[TOOL_MODE_ROTATE]->set_tooltip(TTR("Rotate Mode (E)"));
  4314. tool_button[TOOL_MODE_SCALE] = memnew(ToolButton);
  4315. hbc_menu->add_child(tool_button[TOOL_MODE_SCALE]);
  4316. tool_button[TOOL_MODE_SCALE]->set_toggle_mode(true);
  4317. tool_button[TOOL_MODE_SCALE]->set_flat(true);
  4318. button_binds.write[0] = MENU_TOOL_SCALE;
  4319. tool_button[TOOL_MODE_SCALE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4320. tool_button[TOOL_MODE_SCALE]->set_tooltip(TTR("Scale Mode (R)"));
  4321. hbc_menu->add_child(memnew(VSeparator));
  4322. tool_button[TOOL_MODE_LIST_SELECT] = memnew(ToolButton);
  4323. hbc_menu->add_child(tool_button[TOOL_MODE_LIST_SELECT]);
  4324. tool_button[TOOL_MODE_LIST_SELECT]->set_toggle_mode(true);
  4325. tool_button[TOOL_MODE_LIST_SELECT]->set_flat(true);
  4326. button_binds.write[0] = MENU_TOOL_LIST_SELECT;
  4327. tool_button[TOOL_MODE_LIST_SELECT]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4328. 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)."));
  4329. tool_button[TOOL_LOCK_SELECTED] = memnew(ToolButton);
  4330. hbc_menu->add_child(tool_button[TOOL_LOCK_SELECTED]);
  4331. button_binds.write[0] = MENU_LOCK_SELECTED;
  4332. tool_button[TOOL_LOCK_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4333. tool_button[TOOL_LOCK_SELECTED]->set_tooltip(TTR("Lock the selected object in place (can't be moved)."));
  4334. tool_button[TOOL_UNLOCK_SELECTED] = memnew(ToolButton);
  4335. hbc_menu->add_child(tool_button[TOOL_UNLOCK_SELECTED]);
  4336. button_binds.write[0] = MENU_UNLOCK_SELECTED;
  4337. tool_button[TOOL_UNLOCK_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4338. tool_button[TOOL_UNLOCK_SELECTED]->set_tooltip(TTR("Unlock the selected object (can be moved)."));
  4339. tool_button[TOOL_GROUP_SELECTED] = memnew(ToolButton);
  4340. hbc_menu->add_child(tool_button[TOOL_GROUP_SELECTED]);
  4341. button_binds.write[0] = MENU_GROUP_SELECTED;
  4342. tool_button[TOOL_GROUP_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4343. tool_button[TOOL_GROUP_SELECTED]->set_tooltip(TTR("Makes sure the object's children are not selectable."));
  4344. tool_button[TOOL_UNGROUP_SELECTED] = memnew(ToolButton);
  4345. hbc_menu->add_child(tool_button[TOOL_UNGROUP_SELECTED]);
  4346. button_binds.write[0] = MENU_UNGROUP_SELECTED;
  4347. tool_button[TOOL_UNGROUP_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  4348. tool_button[TOOL_UNGROUP_SELECTED]->set_tooltip(TTR("Restores the object's children's ability to be selected."));
  4349. hbc_menu->add_child(memnew(VSeparator));
  4350. tool_option_button[TOOL_OPT_LOCAL_COORDS] = memnew(ToolButton);
  4351. hbc_menu->add_child(tool_option_button[TOOL_OPT_LOCAL_COORDS]);
  4352. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_toggle_mode(true);
  4353. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_flat(true);
  4354. button_binds.write[0] = MENU_TOOL_LOCAL_COORDS;
  4355. tool_option_button[TOOL_OPT_LOCAL_COORDS]->connect("toggled", this, "_menu_item_toggled", button_binds);
  4356. ED_SHORTCUT("spatial_editor/local_coords", TTR("Local Coords"), KEY_T);
  4357. sct = ED_GET_SHORTCUT("spatial_editor/local_coords").ptr()->get_as_text();
  4358. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_tooltip(vformat(TTR("Local Space Mode (%s)"), sct));
  4359. tool_option_button[TOOL_OPT_USE_SNAP] = memnew(ToolButton);
  4360. hbc_menu->add_child(tool_option_button[TOOL_OPT_USE_SNAP]);
  4361. tool_option_button[TOOL_OPT_USE_SNAP]->set_toggle_mode(true);
  4362. tool_option_button[TOOL_OPT_USE_SNAP]->set_flat(true);
  4363. button_binds.write[0] = MENU_TOOL_USE_SNAP;
  4364. tool_option_button[TOOL_OPT_USE_SNAP]->connect("toggled", this, "_menu_item_toggled", button_binds);
  4365. ED_SHORTCUT("spatial_editor/snap", TTR("Snap"), KEY_Y);
  4366. sct = ED_GET_SHORTCUT("spatial_editor/snap").ptr()->get_as_text();
  4367. tool_option_button[TOOL_OPT_USE_SNAP]->set_tooltip(vformat(TTR("Snap Mode (%s)"), sct));
  4368. hbc_menu->add_child(memnew(VSeparator));
  4369. // Drag and drop support;
  4370. preview_node = memnew(Spatial);
  4371. preview_bounds = AABB();
  4372. ED_SHORTCUT("spatial_editor/bottom_view", TTR("Bottom View"), KEY_MASK_ALT + KEY_KP_7);
  4373. ED_SHORTCUT("spatial_editor/top_view", TTR("Top View"), KEY_KP_7);
  4374. ED_SHORTCUT("spatial_editor/rear_view", TTR("Rear View"), KEY_MASK_ALT + KEY_KP_1);
  4375. ED_SHORTCUT("spatial_editor/front_view", TTR("Front View"), KEY_KP_1);
  4376. ED_SHORTCUT("spatial_editor/left_view", TTR("Left View"), KEY_MASK_ALT + KEY_KP_3);
  4377. ED_SHORTCUT("spatial_editor/right_view", TTR("Right View"), KEY_KP_3);
  4378. ED_SHORTCUT("spatial_editor/switch_perspective_orthogonal", TTR("Switch Perspective/Orthogonal View"), KEY_KP_5);
  4379. ED_SHORTCUT("spatial_editor/insert_anim_key", TTR("Insert Animation Key"), KEY_K);
  4380. ED_SHORTCUT("spatial_editor/focus_origin", TTR("Focus Origin"), KEY_O);
  4381. ED_SHORTCUT("spatial_editor/focus_selection", TTR("Focus Selection"), KEY_F);
  4382. ED_SHORTCUT("spatial_editor/align_transform_with_view", TTR("Align Transform with View"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_M);
  4383. ED_SHORTCUT("spatial_editor/align_rotation_with_view", TTR("Align Rotation with View"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_F);
  4384. ED_SHORTCUT("spatial_editor/tool_select", TTR("Tool Select"), KEY_Q);
  4385. ED_SHORTCUT("spatial_editor/tool_move", TTR("Tool Move"), KEY_W);
  4386. ED_SHORTCUT("spatial_editor/tool_rotate", TTR("Tool Rotate"), KEY_E);
  4387. ED_SHORTCUT("spatial_editor/tool_scale", TTR("Tool Scale"), KEY_R);
  4388. ED_SHORTCUT("spatial_editor/freelook_toggle", TTR("Toggle Freelook"), KEY_MASK_SHIFT + KEY_F);
  4389. PopupMenu *p;
  4390. transform_menu = memnew(MenuButton);
  4391. transform_menu->set_text(TTR("Transform"));
  4392. transform_menu->set_switch_on_hover(true);
  4393. hbc_menu->add_child(transform_menu);
  4394. p = transform_menu->get_popup();
  4395. p->add_shortcut(ED_SHORTCUT("spatial_editor/snap_to_floor", TTR("Snap Object to Floor"), KEY_PAGEDOWN), MENU_SNAP_TO_FLOOR);
  4396. p->add_shortcut(ED_SHORTCUT("spatial_editor/transform_dialog", TTR("Transform Dialog...")), MENU_TRANSFORM_DIALOG);
  4397. p->add_separator();
  4398. p->add_shortcut(ED_SHORTCUT("spatial_editor/configure_snap", TTR("Configure Snap...")), MENU_TRANSFORM_CONFIGURE_SNAP);
  4399. p->connect("id_pressed", this, "_menu_item_pressed");
  4400. view_menu = memnew(MenuButton);
  4401. view_menu->set_text(TTR("View"));
  4402. view_menu->set_switch_on_hover(true);
  4403. hbc_menu->add_child(view_menu);
  4404. p = view_menu->get_popup();
  4405. accept = memnew(AcceptDialog);
  4406. editor->get_gui_base()->add_child(accept);
  4407. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/1_viewport", TTR("1 Viewport"), KEY_MASK_CMD + KEY_1), MENU_VIEW_USE_1_VIEWPORT);
  4408. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports", TTR("2 Viewports"), KEY_MASK_CMD + KEY_2), MENU_VIEW_USE_2_VIEWPORTS);
  4409. 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);
  4410. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports", TTR("3 Viewports"), KEY_MASK_CMD + KEY_3), MENU_VIEW_USE_3_VIEWPORTS);
  4411. 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);
  4412. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/4_viewports", TTR("4 Viewports"), KEY_MASK_CMD + KEY_4), MENU_VIEW_USE_4_VIEWPORTS);
  4413. p->add_separator();
  4414. p->add_submenu_item(TTR("Gizmos"), "GizmosMenu");
  4415. p->add_separator();
  4416. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_origin", TTR("View Origin")), MENU_VIEW_ORIGIN);
  4417. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_grid", TTR("View Grid")), MENU_VIEW_GRID);
  4418. p->add_separator();
  4419. p->add_shortcut(ED_SHORTCUT("spatial_editor/settings", TTR("Settings...")), MENU_VIEW_CAMERA_SETTINGS);
  4420. p->set_item_checked(p->get_item_index(MENU_VIEW_ORIGIN), true);
  4421. p->set_item_checked(p->get_item_index(MENU_VIEW_GRID), true);
  4422. p->connect("id_pressed", this, "_menu_item_pressed");
  4423. gizmos_menu = memnew(PopupMenu);
  4424. p->add_child(gizmos_menu);
  4425. gizmos_menu->set_name("GizmosMenu");
  4426. gizmos_menu->set_hide_on_checkable_item_selection(false);
  4427. gizmos_menu->connect("id_pressed", this, "_menu_gizmo_toggled");
  4428. /* REST OF MENU */
  4429. palette_split = memnew(HSplitContainer);
  4430. palette_split->set_v_size_flags(SIZE_EXPAND_FILL);
  4431. vbc->add_child(palette_split);
  4432. shader_split = memnew(VSplitContainer);
  4433. shader_split->set_h_size_flags(SIZE_EXPAND_FILL);
  4434. palette_split->add_child(shader_split);
  4435. viewport_base = memnew(SpatialEditorViewportContainer);
  4436. shader_split->add_child(viewport_base);
  4437. viewport_base->set_v_size_flags(SIZE_EXPAND_FILL);
  4438. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  4439. viewports[i] = memnew(SpatialEditorViewport(this, editor, i));
  4440. viewports[i]->connect("toggle_maximize_view", this, "_toggle_maximize_view");
  4441. viewports[i]->assign_pending_data_pointers(preview_node, &preview_bounds, accept);
  4442. viewport_base->add_child(viewports[i]);
  4443. }
  4444. /* SNAP DIALOG */
  4445. snap_dialog = memnew(ConfirmationDialog);
  4446. snap_dialog->set_title(TTR("Snap Settings"));
  4447. add_child(snap_dialog);
  4448. VBoxContainer *snap_dialog_vbc = memnew(VBoxContainer);
  4449. snap_dialog->add_child(snap_dialog_vbc);
  4450. snap_translate = memnew(LineEdit);
  4451. snap_translate->set_text("1");
  4452. snap_dialog_vbc->add_margin_child(TTR("Translate Snap:"), snap_translate);
  4453. snap_rotate = memnew(LineEdit);
  4454. snap_rotate->set_text("15");
  4455. snap_dialog_vbc->add_margin_child(TTR("Rotate Snap (deg.):"), snap_rotate);
  4456. snap_scale = memnew(LineEdit);
  4457. snap_scale->set_text("10");
  4458. snap_dialog_vbc->add_margin_child(TTR("Scale Snap (%):"), snap_scale);
  4459. /* SETTINGS DIALOG */
  4460. settings_dialog = memnew(ConfirmationDialog);
  4461. settings_dialog->set_title(TTR("Viewport Settings"));
  4462. add_child(settings_dialog);
  4463. settings_vbc = memnew(VBoxContainer);
  4464. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  4465. settings_dialog->add_child(settings_vbc);
  4466. settings_fov = memnew(SpinBox);
  4467. settings_fov->set_max(MAX_FOV);
  4468. settings_fov->set_min(MIN_FOV);
  4469. settings_fov->set_step(0.01);
  4470. settings_fov->set_value(EDITOR_DEF("editors/3d/default_fov", 70.0));
  4471. settings_vbc->add_margin_child(TTR("Perspective FOV (deg.):"), settings_fov);
  4472. settings_znear = memnew(SpinBox);
  4473. settings_znear->set_max(MAX_Z);
  4474. settings_znear->set_min(MIN_Z);
  4475. settings_znear->set_step(0.01);
  4476. settings_znear->set_value(EDITOR_DEF("editors/3d/default_z_near", 0.05));
  4477. settings_vbc->add_margin_child(TTR("View Z-Near:"), settings_znear);
  4478. settings_zfar = memnew(SpinBox);
  4479. settings_zfar->set_max(MAX_Z);
  4480. settings_zfar->set_min(MIN_Z);
  4481. settings_zfar->set_step(0.01);
  4482. settings_zfar->set_value(EDITOR_DEF("editors/3d/default_z_far", 1500));
  4483. settings_vbc->add_margin_child(TTR("View Z-Far:"), settings_zfar);
  4484. /* XFORM DIALOG */
  4485. xform_dialog = memnew(ConfirmationDialog);
  4486. xform_dialog->set_title(TTR("Transform Change"));
  4487. add_child(xform_dialog);
  4488. VBoxContainer *xform_vbc = memnew(VBoxContainer);
  4489. xform_dialog->add_child(xform_vbc);
  4490. Label *l = memnew(Label);
  4491. l->set_text(TTR("Translate:"));
  4492. xform_vbc->add_child(l);
  4493. HBoxContainer *xform_hbc = memnew(HBoxContainer);
  4494. xform_vbc->add_child(xform_hbc);
  4495. for (int i = 0; i < 3; i++) {
  4496. xform_translate[i] = memnew(LineEdit);
  4497. xform_translate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  4498. xform_hbc->add_child(xform_translate[i]);
  4499. }
  4500. l = memnew(Label);
  4501. l->set_text(TTR("Rotate (deg.):"));
  4502. xform_vbc->add_child(l);
  4503. xform_hbc = memnew(HBoxContainer);
  4504. xform_vbc->add_child(xform_hbc);
  4505. for (int i = 0; i < 3; i++) {
  4506. xform_rotate[i] = memnew(LineEdit);
  4507. xform_rotate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  4508. xform_hbc->add_child(xform_rotate[i]);
  4509. }
  4510. l = memnew(Label);
  4511. l->set_text(TTR("Scale (ratio):"));
  4512. xform_vbc->add_child(l);
  4513. xform_hbc = memnew(HBoxContainer);
  4514. xform_vbc->add_child(xform_hbc);
  4515. for (int i = 0; i < 3; i++) {
  4516. xform_scale[i] = memnew(LineEdit);
  4517. xform_scale[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  4518. xform_hbc->add_child(xform_scale[i]);
  4519. }
  4520. l = memnew(Label);
  4521. l->set_text(TTR("Transform Type"));
  4522. xform_vbc->add_child(l);
  4523. xform_type = memnew(OptionButton);
  4524. xform_type->set_h_size_flags(SIZE_EXPAND_FILL);
  4525. xform_type->add_item(TTR("Pre"));
  4526. xform_type->add_item(TTR("Post"));
  4527. xform_vbc->add_child(xform_type);
  4528. xform_dialog->connect("confirmed", this, "_xform_dialog_action");
  4529. scenario_debug = VisualServer::SCENARIO_DEBUG_DISABLED;
  4530. selected = NULL;
  4531. set_process_unhandled_key_input(true);
  4532. add_to_group("_spatial_editor_group");
  4533. EDITOR_DEF("editors/3d/manipulator_gizmo_size", 80);
  4534. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/3d/manipulator_gizmo_size", PROPERTY_HINT_RANGE, "16,1024,1"));
  4535. EDITOR_DEF("editors/3d/manipulator_gizmo_opacity", 0.4);
  4536. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::REAL, "editors/3d/manipulator_gizmo_opacity", PROPERTY_HINT_RANGE, "0,1,0.01"));
  4537. over_gizmo_handle = -1;
  4538. }
  4539. SpatialEditor::~SpatialEditor() {
  4540. memdelete(preview_node);
  4541. }
  4542. void SpatialEditorPlugin::make_visible(bool p_visible) {
  4543. if (p_visible) {
  4544. spatial_editor->show();
  4545. spatial_editor->set_process(true);
  4546. } else {
  4547. spatial_editor->hide();
  4548. spatial_editor->set_process(false);
  4549. }
  4550. }
  4551. void SpatialEditorPlugin::edit(Object *p_object) {
  4552. spatial_editor->edit(Object::cast_to<Spatial>(p_object));
  4553. }
  4554. bool SpatialEditorPlugin::handles(Object *p_object) const {
  4555. return p_object->is_class("Spatial");
  4556. }
  4557. Dictionary SpatialEditorPlugin::get_state() const {
  4558. return spatial_editor->get_state();
  4559. }
  4560. void SpatialEditorPlugin::set_state(const Dictionary &p_state) {
  4561. spatial_editor->set_state(p_state);
  4562. }
  4563. void SpatialEditor::snap_cursor_to_plane(const Plane &p_plane) {
  4564. //cursor.pos=p_plane.project(cursor.pos);
  4565. }
  4566. Vector3 SpatialEditor::snap_point(Vector3 p_target, Vector3 p_start) const {
  4567. if (is_snap_enabled()) {
  4568. p_target.x = Math::snap_scalar(0.0, get_translate_snap(), p_target.x);
  4569. p_target.y = Math::snap_scalar(0.0, get_translate_snap(), p_target.y);
  4570. p_target.z = Math::snap_scalar(0.0, get_translate_snap(), p_target.z);
  4571. }
  4572. return p_target;
  4573. }
  4574. float SpatialEditor::get_translate_snap() const {
  4575. float snap_value;
  4576. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  4577. snap_value = snap_translate->get_text().to_double() / 10.0;
  4578. } else {
  4579. snap_value = snap_translate->get_text().to_double();
  4580. }
  4581. return snap_value;
  4582. }
  4583. float SpatialEditor::get_rotate_snap() const {
  4584. float snap_value;
  4585. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  4586. snap_value = snap_rotate->get_text().to_double() / 3.0;
  4587. } else {
  4588. snap_value = snap_rotate->get_text().to_double();
  4589. }
  4590. return snap_value;
  4591. }
  4592. float SpatialEditor::get_scale_snap() const {
  4593. float snap_value;
  4594. if (Input::get_singleton()->is_key_pressed(KEY_SHIFT)) {
  4595. snap_value = snap_scale->get_text().to_double() / 2.0;
  4596. } else {
  4597. snap_value = snap_scale->get_text().to_double();
  4598. }
  4599. return snap_value;
  4600. }
  4601. void SpatialEditorPlugin::_bind_methods() {
  4602. ClassDB::bind_method("snap_cursor_to_plane", &SpatialEditorPlugin::snap_cursor_to_plane);
  4603. }
  4604. void SpatialEditorPlugin::snap_cursor_to_plane(const Plane &p_plane) {
  4605. spatial_editor->snap_cursor_to_plane(p_plane);
  4606. }
  4607. struct _GizmoPluginPriorityComparator {
  4608. bool operator()(const Ref<EditorSpatialGizmoPlugin> &p_a, const Ref<EditorSpatialGizmoPlugin> &p_b) const {
  4609. if (p_a->get_priority() == p_b->get_priority()) {
  4610. return p_a->get_name() < p_b->get_name();
  4611. }
  4612. return p_a->get_priority() > p_b->get_priority();
  4613. }
  4614. };
  4615. struct _GizmoPluginNameComparator {
  4616. bool operator()(const Ref<EditorSpatialGizmoPlugin> &p_a, const Ref<EditorSpatialGizmoPlugin> &p_b) const {
  4617. return p_a->get_name() < p_b->get_name();
  4618. }
  4619. };
  4620. void SpatialEditor::add_gizmo_plugin(Ref<EditorSpatialGizmoPlugin> p_plugin) {
  4621. ERR_FAIL_NULL(p_plugin.ptr());
  4622. gizmo_plugins_by_priority.push_back(p_plugin);
  4623. gizmo_plugins_by_priority.sort_custom<_GizmoPluginPriorityComparator>();
  4624. gizmo_plugins_by_name.push_back(p_plugin);
  4625. gizmo_plugins_by_name.sort_custom<_GizmoPluginNameComparator>();
  4626. _update_gizmos_menu();
  4627. SpatialEditor::get_singleton()->update_all_gizmos();
  4628. }
  4629. void SpatialEditor::remove_gizmo_plugin(Ref<EditorSpatialGizmoPlugin> p_plugin) {
  4630. gizmo_plugins_by_priority.erase(p_plugin);
  4631. gizmo_plugins_by_name.erase(p_plugin);
  4632. _update_gizmos_menu();
  4633. }
  4634. SpatialEditorPlugin::SpatialEditorPlugin(EditorNode *p_node) {
  4635. editor = p_node;
  4636. spatial_editor = memnew(SpatialEditor(p_node));
  4637. spatial_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  4638. editor->get_viewport()->add_child(spatial_editor);
  4639. spatial_editor->hide();
  4640. spatial_editor->connect("transform_key_request", editor->get_inspector_dock(), "_transform_keyed");
  4641. }
  4642. SpatialEditorPlugin::~SpatialEditorPlugin() {
  4643. }
  4644. void EditorSpatialGizmoPlugin::create_material(const String &p_name, const Color &p_color, bool p_billboard, bool p_on_top, bool p_use_vertex_color) {
  4645. Color instanced_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/instanced", Color(0.7, 0.7, 0.7, 0.6));
  4646. Vector<Ref<SpatialMaterial> > mats;
  4647. for (int i = 0; i < 4; i++) {
  4648. bool selected = i % 2 == 1;
  4649. bool instanced = i < 2;
  4650. Ref<SpatialMaterial> material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  4651. Color color = instanced ? instanced_color : p_color;
  4652. if (!selected) {
  4653. color.a *= 0.3;
  4654. }
  4655. material->set_albedo(color);
  4656. material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  4657. material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  4658. material->set_render_priority(SpatialMaterial::RENDER_PRIORITY_MIN + 1);
  4659. if (p_use_vertex_color) {
  4660. material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  4661. material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  4662. }
  4663. if (p_billboard) {
  4664. material->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
  4665. }
  4666. if (p_on_top && selected) {
  4667. material->set_on_top_of_alpha();
  4668. }
  4669. mats.push_back(material);
  4670. }
  4671. materials[p_name] = mats;
  4672. }
  4673. void EditorSpatialGizmoPlugin::create_icon_material(const String &p_name, const Ref<Texture> &p_texture, bool p_on_top, const Color &p_albedo) {
  4674. Color instanced_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/instanced", Color(0.7, 0.7, 0.7, 0.6));
  4675. Vector<Ref<SpatialMaterial> > icons;
  4676. for (int i = 0; i < 4; i++) {
  4677. bool selected = i % 2 == 1;
  4678. bool instanced = i < 2;
  4679. Ref<SpatialMaterial> icon = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  4680. Color color = instanced ? instanced_color : p_albedo;
  4681. if (!selected) {
  4682. color.a *= 0.85;
  4683. }
  4684. icon->set_albedo(color);
  4685. icon->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  4686. icon->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  4687. icon->set_depth_draw_mode(SpatialMaterial::DEPTH_DRAW_DISABLED);
  4688. icon->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  4689. icon->set_texture(SpatialMaterial::TEXTURE_ALBEDO, p_texture);
  4690. icon->set_flag(SpatialMaterial::FLAG_FIXED_SIZE, true);
  4691. icon->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
  4692. icon->set_render_priority(SpatialMaterial::RENDER_PRIORITY_MIN);
  4693. if (p_on_top && selected) {
  4694. icon->set_on_top_of_alpha();
  4695. }
  4696. icons.push_back(icon);
  4697. }
  4698. materials[p_name] = icons;
  4699. }
  4700. void EditorSpatialGizmoPlugin::create_handle_material(const String &p_name, bool p_billboard) {
  4701. Ref<SpatialMaterial> handle_material = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  4702. handle_material->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  4703. handle_material->set_flag(SpatialMaterial::FLAG_USE_POINT_SIZE, true);
  4704. Ref<Texture> handle_t = SpatialEditor::get_singleton()->get_icon("Editor3DHandle", "EditorIcons");
  4705. handle_material->set_point_size(handle_t->get_width());
  4706. handle_material->set_texture(SpatialMaterial::TEXTURE_ALBEDO, handle_t);
  4707. handle_material->set_albedo(Color(1, 1, 1));
  4708. handle_material->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  4709. handle_material->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  4710. handle_material->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  4711. handle_material->set_on_top_of_alpha();
  4712. if (p_billboard) {
  4713. handle_material->set_billboard_mode(SpatialMaterial::BILLBOARD_ENABLED);
  4714. handle_material->set_on_top_of_alpha();
  4715. }
  4716. materials[p_name] = Vector<Ref<SpatialMaterial> >();
  4717. materials[p_name].push_back(handle_material);
  4718. }
  4719. void EditorSpatialGizmoPlugin::add_material(const String &p_name, Ref<SpatialMaterial> p_material) {
  4720. materials[p_name] = Vector<Ref<SpatialMaterial> >();
  4721. materials[p_name].push_back(p_material);
  4722. }
  4723. Ref<SpatialMaterial> EditorSpatialGizmoPlugin::get_material(const String &p_name, const Ref<EditorSpatialGizmo> &p_gizmo) {
  4724. ERR_FAIL_COND_V(!materials.has(p_name), Ref<SpatialMaterial>());
  4725. ERR_FAIL_COND_V(materials[p_name].size() == 0, Ref<SpatialMaterial>());
  4726. if (p_gizmo.is_null() || materials[p_name].size() == 1) return materials[p_name][0];
  4727. int index = (p_gizmo->is_selected() ? 1 : 0) + (p_gizmo->is_editable() ? 2 : 0);
  4728. Ref<SpatialMaterial> mat = materials[p_name][index];
  4729. if (current_state == ON_TOP && p_gizmo->is_selected()) {
  4730. mat->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, true);
  4731. } else {
  4732. mat->set_flag(SpatialMaterial::FLAG_DISABLE_DEPTH_TEST, false);
  4733. }
  4734. return mat;
  4735. }
  4736. String EditorSpatialGizmoPlugin::get_name() const {
  4737. if (get_script_instance() && get_script_instance()->has_method("get_name")) {
  4738. return get_script_instance()->call("get_name");
  4739. }
  4740. return TTR("Nameless gizmo");
  4741. }
  4742. int EditorSpatialGizmoPlugin::get_priority() const {
  4743. if (get_script_instance() && get_script_instance()->has_method("get_priority")) {
  4744. return get_script_instance()->call("get_priority");
  4745. }
  4746. return 0;
  4747. }
  4748. Ref<EditorSpatialGizmo> EditorSpatialGizmoPlugin::get_gizmo(Spatial *p_spatial) {
  4749. if (get_script_instance() && get_script_instance()->has_method("get_gizmo")) {
  4750. return get_script_instance()->call("get_gizmo", p_spatial);
  4751. }
  4752. Ref<EditorSpatialGizmo> ref = create_gizmo(p_spatial);
  4753. if (ref.is_null()) return ref;
  4754. ref->set_plugin(this);
  4755. ref->set_spatial_node(p_spatial);
  4756. ref->set_hidden(current_state == HIDDEN);
  4757. current_gizmos.push_back(ref.ptr());
  4758. return ref;
  4759. }
  4760. void EditorSpatialGizmoPlugin::_bind_methods() {
  4761. #define GIZMO_REF PropertyInfo(Variant::OBJECT, "gizmo", PROPERTY_HINT_RESOURCE_TYPE, "EditorSpatialGizmo")
  4762. BIND_VMETHOD(MethodInfo(Variant::BOOL, "has_gizmo", PropertyInfo(Variant::OBJECT, "spatial", PROPERTY_HINT_RESOURCE_TYPE, "Spatial")));
  4763. BIND_VMETHOD(MethodInfo(GIZMO_REF, "create_gizmo", PropertyInfo(Variant::OBJECT, "spatial", PROPERTY_HINT_RESOURCE_TYPE, "Spatial")));
  4764. ClassDB::bind_method(D_METHOD("create_material", "name", "color", "billboard", "on_top", "use_vertex_color"), &EditorSpatialGizmoPlugin::create_material, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  4765. 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)));
  4766. ClassDB::bind_method(D_METHOD("create_handle_material", "name", "billboard"), &EditorSpatialGizmoPlugin::create_handle_material, DEFVAL(false));
  4767. ClassDB::bind_method(D_METHOD("add_material", "name", "material"), &EditorSpatialGizmoPlugin::add_material);
  4768. ClassDB::bind_method(D_METHOD("get_material", "name", "gizmo"), &EditorSpatialGizmoPlugin::get_material); //, DEFVAL(Ref<EditorSpatialGizmo>()));
  4769. BIND_VMETHOD(MethodInfo(Variant::STRING, "get_name"));
  4770. BIND_VMETHOD(MethodInfo(Variant::STRING, "get_priority"));
  4771. BIND_VMETHOD(MethodInfo(Variant::BOOL, "can_be_hidden"));
  4772. BIND_VMETHOD(MethodInfo(Variant::BOOL, "is_selectable_when_hidden"));
  4773. BIND_VMETHOD(MethodInfo("redraw", GIZMO_REF));
  4774. BIND_VMETHOD(MethodInfo(Variant::STRING, "get_handle_name", GIZMO_REF, PropertyInfo(Variant::INT, "index")));
  4775. MethodInfo hvget(Variant::NIL, "get_handle_value", GIZMO_REF, PropertyInfo(Variant::INT, "index"));
  4776. hvget.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  4777. BIND_VMETHOD(hvget);
  4778. BIND_VMETHOD(MethodInfo("set_handle", GIZMO_REF, PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera"), PropertyInfo(Variant::VECTOR2, "point")));
  4779. MethodInfo cm = MethodInfo("commit_handle", GIZMO_REF, PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::NIL, "restore"), PropertyInfo(Variant::BOOL, "cancel"));
  4780. cm.default_arguments.push_back(false);
  4781. BIND_VMETHOD(cm);
  4782. BIND_VMETHOD(MethodInfo(Variant::BOOL, "is_handle_highlighted", GIZMO_REF, PropertyInfo(Variant::INT, "index")));
  4783. #undef GIZMO_REF
  4784. }
  4785. bool EditorSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  4786. if (get_script_instance() && get_script_instance()->has_method("has_gizmo")) {
  4787. return get_script_instance()->call("has_gizmo", p_spatial);
  4788. }
  4789. return false;
  4790. }
  4791. Ref<EditorSpatialGizmo> EditorSpatialGizmoPlugin::create_gizmo(Spatial *p_spatial) {
  4792. if (get_script_instance() && get_script_instance()->has_method("create_gizmo")) {
  4793. return get_script_instance()->call("create_gizmo", p_spatial);
  4794. }
  4795. Ref<EditorSpatialGizmo> ref;
  4796. if (has_gizmo(p_spatial)) ref.instance();
  4797. return ref;
  4798. }
  4799. bool EditorSpatialGizmoPlugin::can_be_hidden() const {
  4800. if (get_script_instance() && get_script_instance()->has_method("can_be_hidden")) {
  4801. return get_script_instance()->call("can_be_hidden");
  4802. }
  4803. return true;
  4804. }
  4805. bool EditorSpatialGizmoPlugin::is_selectable_when_hidden() const {
  4806. if (get_script_instance() && get_script_instance()->has_method("is_selectable_when_hidden")) {
  4807. return get_script_instance()->call("is_selectable_when_hidden");
  4808. }
  4809. return false;
  4810. }
  4811. void EditorSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  4812. if (get_script_instance() && get_script_instance()->has_method("redraw")) {
  4813. Ref<EditorSpatialGizmo> ref(p_gizmo);
  4814. get_script_instance()->call("redraw", ref);
  4815. }
  4816. }
  4817. String EditorSpatialGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  4818. if (get_script_instance() && get_script_instance()->has_method("get_handle_name")) {
  4819. return get_script_instance()->call("get_handle_name", p_gizmo, p_idx);
  4820. }
  4821. return "";
  4822. }
  4823. Variant EditorSpatialGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  4824. if (get_script_instance() && get_script_instance()->has_method("get_handle_value")) {
  4825. return get_script_instance()->call("get_handle_value", p_gizmo, p_idx);
  4826. }
  4827. return Variant();
  4828. }
  4829. void EditorSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  4830. if (get_script_instance() && get_script_instance()->has_method("set_handle")) {
  4831. get_script_instance()->call("set_handle", p_gizmo, p_idx, p_camera, p_point);
  4832. }
  4833. }
  4834. void EditorSpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  4835. if (get_script_instance() && get_script_instance()->has_method("commit_handle")) {
  4836. get_script_instance()->call("commit_handle", p_gizmo, p_idx, p_restore, p_cancel);
  4837. }
  4838. }
  4839. bool EditorSpatialGizmoPlugin::is_handle_highlighted(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  4840. if (get_script_instance() && get_script_instance()->has_method("is_handle_highlighted")) {
  4841. return get_script_instance()->call("is_handle_highlighted", p_gizmo, p_idx);
  4842. }
  4843. return false;
  4844. }
  4845. void EditorSpatialGizmoPlugin::set_state(int p_state) {
  4846. current_state = p_state;
  4847. for (int i = 0; i < current_gizmos.size(); ++i) {
  4848. current_gizmos[i]->set_hidden(current_state == HIDDEN);
  4849. }
  4850. }
  4851. int EditorSpatialGizmoPlugin::get_state() const {
  4852. return current_state;
  4853. }
  4854. void EditorSpatialGizmoPlugin::unregister_gizmo(EditorSpatialGizmo *p_gizmo) {
  4855. current_gizmos.erase(p_gizmo);
  4856. }
  4857. EditorSpatialGizmoPlugin::EditorSpatialGizmoPlugin() {
  4858. current_state = VISIBLE;
  4859. }
  4860. EditorSpatialGizmoPlugin::~EditorSpatialGizmoPlugin() {
  4861. for (int i = 0; i < current_gizmos.size(); ++i) {
  4862. current_gizmos[i]->set_plugin(NULL);
  4863. current_gizmos[i]->get_spatial_node()->set_gizmo(NULL);
  4864. }
  4865. SpatialEditor::get_singleton()->update_all_gizmos();
  4866. }