spatial_editor_plugin.cpp 216 KB

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