spatial_editor_plugin.cpp 183 KB

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