spatial_editor_plugin.cpp 212 KB

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