spatial_editor_plugin.cpp 192 KB

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