spatial_editor_plugin.cpp 195 KB

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