node_3d_editor_plugin.cpp 353 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430
  1. /**************************************************************************/
  2. /* node_3d_editor_plugin.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "node_3d_editor_plugin.h"
  31. #include "core/config/project_settings.h"
  32. #include "core/input/input.h"
  33. #include "core/input/input_map.h"
  34. #include "core/math/math_funcs.h"
  35. #include "core/math/projection.h"
  36. #include "core/os/keyboard.h"
  37. #include "editor/debugger/editor_debugger_node.h"
  38. #include "editor/editor_main_screen.h"
  39. #include "editor/editor_node.h"
  40. #include "editor/editor_settings.h"
  41. #include "editor/editor_string_names.h"
  42. #include "editor/editor_undo_redo_manager.h"
  43. #include "editor/gui/editor_run_bar.h"
  44. #include "editor/gui/editor_spin_slider.h"
  45. #include "editor/plugins/animation_player_editor_plugin.h"
  46. #include "editor/plugins/gizmos/audio_listener_3d_gizmo_plugin.h"
  47. #include "editor/plugins/gizmos/audio_stream_player_3d_gizmo_plugin.h"
  48. #include "editor/plugins/gizmos/camera_3d_gizmo_plugin.h"
  49. #include "editor/plugins/gizmos/collision_object_3d_gizmo_plugin.h"
  50. #include "editor/plugins/gizmos/collision_polygon_3d_gizmo_plugin.h"
  51. #include "editor/plugins/gizmos/collision_shape_3d_gizmo_plugin.h"
  52. #include "editor/plugins/gizmos/cpu_particles_3d_gizmo_plugin.h"
  53. #include "editor/plugins/gizmos/decal_gizmo_plugin.h"
  54. #include "editor/plugins/gizmos/fog_volume_gizmo_plugin.h"
  55. #include "editor/plugins/gizmos/geometry_instance_3d_gizmo_plugin.h"
  56. #include "editor/plugins/gizmos/gpu_particles_3d_gizmo_plugin.h"
  57. #include "editor/plugins/gizmos/gpu_particles_collision_3d_gizmo_plugin.h"
  58. #include "editor/plugins/gizmos/joint_3d_gizmo_plugin.h"
  59. #include "editor/plugins/gizmos/label_3d_gizmo_plugin.h"
  60. #include "editor/plugins/gizmos/light_3d_gizmo_plugin.h"
  61. #include "editor/plugins/gizmos/lightmap_gi_gizmo_plugin.h"
  62. #include "editor/plugins/gizmos/lightmap_probe_gizmo_plugin.h"
  63. #include "editor/plugins/gizmos/marker_3d_gizmo_plugin.h"
  64. #include "editor/plugins/gizmos/mesh_instance_3d_gizmo_plugin.h"
  65. #include "editor/plugins/gizmos/navigation_link_3d_gizmo_plugin.h"
  66. #include "editor/plugins/gizmos/navigation_region_3d_gizmo_plugin.h"
  67. #include "editor/plugins/gizmos/occluder_instance_3d_gizmo_plugin.h"
  68. #include "editor/plugins/gizmos/physics_bone_3d_gizmo_plugin.h"
  69. #include "editor/plugins/gizmos/ray_cast_3d_gizmo_plugin.h"
  70. #include "editor/plugins/gizmos/reflection_probe_gizmo_plugin.h"
  71. #include "editor/plugins/gizmos/shape_cast_3d_gizmo_plugin.h"
  72. #include "editor/plugins/gizmos/soft_body_3d_gizmo_plugin.h"
  73. #include "editor/plugins/gizmos/spring_arm_3d_gizmo_plugin.h"
  74. #include "editor/plugins/gizmos/sprite_base_3d_gizmo_plugin.h"
  75. #include "editor/plugins/gizmos/vehicle_body_3d_gizmo_plugin.h"
  76. #include "editor/plugins/gizmos/visible_on_screen_notifier_3d_gizmo_plugin.h"
  77. #include "editor/plugins/gizmos/voxel_gi_gizmo_plugin.h"
  78. #include "editor/plugins/node_3d_editor_gizmos.h"
  79. #include "editor/scene_tree_dock.h"
  80. #include "scene/3d/audio_stream_player_3d.h"
  81. #include "scene/3d/camera_3d.h"
  82. #include "scene/3d/decal.h"
  83. #include "scene/3d/light_3d.h"
  84. #include "scene/3d/mesh_instance_3d.h"
  85. #include "scene/3d/physics/collision_shape_3d.h"
  86. #include "scene/3d/physics/physics_body_3d.h"
  87. #include "scene/3d/sprite_3d.h"
  88. #include "scene/3d/visual_instance_3d.h"
  89. #include "scene/3d/world_environment.h"
  90. #include "scene/gui/center_container.h"
  91. #include "scene/gui/color_picker.h"
  92. #include "scene/gui/flow_container.h"
  93. #include "scene/gui/split_container.h"
  94. #include "scene/gui/subviewport_container.h"
  95. #include "scene/resources/3d/sky_material.h"
  96. #include "scene/resources/packed_scene.h"
  97. #include "scene/resources/surface_tool.h"
  98. constexpr real_t DISTANCE_DEFAULT = 4;
  99. constexpr real_t GIZMO_ARROW_SIZE = 0.35;
  100. constexpr real_t GIZMO_RING_HALF_WIDTH = 0.1;
  101. constexpr real_t GIZMO_PLANE_SIZE = 0.2;
  102. constexpr real_t GIZMO_PLANE_DST = 0.3;
  103. constexpr real_t GIZMO_CIRCLE_SIZE = 1.1;
  104. constexpr real_t GIZMO_SCALE_OFFSET = GIZMO_CIRCLE_SIZE + 0.3;
  105. constexpr real_t GIZMO_ARROW_OFFSET = GIZMO_CIRCLE_SIZE + 0.3;
  106. constexpr real_t ZOOM_FREELOOK_MIN = 0.01;
  107. constexpr real_t ZOOM_FREELOOK_MULTIPLIER = 1.08;
  108. constexpr real_t ZOOM_FREELOOK_INDICATOR_DELAY_S = 1.5;
  109. #ifdef REAL_T_IS_DOUBLE
  110. constexpr double ZOOM_FREELOOK_MAX = 1'000'000'000'000;
  111. #else
  112. constexpr float ZOOM_FREELOOK_MAX = 10'000;
  113. #endif
  114. constexpr real_t MIN_Z = 0.01;
  115. constexpr real_t MAX_Z = 1000000.0;
  116. constexpr real_t MIN_FOV = 0.01;
  117. constexpr real_t MAX_FOV = 179;
  118. void ViewportNavigationControl::_notification(int p_what) {
  119. switch (p_what) {
  120. case NOTIFICATION_ENTER_TREE: {
  121. if (!is_connected(SceneStringName(mouse_exited), callable_mp(this, &ViewportNavigationControl::_on_mouse_exited))) {
  122. connect(SceneStringName(mouse_exited), callable_mp(this, &ViewportNavigationControl::_on_mouse_exited));
  123. }
  124. if (!is_connected(SceneStringName(mouse_entered), callable_mp(this, &ViewportNavigationControl::_on_mouse_entered))) {
  125. connect(SceneStringName(mouse_entered), callable_mp(this, &ViewportNavigationControl::_on_mouse_entered));
  126. }
  127. } break;
  128. case NOTIFICATION_DRAW: {
  129. if (viewport != nullptr) {
  130. _draw();
  131. _update_navigation();
  132. }
  133. } break;
  134. }
  135. }
  136. void ViewportNavigationControl::_draw() {
  137. if (nav_mode == Node3DEditorViewport::NAVIGATION_NONE) {
  138. return;
  139. }
  140. Vector2 center = get_size() / 2.0;
  141. float radius = get_size().x / 2.0;
  142. const bool focused = focused_index != -1;
  143. draw_circle(center, radius, Color(0.5, 0.5, 0.5, focused || hovered ? 0.35 : 0.15));
  144. const Color c = focused ? Color(0.9, 0.9, 0.9, 0.9) : Color(0.5, 0.5, 0.5, 0.25);
  145. Vector2 circle_pos = focused ? center.move_toward(focused_pos, radius) : center;
  146. draw_circle(circle_pos, AXIS_CIRCLE_RADIUS, c);
  147. draw_circle(circle_pos, AXIS_CIRCLE_RADIUS * 0.8, c.darkened(0.4));
  148. }
  149. void ViewportNavigationControl::_process_click(int p_index, Vector2 p_position, bool p_pressed) {
  150. hovered = false;
  151. queue_redraw();
  152. if (focused_index != -1 && focused_index != p_index) {
  153. return;
  154. }
  155. if (p_pressed) {
  156. if (p_position.distance_to(get_size() / 2.0) < get_size().x / 2.0) {
  157. focused_pos = p_position;
  158. focused_index = p_index;
  159. queue_redraw();
  160. }
  161. } else {
  162. focused_index = -1;
  163. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
  164. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  165. Input::get_singleton()->warp_mouse(focused_mouse_start);
  166. }
  167. }
  168. }
  169. void ViewportNavigationControl::_process_drag(int p_index, Vector2 p_position, Vector2 p_relative_position) {
  170. if (focused_index == p_index) {
  171. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) {
  172. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  173. focused_mouse_start = p_position;
  174. }
  175. focused_pos += p_relative_position;
  176. queue_redraw();
  177. }
  178. }
  179. void ViewportNavigationControl::gui_input(const Ref<InputEvent> &p_event) {
  180. // Mouse events
  181. const Ref<InputEventMouseButton> mouse_button = p_event;
  182. if (mouse_button.is_valid() && mouse_button->get_button_index() == MouseButton::LEFT) {
  183. _process_click(100, mouse_button->get_position(), mouse_button->is_pressed());
  184. }
  185. const Ref<InputEventMouseMotion> mouse_motion = p_event;
  186. if (mouse_motion.is_valid()) {
  187. _process_drag(100, mouse_motion->get_global_position(), viewport->_get_warped_mouse_motion(mouse_motion));
  188. }
  189. // Touch events
  190. const Ref<InputEventScreenTouch> screen_touch = p_event;
  191. if (screen_touch.is_valid()) {
  192. _process_click(screen_touch->get_index(), screen_touch->get_position(), screen_touch->is_pressed());
  193. }
  194. const Ref<InputEventScreenDrag> screen_drag = p_event;
  195. if (screen_drag.is_valid()) {
  196. _process_drag(screen_drag->get_index(), screen_drag->get_position(), screen_drag->get_relative());
  197. }
  198. }
  199. void ViewportNavigationControl::_update_navigation() {
  200. if (focused_index == -1) {
  201. return;
  202. }
  203. Vector2 delta = focused_pos - (get_size() / 2.0);
  204. Vector2 delta_normalized = delta.normalized();
  205. switch (nav_mode) {
  206. case Node3DEditorViewport::NavigationMode::NAVIGATION_MOVE: {
  207. real_t speed_multiplier = MIN(delta.length() / (get_size().x * 100.0), 3.0);
  208. real_t speed = viewport->freelook_speed * speed_multiplier;
  209. const Node3DEditorViewport::FreelookNavigationScheme navigation_scheme = (Node3DEditorViewport::FreelookNavigationScheme)EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_navigation_scheme").operator int();
  210. Vector3 forward;
  211. if (navigation_scheme == Node3DEditorViewport::FreelookNavigationScheme::FREELOOK_FULLY_AXIS_LOCKED) {
  212. // Forward/backward keys will always go straight forward/backward, never moving on the Y axis.
  213. forward = Vector3(0, 0, delta_normalized.y).rotated(Vector3(0, 1, 0), viewport->camera->get_rotation().y);
  214. } else {
  215. // Forward/backward keys will be relative to the camera pitch.
  216. forward = viewport->camera->get_transform().basis.xform(Vector3(0, 0, delta_normalized.y));
  217. }
  218. const Vector3 right = viewport->camera->get_transform().basis.xform(Vector3(delta_normalized.x, 0, 0));
  219. const Vector3 direction = forward + right;
  220. const Vector3 motion = direction * speed;
  221. viewport->cursor.pos += motion;
  222. viewport->cursor.eye_pos += motion;
  223. } break;
  224. case Node3DEditorViewport::NavigationMode::NAVIGATION_LOOK: {
  225. real_t speed_multiplier = MIN(delta.length() / (get_size().x * 2.5), 3.0);
  226. real_t speed = viewport->freelook_speed * speed_multiplier;
  227. viewport->_nav_look(nullptr, delta_normalized * speed);
  228. } break;
  229. case Node3DEditorViewport::NAVIGATION_PAN: {
  230. real_t speed_multiplier = MIN(delta.length() / (get_size().x), 3.0);
  231. real_t speed = viewport->freelook_speed * speed_multiplier;
  232. viewport->_nav_pan(nullptr, -delta_normalized * speed);
  233. } break;
  234. case Node3DEditorViewport::NAVIGATION_ZOOM: {
  235. real_t speed_multiplier = MIN(delta.length() / (get_size().x), 3.0);
  236. real_t speed = viewport->freelook_speed * speed_multiplier;
  237. viewport->_nav_zoom(nullptr, delta_normalized * speed);
  238. } break;
  239. case Node3DEditorViewport::NAVIGATION_ORBIT: {
  240. real_t speed_multiplier = MIN(delta.length() / (get_size().x), 3.0);
  241. real_t speed = viewport->freelook_speed * speed_multiplier;
  242. viewport->_nav_orbit(nullptr, delta_normalized * speed);
  243. } break;
  244. case Node3DEditorViewport::NAVIGATION_NONE: {
  245. } break;
  246. }
  247. }
  248. void ViewportNavigationControl::_on_mouse_entered() {
  249. hovered = true;
  250. queue_redraw();
  251. }
  252. void ViewportNavigationControl::_on_mouse_exited() {
  253. hovered = false;
  254. queue_redraw();
  255. }
  256. void ViewportNavigationControl::set_navigation_mode(Node3DEditorViewport::NavigationMode p_nav_mode) {
  257. nav_mode = p_nav_mode;
  258. }
  259. void ViewportNavigationControl::set_viewport(Node3DEditorViewport *p_viewport) {
  260. viewport = p_viewport;
  261. }
  262. void ViewportRotationControl::_notification(int p_what) {
  263. switch (p_what) {
  264. case NOTIFICATION_ENTER_TREE: {
  265. axis_menu_options.clear();
  266. axis_menu_options.push_back(Node3DEditorViewport::VIEW_RIGHT);
  267. axis_menu_options.push_back(Node3DEditorViewport::VIEW_TOP);
  268. axis_menu_options.push_back(Node3DEditorViewport::VIEW_FRONT);
  269. axis_menu_options.push_back(Node3DEditorViewport::VIEW_LEFT);
  270. axis_menu_options.push_back(Node3DEditorViewport::VIEW_BOTTOM);
  271. axis_menu_options.push_back(Node3DEditorViewport::VIEW_REAR);
  272. axis_colors.clear();
  273. axis_colors.push_back(get_theme_color(SNAME("axis_x_color"), EditorStringName(Editor)));
  274. axis_colors.push_back(get_theme_color(SNAME("axis_y_color"), EditorStringName(Editor)));
  275. axis_colors.push_back(get_theme_color(SNAME("axis_z_color"), EditorStringName(Editor)));
  276. queue_redraw();
  277. if (!is_connected(SceneStringName(mouse_exited), callable_mp(this, &ViewportRotationControl::_on_mouse_exited))) {
  278. connect(SceneStringName(mouse_exited), callable_mp(this, &ViewportRotationControl::_on_mouse_exited));
  279. }
  280. } break;
  281. case NOTIFICATION_DRAW: {
  282. if (viewport != nullptr) {
  283. _draw();
  284. }
  285. } break;
  286. }
  287. }
  288. void ViewportRotationControl::_draw() {
  289. const Vector2 center = get_size() / 2.0;
  290. const real_t radius = get_size().x / 2.0;
  291. if (focused_axis > -2 || orbiting_index != -1) {
  292. draw_circle(center, radius, Color(0.5, 0.5, 0.5, 0.25), true, -1.0, true);
  293. }
  294. Vector<Axis2D> axis_to_draw;
  295. _get_sorted_axis(axis_to_draw);
  296. for (int i = 0; i < axis_to_draw.size(); ++i) {
  297. _draw_axis(axis_to_draw[i]);
  298. }
  299. }
  300. void ViewportRotationControl::_draw_axis(const Axis2D &p_axis) {
  301. const bool focused = focused_axis == p_axis.axis;
  302. const bool positive = p_axis.axis < 3;
  303. const int direction = p_axis.axis % 3;
  304. const Color axis_color = axis_colors[direction];
  305. const double min_alpha = 0.35;
  306. const double alpha = focused ? 1.0 : Math::remap((p_axis.z_axis + 1.0) / 2.0, 0, 0.5, min_alpha, 1.0);
  307. const Color c = focused ? Color(axis_color.lightened(0.75), 1.0) : Color(axis_color, alpha);
  308. if (positive) {
  309. // Draw axis lines for the positive axes.
  310. const Vector2 center = get_size() / 2.0;
  311. const Vector2 diff = p_axis.screen_point - center;
  312. const float line_length = MAX(diff.length() - AXIS_CIRCLE_RADIUS - 0.5 * EDSCALE, 0);
  313. draw_line(center + diff.limit_length(0.5 * EDSCALE), center + diff.limit_length(line_length), c, 1.5 * EDSCALE, true);
  314. draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS, c, true, -1.0, true);
  315. // Draw the axis letter for the positive axes.
  316. const String axis_name = direction == 0 ? "X" : (direction == 1 ? "Y" : "Z");
  317. const Ref<Font> &font = get_theme_font(SNAME("rotation_control"), EditorStringName(EditorFonts));
  318. const int font_size = get_theme_font_size(SNAME("rotation_control_size"), EditorStringName(EditorFonts));
  319. const Size2 char_size = font->get_char_size(axis_name[0], font_size);
  320. const Vector2 char_offset = Vector2(-char_size.width / 2.0, char_size.height * 0.25);
  321. draw_char(font, p_axis.screen_point + char_offset, axis_name, font_size, Color(0.0, 0.0, 0.0, alpha * 0.6));
  322. } else {
  323. // Draw an outline around the negative axes.
  324. draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS, c, true, -1.0, true);
  325. draw_circle(p_axis.screen_point, AXIS_CIRCLE_RADIUS * 0.8, c.darkened(0.4), true, -1.0, true);
  326. }
  327. }
  328. void ViewportRotationControl::_get_sorted_axis(Vector<Axis2D> &r_axis) {
  329. const Vector2 center = get_size() / 2.0;
  330. const real_t radius = get_size().x / 2.0 - AXIS_CIRCLE_RADIUS - 2.0 * EDSCALE;
  331. const Basis camera_basis = viewport->to_camera_transform(viewport->cursor).get_basis().inverse();
  332. for (int i = 0; i < 3; ++i) {
  333. Vector3 axis_3d = camera_basis.get_column(i);
  334. Vector2 axis_vector = Vector2(axis_3d.x, -axis_3d.y) * radius;
  335. if (Math::abs(axis_3d.z) <= 1.0) {
  336. Axis2D pos_axis;
  337. pos_axis.axis = i;
  338. pos_axis.screen_point = center + axis_vector;
  339. pos_axis.z_axis = axis_3d.z;
  340. r_axis.push_back(pos_axis);
  341. Axis2D neg_axis;
  342. neg_axis.axis = i + 3;
  343. neg_axis.screen_point = center - axis_vector;
  344. neg_axis.z_axis = -axis_3d.z;
  345. r_axis.push_back(neg_axis);
  346. } else {
  347. // Special case when the camera is aligned with one axis
  348. Axis2D axis;
  349. axis.axis = i + (axis_3d.z <= 0 ? 0 : 3);
  350. axis.screen_point = center;
  351. axis.z_axis = 1.0;
  352. r_axis.push_back(axis);
  353. }
  354. }
  355. r_axis.sort_custom<Axis2DCompare>();
  356. }
  357. void ViewportRotationControl::_process_click(int p_index, Vector2 p_position, bool p_pressed) {
  358. if (orbiting_index != -1 && orbiting_index != p_index) {
  359. return;
  360. }
  361. if (p_pressed) {
  362. if (p_position.distance_to(get_size() / 2.0) < get_size().x / 2.0) {
  363. orbiting_index = p_index;
  364. }
  365. } else {
  366. if (focused_axis > -1) {
  367. viewport->_menu_option(axis_menu_options[focused_axis]);
  368. _update_focus();
  369. }
  370. orbiting_index = -1;
  371. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
  372. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  373. Input::get_singleton()->warp_mouse(orbiting_mouse_start);
  374. }
  375. }
  376. }
  377. void ViewportRotationControl::_process_drag(Ref<InputEventWithModifiers> p_event, int p_index, Vector2 p_position, Vector2 p_relative_position) {
  378. if (orbiting_index == p_index) {
  379. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_VISIBLE) {
  380. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  381. orbiting_mouse_start = p_position;
  382. }
  383. viewport->_nav_orbit(p_event, p_relative_position);
  384. focused_axis = -1;
  385. } else {
  386. _update_focus();
  387. }
  388. }
  389. void ViewportRotationControl::gui_input(const Ref<InputEvent> &p_event) {
  390. ERR_FAIL_COND(p_event.is_null());
  391. // Mouse events
  392. const Ref<InputEventMouseButton> mb = p_event;
  393. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  394. _process_click(100, mb->get_position(), mb->is_pressed());
  395. }
  396. const Ref<InputEventMouseMotion> mm = p_event;
  397. if (mm.is_valid()) {
  398. _process_drag(mm, 100, mm->get_global_position(), viewport->_get_warped_mouse_motion(mm));
  399. }
  400. // Touch events
  401. const Ref<InputEventScreenTouch> screen_touch = p_event;
  402. if (screen_touch.is_valid()) {
  403. _process_click(screen_touch->get_index(), screen_touch->get_position(), screen_touch->is_pressed());
  404. }
  405. const Ref<InputEventScreenDrag> screen_drag = p_event;
  406. if (screen_drag.is_valid()) {
  407. _process_drag(screen_drag, screen_drag->get_index(), screen_drag->get_position(), screen_drag->get_relative());
  408. }
  409. }
  410. void ViewportRotationControl::_update_focus() {
  411. int original_focus = focused_axis;
  412. focused_axis = -2;
  413. Vector2 mouse_pos = get_local_mouse_position();
  414. if (mouse_pos.distance_to(get_size() / 2.0) < get_size().x / 2.0) {
  415. focused_axis = -1;
  416. }
  417. Vector<Axis2D> axes;
  418. _get_sorted_axis(axes);
  419. for (int i = 0; i < axes.size(); i++) {
  420. const Axis2D &axis = axes[i];
  421. if (mouse_pos.distance_to(axis.screen_point) < AXIS_CIRCLE_RADIUS) {
  422. focused_axis = axis.axis;
  423. }
  424. }
  425. if (focused_axis != original_focus) {
  426. queue_redraw();
  427. }
  428. }
  429. void ViewportRotationControl::_on_mouse_exited() {
  430. focused_axis = -2;
  431. queue_redraw();
  432. }
  433. void ViewportRotationControl::set_viewport(Node3DEditorViewport *p_viewport) {
  434. viewport = p_viewport;
  435. }
  436. void Node3DEditorViewport::_view_settings_confirmed(real_t p_interp_delta) {
  437. // Set FOV override multiplier back to the default, so that the FOV
  438. // setting specified in the View menu is correctly applied.
  439. cursor.fov_scale = 1.0;
  440. _update_camera(p_interp_delta);
  441. }
  442. void Node3DEditorViewport::_update_navigation_controls_visibility() {
  443. bool show_viewport_rotation_gizmo = EDITOR_GET("editors/3d/navigation/show_viewport_rotation_gizmo") && (!previewing_cinema && !previewing_camera);
  444. rotation_control->set_visible(show_viewport_rotation_gizmo);
  445. bool show_viewport_navigation_gizmo = EDITOR_GET("editors/3d/navigation/show_viewport_navigation_gizmo") && (!previewing_cinema && !previewing_camera);
  446. position_control->set_visible(show_viewport_navigation_gizmo);
  447. look_control->set_visible(show_viewport_navigation_gizmo);
  448. }
  449. void Node3DEditorViewport::_update_camera(real_t p_interp_delta) {
  450. bool is_orthogonal = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL;
  451. Cursor old_camera_cursor = camera_cursor;
  452. camera_cursor = cursor;
  453. if (p_interp_delta > 0) {
  454. //-------
  455. // Perform smoothing
  456. if (is_freelook_active()) {
  457. // Higher inertia should increase "lag" (lerp with factor between 0 and 1)
  458. // 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.
  459. const real_t inertia = EDITOR_GET("editors/3d/freelook/freelook_inertia");
  460. real_t factor = (1.0 / inertia) * p_interp_delta;
  461. // We interpolate a different point here, because in freelook mode the focus point (cursor.pos) orbits around eye_pos
  462. camera_cursor.eye_pos = old_camera_cursor.eye_pos.lerp(cursor.eye_pos, CLAMP(factor, 0, 1));
  463. const real_t orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  464. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  465. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  466. if (Math::abs(camera_cursor.x_rot - cursor.x_rot) < 0.1) {
  467. camera_cursor.x_rot = cursor.x_rot;
  468. }
  469. if (Math::abs(camera_cursor.y_rot - cursor.y_rot) < 0.1) {
  470. camera_cursor.y_rot = cursor.y_rot;
  471. }
  472. Vector3 forward = to_camera_transform(camera_cursor).basis.xform(Vector3(0, 0, -1));
  473. camera_cursor.pos = camera_cursor.eye_pos + forward * camera_cursor.distance;
  474. } else {
  475. const real_t orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  476. const real_t translation_inertia = EDITOR_GET("editors/3d/navigation_feel/translation_inertia");
  477. const real_t zoom_inertia = EDITOR_GET("editors/3d/navigation_feel/zoom_inertia");
  478. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  479. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  480. if (Math::abs(camera_cursor.x_rot - cursor.x_rot) < 0.1) {
  481. camera_cursor.x_rot = cursor.x_rot;
  482. }
  483. if (Math::abs(camera_cursor.y_rot - cursor.y_rot) < 0.1) {
  484. camera_cursor.y_rot = cursor.y_rot;
  485. }
  486. camera_cursor.pos = old_camera_cursor.pos.lerp(cursor.pos, MIN(1.f, p_interp_delta * (1 / translation_inertia)));
  487. camera_cursor.distance = Math::lerp(old_camera_cursor.distance, cursor.distance, MIN((real_t)1.0, p_interp_delta * (1 / zoom_inertia)));
  488. }
  489. }
  490. //-------
  491. // Apply camera transform
  492. real_t tolerance = 0.001;
  493. bool equal = true;
  494. if (!Math::is_equal_approx(old_camera_cursor.x_rot, camera_cursor.x_rot, tolerance) || !Math::is_equal_approx(old_camera_cursor.y_rot, camera_cursor.y_rot, tolerance)) {
  495. equal = false;
  496. } else if (!old_camera_cursor.pos.is_equal_approx(camera_cursor.pos)) {
  497. equal = false;
  498. } else if (!Math::is_equal_approx(old_camera_cursor.distance, camera_cursor.distance, tolerance)) {
  499. equal = false;
  500. } else if (!Math::is_equal_approx(old_camera_cursor.fov_scale, camera_cursor.fov_scale, tolerance)) {
  501. equal = false;
  502. }
  503. if (!equal || p_interp_delta == 0 || is_orthogonal != orthogonal) {
  504. camera->set_global_transform(to_camera_transform(camera_cursor));
  505. if (orthogonal) {
  506. float half_fov = Math::deg_to_rad(get_fov()) / 2.0;
  507. float height = 2.0 * cursor.distance * Math::tan(half_fov);
  508. camera->set_orthogonal(height, get_znear(), get_zfar());
  509. } else {
  510. camera->set_perspective(get_fov(), get_znear(), get_zfar());
  511. }
  512. update_transform_gizmo_view();
  513. rotation_control->queue_redraw();
  514. position_control->queue_redraw();
  515. look_control->queue_redraw();
  516. spatial_editor->update_grid();
  517. }
  518. }
  519. Transform3D Node3DEditorViewport::to_camera_transform(const Cursor &p_cursor) const {
  520. Transform3D camera_transform;
  521. camera_transform.translate_local(p_cursor.pos);
  522. camera_transform.basis.rotate(Vector3(1, 0, 0), -p_cursor.x_rot);
  523. camera_transform.basis.rotate(Vector3(0, 1, 0), -p_cursor.y_rot);
  524. if (orthogonal) {
  525. camera_transform.translate_local(0, 0, (get_zfar() - get_znear()) / 2.0);
  526. } else {
  527. camera_transform.translate_local(0, 0, p_cursor.distance);
  528. }
  529. return camera_transform;
  530. }
  531. int Node3DEditorViewport::get_selected_count() const {
  532. const HashMap<Node *, Object *> &selection = editor_selection->get_selection();
  533. int count = 0;
  534. for (const KeyValue<Node *, Object *> &E : selection) {
  535. Node3D *sp = Object::cast_to<Node3D>(E.key);
  536. if (!sp) {
  537. continue;
  538. }
  539. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  540. if (!se) {
  541. continue;
  542. }
  543. count++;
  544. }
  545. return count;
  546. }
  547. void Node3DEditorViewport::cancel_transform() {
  548. List<Node *> &selection = editor_selection->get_selected_node_list();
  549. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  550. Node3D *sp = Object::cast_to<Node3D>(E->get());
  551. if (!sp) {
  552. continue;
  553. }
  554. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  555. if (!se) {
  556. continue;
  557. }
  558. if (se && se->gizmo.is_valid()) {
  559. Vector<int> ids;
  560. Vector<Transform3D> restore;
  561. for (const KeyValue<int, Transform3D> &GE : se->subgizmos) {
  562. ids.push_back(GE.key);
  563. restore.push_back(GE.value);
  564. }
  565. se->gizmo->commit_subgizmos(ids, restore, true);
  566. }
  567. sp->set_global_transform(se->original);
  568. }
  569. collision_reposition = false;
  570. finish_transform();
  571. set_message(TTR("Transform Aborted."), 3);
  572. }
  573. void Node3DEditorViewport::_update_shrink() {
  574. bool shrink = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION));
  575. subviewport_container->set_stretch_shrink(shrink ? 2 : 1);
  576. subviewport_container->set_texture_filter(shrink ? TEXTURE_FILTER_NEAREST : TEXTURE_FILTER_PARENT_NODE);
  577. }
  578. float Node3DEditorViewport::get_znear() const {
  579. return CLAMP(spatial_editor->get_znear(), MIN_Z, MAX_Z);
  580. }
  581. float Node3DEditorViewport::get_zfar() const {
  582. return CLAMP(spatial_editor->get_zfar(), MIN_Z, MAX_Z);
  583. }
  584. float Node3DEditorViewport::get_fov() const {
  585. return CLAMP(spatial_editor->get_fov() * cursor.fov_scale, MIN_FOV, MAX_FOV);
  586. }
  587. Transform3D Node3DEditorViewport::_get_camera_transform() const {
  588. return camera->get_global_transform();
  589. }
  590. Vector3 Node3DEditorViewport::_get_camera_position() const {
  591. return _get_camera_transform().origin;
  592. }
  593. Point2 Node3DEditorViewport::point_to_screen(const Vector3 &p_point) {
  594. return camera->unproject_position(p_point) * subviewport_container->get_stretch_shrink();
  595. }
  596. Vector3 Node3DEditorViewport::get_ray_pos(const Vector2 &p_pos) const {
  597. return camera->project_ray_origin(p_pos / subviewport_container->get_stretch_shrink());
  598. }
  599. Vector3 Node3DEditorViewport::_get_camera_normal() const {
  600. return -_get_camera_transform().basis.get_column(2);
  601. }
  602. Vector3 Node3DEditorViewport::get_ray(const Vector2 &p_pos) const {
  603. return camera->project_ray_normal(p_pos / subviewport_container->get_stretch_shrink());
  604. }
  605. void Node3DEditorViewport::_clear_selected() {
  606. _edit.gizmo = Ref<EditorNode3DGizmo>();
  607. _edit.gizmo_handle = -1;
  608. _edit.gizmo_handle_secondary = false;
  609. _edit.gizmo_initial_value = Variant();
  610. Node3D *selected = spatial_editor->get_single_selected_node();
  611. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  612. if (se && se->gizmo.is_valid()) {
  613. se->subgizmos.clear();
  614. se->gizmo->redraw();
  615. se->gizmo.unref();
  616. spatial_editor->update_transform_gizmo();
  617. } else {
  618. editor_selection->clear();
  619. Node3DEditor::get_singleton()->edit(nullptr);
  620. }
  621. }
  622. void Node3DEditorViewport::_select_clicked(bool p_allow_locked) {
  623. Node *node = Object::cast_to<Node3D>(ObjectDB::get_instance(clicked));
  624. Node3D *selected = Object::cast_to<Node3D>(node);
  625. clicked = ObjectID();
  626. if (!selected) {
  627. return;
  628. }
  629. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  630. // Prevent selection of nodes not owned by the edited scene.
  631. while (node && node != edited_scene->get_parent()) {
  632. Node *node_owner = node->get_owner();
  633. if (node_owner == edited_scene || node == edited_scene || (node_owner != nullptr && edited_scene->is_editable_instance(node_owner))) {
  634. break;
  635. }
  636. node = node->get_parent();
  637. selected = Object::cast_to<Node3D>(node);
  638. }
  639. if (!p_allow_locked) {
  640. // Replace the node by the group if grouped
  641. while (node && node != edited_scene->get_parent()) {
  642. Node3D *selected_tmp = Object::cast_to<Node3D>(node);
  643. if (selected_tmp && node->has_meta("_edit_group_")) {
  644. selected = selected_tmp;
  645. }
  646. node = node->get_parent();
  647. }
  648. }
  649. if (p_allow_locked || (selected != nullptr && !_is_node_locked(selected))) {
  650. if (clicked_wants_append) {
  651. if (editor_selection->is_selected(selected)) {
  652. editor_selection->remove_node(selected);
  653. } else {
  654. editor_selection->add_node(selected);
  655. }
  656. } else {
  657. if (!editor_selection->is_selected(selected)) {
  658. editor_selection->clear();
  659. editor_selection->add_node(selected);
  660. EditorNode::get_singleton()->edit_node(selected);
  661. }
  662. }
  663. if (editor_selection->get_selected_node_list().size() == 1) {
  664. EditorNode::get_singleton()->edit_node(editor_selection->get_selected_node_list().front()->get());
  665. }
  666. }
  667. }
  668. ObjectID Node3DEditorViewport::_select_ray(const Point2 &p_pos) const {
  669. Vector3 ray = get_ray(p_pos);
  670. Vector3 pos = get_ray_pos(p_pos);
  671. Vector2 shrinked_pos = p_pos / subviewport_container->get_stretch_shrink();
  672. if (viewport->get_debug_draw() == Viewport::DEBUG_DRAW_SDFGI_PROBES) {
  673. RS::get_singleton()->sdfgi_set_debug_probe_select(pos, ray);
  674. }
  675. HashSet<Ref<EditorNode3DGizmo>> found_gizmos;
  676. Node *edited_scene = get_tree()->get_edited_scene_root();
  677. ObjectID closest;
  678. Node *item = nullptr;
  679. float closest_dist = 1e20;
  680. Vector<Node3D *> nodes_with_gizmos = Node3DEditor::get_singleton()->gizmo_bvh_ray_query(pos, pos + ray * camera->get_far());
  681. for (Node3D *spat : nodes_with_gizmos) {
  682. if (!spat || _is_node_locked(spat)) {
  683. continue;
  684. }
  685. Vector<Ref<Node3DGizmo>> gizmos = spat->get_gizmos();
  686. for (int j = 0; j < gizmos.size(); j++) {
  687. Ref<EditorNode3DGizmo> seg = gizmos[j];
  688. if ((!seg.is_valid()) || found_gizmos.has(seg)) {
  689. continue;
  690. }
  691. found_gizmos.insert(seg);
  692. Vector3 point;
  693. Vector3 normal;
  694. bool inters = seg->intersect_ray(camera, shrinked_pos, point, normal);
  695. if (!inters) {
  696. continue;
  697. }
  698. const real_t dist = pos.distance_to(point);
  699. if (dist < 0) {
  700. continue;
  701. }
  702. if (dist < closest_dist) {
  703. item = Object::cast_to<Node>(spat);
  704. if (item != edited_scene) {
  705. item = edited_scene->get_deepest_editable_node(item);
  706. }
  707. closest = item->get_instance_id();
  708. closest_dist = dist;
  709. }
  710. }
  711. }
  712. if (!item) {
  713. return ObjectID();
  714. }
  715. return closest;
  716. }
  717. void Node3DEditorViewport::_find_items_at_pos(const Point2 &p_pos, Vector<_RayResult> &r_results, bool p_include_locked_nodes) {
  718. Vector3 ray = get_ray(p_pos);
  719. Vector3 pos = get_ray_pos(p_pos);
  720. Vector<Node3D *> nodes_with_gizmos = Node3DEditor::get_singleton()->gizmo_bvh_ray_query(pos, pos + ray * camera->get_far());
  721. HashSet<Node3D *> found_nodes;
  722. for (Node3D *spat : nodes_with_gizmos) {
  723. if (!spat) {
  724. continue;
  725. }
  726. if (found_nodes.has(spat)) {
  727. continue;
  728. }
  729. if (!p_include_locked_nodes && _is_node_locked(spat)) {
  730. continue;
  731. }
  732. Vector<Ref<Node3DGizmo>> gizmos = spat->get_gizmos();
  733. for (int j = 0; j < gizmos.size(); j++) {
  734. Ref<EditorNode3DGizmo> seg = gizmos[j];
  735. if (!seg.is_valid()) {
  736. continue;
  737. }
  738. Vector3 point;
  739. Vector3 normal;
  740. bool inters = seg->intersect_ray(camera, p_pos, point, normal);
  741. if (!inters) {
  742. continue;
  743. }
  744. const real_t dist = pos.distance_to(point);
  745. if (dist < 0) {
  746. continue;
  747. }
  748. found_nodes.insert(spat);
  749. _RayResult res;
  750. res.item = spat;
  751. res.depth = dist;
  752. r_results.push_back(res);
  753. break;
  754. }
  755. }
  756. r_results.sort();
  757. }
  758. Vector3 Node3DEditorViewport::_get_screen_to_space(const Vector3 &p_vector3) {
  759. Projection cm;
  760. if (orthogonal) {
  761. cm.set_orthogonal(camera->get_size(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  762. } else {
  763. cm.set_perspective(get_fov(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  764. }
  765. Vector2 screen_he = cm.get_viewport_half_extents();
  766. Transform3D camera_transform;
  767. camera_transform.translate_local(cursor.pos);
  768. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  769. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  770. camera_transform.translate_local(0, 0, cursor.distance);
  771. return camera_transform.xform(Vector3(((p_vector3.x / get_size().width) * 2.0 - 1.0) * screen_he.x, ((1.0 - (p_vector3.y / get_size().height)) * 2.0 - 1.0) * screen_he.y, -(get_znear() + p_vector3.z)));
  772. }
  773. void Node3DEditorViewport::_select_region() {
  774. if (cursor.region_begin == cursor.region_end) {
  775. if (!clicked_wants_append) {
  776. _clear_selected();
  777. }
  778. return; //nothing really
  779. }
  780. const real_t z_offset = MAX(0.0, 5.0 - get_znear());
  781. Vector3 box[4] = {
  782. Vector3(
  783. MIN(cursor.region_begin.x, cursor.region_end.x),
  784. MIN(cursor.region_begin.y, cursor.region_end.y),
  785. z_offset),
  786. Vector3(
  787. MAX(cursor.region_begin.x, cursor.region_end.x),
  788. MIN(cursor.region_begin.y, cursor.region_end.y),
  789. z_offset),
  790. Vector3(
  791. MAX(cursor.region_begin.x, cursor.region_end.x),
  792. MAX(cursor.region_begin.y, cursor.region_end.y),
  793. z_offset),
  794. Vector3(
  795. MIN(cursor.region_begin.x, cursor.region_end.x),
  796. MAX(cursor.region_begin.y, cursor.region_end.y),
  797. z_offset)
  798. };
  799. Vector<Plane> frustum;
  800. Vector3 cam_pos = _get_camera_position();
  801. for (int i = 0; i < 4; i++) {
  802. Vector3 a = _get_screen_to_space(box[i]);
  803. Vector3 b = _get_screen_to_space(box[(i + 1) % 4]);
  804. if (orthogonal) {
  805. frustum.push_back(Plane((a - b).normalized(), a));
  806. } else {
  807. frustum.push_back(Plane(a, b, cam_pos));
  808. }
  809. }
  810. Plane near_plane = Plane(-_get_camera_normal(), cam_pos);
  811. near_plane.d -= get_znear();
  812. frustum.push_back(near_plane);
  813. Plane far_plane = -near_plane;
  814. far_plane.d += get_zfar();
  815. frustum.push_back(far_plane);
  816. if (spatial_editor->get_single_selected_node()) {
  817. Node3D *single_selected = spatial_editor->get_single_selected_node();
  818. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(single_selected);
  819. if (se) {
  820. Ref<EditorNode3DGizmo> old_gizmo;
  821. if (!clicked_wants_append) {
  822. se->subgizmos.clear();
  823. old_gizmo = se->gizmo;
  824. se->gizmo.unref();
  825. }
  826. bool found_subgizmos = false;
  827. Vector<Ref<Node3DGizmo>> gizmos = single_selected->get_gizmos();
  828. for (int j = 0; j < gizmos.size(); j++) {
  829. Ref<EditorNode3DGizmo> seg = gizmos[j];
  830. if (!seg.is_valid()) {
  831. continue;
  832. }
  833. if (se->gizmo.is_valid() && se->gizmo != seg) {
  834. continue;
  835. }
  836. Vector<int> subgizmos = seg->subgizmos_intersect_frustum(camera, frustum);
  837. if (!subgizmos.is_empty()) {
  838. se->gizmo = seg;
  839. for (int i = 0; i < subgizmos.size(); i++) {
  840. int subgizmo_id = subgizmos[i];
  841. if (!se->subgizmos.has(subgizmo_id)) {
  842. se->subgizmos.insert(subgizmo_id, se->gizmo->get_subgizmo_transform(subgizmo_id));
  843. }
  844. }
  845. found_subgizmos = true;
  846. break;
  847. }
  848. }
  849. if (!clicked_wants_append || found_subgizmos) {
  850. if (se->gizmo.is_valid()) {
  851. se->gizmo->redraw();
  852. }
  853. if (old_gizmo != se->gizmo && old_gizmo.is_valid()) {
  854. old_gizmo->redraw();
  855. }
  856. spatial_editor->update_transform_gizmo();
  857. }
  858. if (found_subgizmos) {
  859. return;
  860. }
  861. }
  862. }
  863. if (!clicked_wants_append) {
  864. _clear_selected();
  865. }
  866. Vector<Node3D *> nodes_with_gizmos = Node3DEditor::get_singleton()->gizmo_bvh_frustum_query(frustum);
  867. HashSet<Node3D *> found_nodes;
  868. Vector<Node *> selected;
  869. Node *edited_scene = get_tree()->get_edited_scene_root();
  870. if (edited_scene == nullptr) {
  871. return;
  872. }
  873. for (Node3D *sp : nodes_with_gizmos) {
  874. if (!sp || _is_node_locked(sp)) {
  875. continue;
  876. }
  877. if (found_nodes.has(sp)) {
  878. continue;
  879. }
  880. found_nodes.insert(sp);
  881. Node *node = Object::cast_to<Node>(sp);
  882. // Selection requires that the node is the edited scene or its descendant, and has an owner.
  883. if (node != edited_scene) {
  884. if (!node->get_owner() || !edited_scene->is_ancestor_of(node)) {
  885. continue;
  886. }
  887. node = edited_scene->get_deepest_editable_node(node);
  888. while (node != edited_scene) {
  889. Node *node_owner = node->get_owner();
  890. if (node_owner == edited_scene || (node_owner != nullptr && edited_scene->is_editable_instance(node_owner))) {
  891. break;
  892. }
  893. node = node->get_parent();
  894. }
  895. }
  896. // Replace the node by the group if grouped
  897. if (node->is_class("Node3D")) {
  898. Node3D *sel = Object::cast_to<Node3D>(node);
  899. while (node && node != EditorNode::get_singleton()->get_edited_scene()->get_parent()) {
  900. Node3D *selected_tmp = Object::cast_to<Node3D>(node);
  901. if (selected_tmp && node->has_meta("_edit_group_")) {
  902. sel = selected_tmp;
  903. }
  904. node = node->get_parent();
  905. }
  906. node = sel;
  907. }
  908. if (_is_node_locked(node)) {
  909. continue;
  910. }
  911. Vector<Ref<Node3DGizmo>> gizmos = sp->get_gizmos();
  912. for (int j = 0; j < gizmos.size(); j++) {
  913. Ref<EditorNode3DGizmo> seg = gizmos[j];
  914. if (!seg.is_valid()) {
  915. continue;
  916. }
  917. if (seg->intersect_frustum(camera, frustum)) {
  918. selected.push_back(node);
  919. }
  920. }
  921. }
  922. for (int i = 0; i < selected.size(); i++) {
  923. if (!editor_selection->is_selected(selected[i])) {
  924. editor_selection->add_node(selected[i]);
  925. }
  926. }
  927. if (editor_selection->get_selected_node_list().size() == 1) {
  928. EditorNode::get_singleton()->edit_node(editor_selection->get_selected_node_list().front()->get());
  929. }
  930. }
  931. void Node3DEditorViewport::_update_name() {
  932. String name;
  933. switch (view_type) {
  934. case VIEW_TYPE_USER: {
  935. if (orthogonal) {
  936. name = TTR("Orthogonal");
  937. } else {
  938. name = TTR("Perspective");
  939. }
  940. } break;
  941. case VIEW_TYPE_TOP: {
  942. if (orthogonal) {
  943. name = TTR("Top Orthogonal");
  944. } else {
  945. name = TTR("Top Perspective");
  946. }
  947. } break;
  948. case VIEW_TYPE_BOTTOM: {
  949. if (orthogonal) {
  950. name = TTR("Bottom Orthogonal");
  951. } else {
  952. name = TTR("Bottom Perspective");
  953. }
  954. } break;
  955. case VIEW_TYPE_LEFT: {
  956. if (orthogonal) {
  957. name = TTR("Left Orthogonal");
  958. } else {
  959. name = TTR("Left Perspective");
  960. }
  961. } break;
  962. case VIEW_TYPE_RIGHT: {
  963. if (orthogonal) {
  964. name = TTR("Right Orthogonal");
  965. } else {
  966. name = TTR("Right Perspective");
  967. }
  968. } break;
  969. case VIEW_TYPE_FRONT: {
  970. if (orthogonal) {
  971. name = TTR("Front Orthogonal");
  972. } else {
  973. name = TTR("Front Perspective");
  974. }
  975. } break;
  976. case VIEW_TYPE_REAR: {
  977. if (orthogonal) {
  978. name = TTR("Rear Orthogonal");
  979. } else {
  980. name = TTR("Rear Perspective");
  981. }
  982. } break;
  983. }
  984. if (auto_orthogonal) {
  985. // TRANSLATORS: This will be appended to the view name when Auto Orthogonal is enabled.
  986. name += " " + TTR("[auto]");
  987. }
  988. view_menu->set_text(name);
  989. view_menu->reset_size();
  990. }
  991. void Node3DEditorViewport::_compute_edit(const Point2 &p_point) {
  992. _edit.original_local = spatial_editor->are_local_coords_enabled();
  993. _edit.click_ray = get_ray(p_point);
  994. _edit.click_ray_pos = get_ray_pos(p_point);
  995. _edit.plane = TRANSFORM_VIEW;
  996. spatial_editor->update_transform_gizmo();
  997. _edit.center = spatial_editor->get_gizmo_transform().origin;
  998. Node3D *selected = spatial_editor->get_single_selected_node();
  999. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  1000. if (se && se->gizmo.is_valid()) {
  1001. for (const KeyValue<int, Transform3D> &E : se->subgizmos) {
  1002. int subgizmo_id = E.key;
  1003. se->subgizmos[subgizmo_id] = se->gizmo->get_subgizmo_transform(subgizmo_id);
  1004. }
  1005. se->original_local = selected->get_transform();
  1006. se->original = selected->get_global_transform();
  1007. } else {
  1008. List<Node *> &selection = editor_selection->get_selected_node_list();
  1009. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1010. Node3D *sp = Object::cast_to<Node3D>(E->get());
  1011. if (!sp) {
  1012. continue;
  1013. }
  1014. Node3DEditorSelectedItem *sel_item = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  1015. if (!sel_item) {
  1016. continue;
  1017. }
  1018. sel_item->original_local = sel_item->sp->get_local_gizmo_transform();
  1019. sel_item->original = sel_item->sp->get_global_gizmo_transform();
  1020. }
  1021. }
  1022. }
  1023. static Key _get_key_modifier_setting(const String &p_property) {
  1024. switch (EDITOR_GET(p_property).operator int()) {
  1025. case 0:
  1026. return Key::NONE;
  1027. case 1:
  1028. return Key::SHIFT;
  1029. case 2:
  1030. return Key::ALT;
  1031. case 3:
  1032. return Key::META;
  1033. case 4:
  1034. return Key::CTRL;
  1035. }
  1036. return Key::NONE;
  1037. }
  1038. static Key _get_key_modifier(Ref<InputEventWithModifiers> e) {
  1039. if (e->is_shift_pressed()) {
  1040. return Key::SHIFT;
  1041. }
  1042. if (e->is_alt_pressed()) {
  1043. return Key::ALT;
  1044. }
  1045. if (e->is_ctrl_pressed()) {
  1046. return Key::CTRL;
  1047. }
  1048. if (e->is_meta_pressed()) {
  1049. return Key::META;
  1050. }
  1051. return Key::NONE;
  1052. }
  1053. bool Node3DEditorViewport::_transform_gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only) {
  1054. if (!spatial_editor->is_gizmo_visible()) {
  1055. return false;
  1056. }
  1057. if (get_selected_count() == 0) {
  1058. if (p_highlight_only) {
  1059. spatial_editor->select_gizmo_highlight_axis(-1);
  1060. }
  1061. return false;
  1062. }
  1063. Vector3 ray_pos = get_ray_pos(p_screenpos);
  1064. Vector3 ray = get_ray(p_screenpos);
  1065. Transform3D gt = spatial_editor->get_gizmo_transform();
  1066. if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE) {
  1067. int col_axis = -1;
  1068. real_t col_d = 1e20;
  1069. for (int i = 0; i < 3; i++) {
  1070. const Vector3 grabber_pos = gt.origin + gt.basis.get_column(i).normalized() * gizmo_scale * (GIZMO_ARROW_OFFSET + (GIZMO_ARROW_SIZE * 0.5));
  1071. const real_t grabber_radius = gizmo_scale * GIZMO_ARROW_SIZE;
  1072. Vector3 r;
  1073. if (Geometry3D::segment_intersects_sphere(ray_pos, ray_pos + ray * MAX_Z, grabber_pos, grabber_radius, &r)) {
  1074. const real_t d = r.distance_to(ray_pos);
  1075. if (d < col_d) {
  1076. col_d = d;
  1077. col_axis = i;
  1078. }
  1079. }
  1080. }
  1081. bool is_plane_translate = false;
  1082. // plane select
  1083. if (col_axis == -1) {
  1084. col_d = 1e20;
  1085. for (int i = 0; i < 3; i++) {
  1086. Vector3 ivec2 = gt.basis.get_column((i + 1) % 3).normalized();
  1087. Vector3 ivec3 = gt.basis.get_column((i + 2) % 3).normalized();
  1088. // Allow some tolerance to make the plane easier to click,
  1089. // even if the click is actually slightly outside the plane.
  1090. const Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gizmo_scale * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST * 0.6667);
  1091. Vector3 r;
  1092. Plane plane(gt.basis.get_column(i).normalized(), gt.origin);
  1093. if (plane.intersects_ray(ray_pos, ray, &r)) {
  1094. const real_t dist = r.distance_to(grabber_pos);
  1095. // Allow some tolerance to make the plane easier to click,
  1096. // even if the click is actually slightly outside the plane.
  1097. if (dist < (gizmo_scale * GIZMO_PLANE_SIZE * 1.5)) {
  1098. const real_t d = ray_pos.distance_to(r);
  1099. if (d < col_d) {
  1100. col_d = d;
  1101. col_axis = i;
  1102. is_plane_translate = true;
  1103. }
  1104. }
  1105. }
  1106. }
  1107. }
  1108. if (col_axis != -1) {
  1109. if (p_highlight_only) {
  1110. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_translate ? 6 : 0));
  1111. } else {
  1112. //handle plane translate
  1113. _edit.mode = TRANSFORM_TRANSLATE;
  1114. _compute_edit(p_screenpos);
  1115. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_translate ? 3 : 0));
  1116. }
  1117. return true;
  1118. }
  1119. }
  1120. if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE) {
  1121. int col_axis = -1;
  1122. Vector3 hit_position;
  1123. Vector3 hit_normal;
  1124. real_t ray_length = gt.origin.distance_to(ray_pos) + (GIZMO_CIRCLE_SIZE * gizmo_scale) * 4.0f;
  1125. if (Geometry3D::segment_intersects_sphere(ray_pos, ray_pos + ray * ray_length, gt.origin, gizmo_scale * (GIZMO_CIRCLE_SIZE), &hit_position, &hit_normal)) {
  1126. if (hit_normal.dot(_get_camera_normal()) < 0.05) {
  1127. hit_position = gt.xform_inv(hit_position).abs();
  1128. int min_axis = hit_position.min_axis_index();
  1129. if (hit_position[min_axis] < gizmo_scale * GIZMO_RING_HALF_WIDTH) {
  1130. col_axis = min_axis;
  1131. }
  1132. }
  1133. }
  1134. if (col_axis == -1) {
  1135. float col_d = 1e20;
  1136. for (int i = 0; i < 3; i++) {
  1137. Plane plane(gt.basis.get_column(i).normalized(), gt.origin);
  1138. Vector3 r;
  1139. if (!plane.intersects_ray(ray_pos, ray, &r)) {
  1140. continue;
  1141. }
  1142. const real_t dist = r.distance_to(gt.origin);
  1143. const Vector3 r_dir = (r - gt.origin).normalized();
  1144. if (_get_camera_normal().dot(r_dir) <= 0.005) {
  1145. if (dist > gizmo_scale * (GIZMO_CIRCLE_SIZE - GIZMO_RING_HALF_WIDTH) && dist < gizmo_scale * (GIZMO_CIRCLE_SIZE + GIZMO_RING_HALF_WIDTH)) {
  1146. const real_t d = ray_pos.distance_to(r);
  1147. if (d < col_d) {
  1148. col_d = d;
  1149. col_axis = i;
  1150. }
  1151. }
  1152. }
  1153. }
  1154. }
  1155. if (col_axis != -1) {
  1156. if (p_highlight_only) {
  1157. spatial_editor->select_gizmo_highlight_axis(col_axis + 3);
  1158. } else {
  1159. //handle rotate
  1160. _edit.mode = TRANSFORM_ROTATE;
  1161. _compute_edit(p_screenpos);
  1162. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis);
  1163. }
  1164. return true;
  1165. }
  1166. }
  1167. if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE) {
  1168. int col_axis = -1;
  1169. float col_d = 1e20;
  1170. for (int i = 0; i < 3; i++) {
  1171. const Vector3 grabber_pos = gt.origin + gt.basis.get_column(i).normalized() * gizmo_scale * GIZMO_SCALE_OFFSET;
  1172. const real_t grabber_radius = gizmo_scale * GIZMO_ARROW_SIZE;
  1173. Vector3 r;
  1174. if (Geometry3D::segment_intersects_sphere(ray_pos, ray_pos + ray * MAX_Z, grabber_pos, grabber_radius, &r)) {
  1175. const real_t d = r.distance_to(ray_pos);
  1176. if (d < col_d) {
  1177. col_d = d;
  1178. col_axis = i;
  1179. }
  1180. }
  1181. }
  1182. bool is_plane_scale = false;
  1183. // plane select
  1184. if (col_axis == -1) {
  1185. col_d = 1e20;
  1186. for (int i = 0; i < 3; i++) {
  1187. const Vector3 ivec2 = gt.basis.get_column((i + 1) % 3).normalized();
  1188. const Vector3 ivec3 = gt.basis.get_column((i + 2) % 3).normalized();
  1189. // Allow some tolerance to make the plane easier to click,
  1190. // even if the click is actually slightly outside the plane.
  1191. const Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gizmo_scale * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST * 0.6667);
  1192. Vector3 r;
  1193. Plane plane(gt.basis.get_column(i).normalized(), gt.origin);
  1194. if (plane.intersects_ray(ray_pos, ray, &r)) {
  1195. const real_t dist = r.distance_to(grabber_pos);
  1196. // Allow some tolerance to make the plane easier to click,
  1197. // even if the click is actually slightly outside the plane.
  1198. if (dist < (gizmo_scale * GIZMO_PLANE_SIZE * 1.5)) {
  1199. const real_t d = ray_pos.distance_to(r);
  1200. if (d < col_d) {
  1201. col_d = d;
  1202. col_axis = i;
  1203. is_plane_scale = true;
  1204. }
  1205. }
  1206. }
  1207. }
  1208. }
  1209. if (col_axis != -1) {
  1210. if (p_highlight_only) {
  1211. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_scale ? 12 : 9));
  1212. } else {
  1213. //handle scale
  1214. _edit.mode = TRANSFORM_SCALE;
  1215. _compute_edit(p_screenpos);
  1216. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_scale ? 3 : 0));
  1217. }
  1218. return true;
  1219. }
  1220. }
  1221. if (p_highlight_only) {
  1222. spatial_editor->select_gizmo_highlight_axis(-1);
  1223. }
  1224. return false;
  1225. }
  1226. void Node3DEditorViewport::_transform_gizmo_apply(Node3D *p_node, const Transform3D &p_transform, bool p_local) {
  1227. if (p_transform.basis.determinant() == 0) {
  1228. return;
  1229. }
  1230. if (p_local) {
  1231. p_node->set_transform(p_transform);
  1232. } else {
  1233. p_node->set_global_transform(p_transform);
  1234. }
  1235. }
  1236. Transform3D Node3DEditorViewport::_compute_transform(TransformMode p_mode, const Transform3D &p_original, const Transform3D &p_original_local, Vector3 p_motion, double p_extra, bool p_local, bool p_orthogonal) {
  1237. switch (p_mode) {
  1238. case TRANSFORM_SCALE: {
  1239. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1240. p_motion.snapf(p_extra);
  1241. }
  1242. Transform3D s;
  1243. if (p_local) {
  1244. s.basis = p_original_local.basis.scaled_local(p_motion + Vector3(1, 1, 1));
  1245. s.origin = p_original_local.origin;
  1246. } else {
  1247. s.basis.scale(p_motion + Vector3(1, 1, 1));
  1248. Transform3D base = Transform3D(Basis(), _edit.center);
  1249. s = base * (s * (base.inverse() * p_original));
  1250. // Recalculate orthogonalized scale without moving origin.
  1251. if (p_orthogonal) {
  1252. s.basis = p_original.basis.scaled_orthogonal(p_motion + Vector3(1, 1, 1));
  1253. }
  1254. }
  1255. return s;
  1256. }
  1257. case TRANSFORM_TRANSLATE: {
  1258. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1259. p_motion.snapf(p_extra);
  1260. }
  1261. if (p_local) {
  1262. return p_original_local.translated_local(p_motion);
  1263. }
  1264. return p_original.translated(p_motion);
  1265. }
  1266. case TRANSFORM_ROTATE: {
  1267. Transform3D r;
  1268. if (p_local) {
  1269. Vector3 axis = p_original_local.basis.xform(p_motion);
  1270. r.basis = Basis(axis.normalized(), p_extra) * p_original_local.basis;
  1271. r.origin = p_original_local.origin;
  1272. } else {
  1273. Basis local = p_original.basis * p_original_local.basis.inverse();
  1274. Vector3 axis = local.xform_inv(p_motion);
  1275. r.basis = local * Basis(axis.normalized(), p_extra) * p_original_local.basis;
  1276. r.origin = Basis(p_motion, p_extra).xform(p_original.origin - _edit.center) + _edit.center;
  1277. }
  1278. return r;
  1279. }
  1280. default: {
  1281. ERR_FAIL_V_MSG(Transform3D(), "Invalid mode in '_compute_transform'");
  1282. }
  1283. }
  1284. }
  1285. void Node3DEditorViewport::_surface_mouse_enter() {
  1286. if (Input::get_singleton()->get_mouse_mode() == Input::MOUSE_MODE_CAPTURED) {
  1287. return;
  1288. }
  1289. if (!surface->has_focus() && (!get_viewport()->gui_get_focus_owner() || !get_viewport()->gui_get_focus_owner()->is_text_field())) {
  1290. surface->grab_focus();
  1291. }
  1292. }
  1293. void Node3DEditorViewport::_surface_mouse_exit() {
  1294. _remove_preview_node();
  1295. _reset_preview_material();
  1296. _remove_preview_material();
  1297. }
  1298. void Node3DEditorViewport::_surface_focus_enter() {
  1299. view_menu->set_disable_shortcuts(false);
  1300. }
  1301. void Node3DEditorViewport::_surface_focus_exit() {
  1302. view_menu->set_disable_shortcuts(true);
  1303. }
  1304. bool Node3DEditorViewport::_is_node_locked(const Node *p_node) const {
  1305. return p_node->get_meta("_edit_lock_", false);
  1306. }
  1307. void Node3DEditorViewport::_list_select(Ref<InputEventMouseButton> b) {
  1308. Vector<_RayResult> potential_selection_results;
  1309. _find_items_at_pos(b->get_position(), potential_selection_results, spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT);
  1310. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  1311. // Filter to a list of nodes which include either the edited scene or nodes directly owned by the edited scene.
  1312. // If a node has an invalid owner, recursively check their parents until a valid node is found.
  1313. for (int i = 0; i < potential_selection_results.size(); i++) {
  1314. Node3D *node = potential_selection_results[i].item;
  1315. while (true) {
  1316. if (node == nullptr || node == edited_scene->get_parent()) {
  1317. break;
  1318. } else {
  1319. Node *node_owner = node->get_owner();
  1320. if (node == edited_scene || node_owner == edited_scene || (node_owner != nullptr && edited_scene->is_editable_instance(node_owner))) {
  1321. if (!selection_results.has(node)) {
  1322. selection_results.append(node);
  1323. }
  1324. break;
  1325. }
  1326. }
  1327. node = Object::cast_to<Node3D>(node->get_parent());
  1328. }
  1329. }
  1330. clicked_wants_append = b->is_shift_pressed();
  1331. if (selection_results.size() == 1) {
  1332. clicked = selection_results[0]->get_instance_id();
  1333. selection_results.clear();
  1334. if (clicked.is_valid()) {
  1335. _select_clicked(spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT);
  1336. }
  1337. } else if (!selection_results.is_empty()) {
  1338. NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
  1339. StringName root_name = root_path.get_name(root_path.get_name_count() - 1);
  1340. for (int i = 0; i < selection_results.size(); i++) {
  1341. Node3D *spat = selection_results[i];
  1342. Ref<Texture2D> icon = EditorNode::get_singleton()->get_object_icon(spat, "Node");
  1343. String node_path = "/" + root_name + "/" + root_path.rel_path_to(spat->get_path());
  1344. int locked = 0;
  1345. if (_is_node_locked(spat)) {
  1346. locked = 1;
  1347. } else {
  1348. Node *ed_scene = EditorNode::get_singleton()->get_edited_scene();
  1349. Node *node = spat;
  1350. while (node && node != ed_scene->get_parent()) {
  1351. Node3D *selected_tmp = Object::cast_to<Node3D>(node);
  1352. if (selected_tmp && node->has_meta("_edit_group_")) {
  1353. locked = 2;
  1354. }
  1355. node = node->get_parent();
  1356. }
  1357. }
  1358. String suffix;
  1359. if (locked == 1) {
  1360. suffix = " (" + TTR("Locked") + ")";
  1361. } else if (locked == 2) {
  1362. suffix = " (" + TTR("Grouped") + ")";
  1363. }
  1364. selection_menu->add_item((String)spat->get_name() + suffix);
  1365. selection_menu->set_item_icon(i, icon);
  1366. selection_menu->set_item_metadata(i, node_path);
  1367. selection_menu->set_item_tooltip(i, String(spat->get_name()) + "\nType: " + spat->get_class() + "\nPath: " + node_path);
  1368. }
  1369. selection_results_menu = selection_results;
  1370. selection_menu->set_position(get_screen_position() + b->get_position());
  1371. selection_menu->reset_size();
  1372. selection_menu->popup();
  1373. }
  1374. }
  1375. // This is only active during instant transforms,
  1376. // to capture and wrap mouse events outside the control.
  1377. void Node3DEditorViewport::input(const Ref<InputEvent> &p_event) {
  1378. ERR_FAIL_COND(!_edit.instant);
  1379. Ref<InputEventMouseMotion> m = p_event;
  1380. if (m.is_valid()) {
  1381. _edit.mouse_pos += _get_warped_mouse_motion(p_event);
  1382. update_transform(_get_key_modifier(m) == Key::SHIFT);
  1383. }
  1384. }
  1385. void Node3DEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
  1386. if (previewing || get_viewport()->gui_get_drag_data()) {
  1387. return; //do NONE
  1388. }
  1389. EditorPlugin::AfterGUIInput after = EditorPlugin::AFTER_GUI_INPUT_PASS;
  1390. {
  1391. EditorNode *en = EditorNode::get_singleton();
  1392. EditorPluginList *force_input_forwarding_list = en->get_editor_plugins_force_input_forwarding();
  1393. if (!force_input_forwarding_list->is_empty()) {
  1394. EditorPlugin::AfterGUIInput discard = force_input_forwarding_list->forward_3d_gui_input(camera, p_event, true);
  1395. if (discard == EditorPlugin::AFTER_GUI_INPUT_STOP) {
  1396. return;
  1397. }
  1398. if (discard == EditorPlugin::AFTER_GUI_INPUT_CUSTOM) {
  1399. after = EditorPlugin::AFTER_GUI_INPUT_CUSTOM;
  1400. }
  1401. }
  1402. }
  1403. {
  1404. EditorNode *en = EditorNode::get_singleton();
  1405. EditorPluginList *over_plugin_list = en->get_editor_plugins_over();
  1406. if (!over_plugin_list->is_empty()) {
  1407. EditorPlugin::AfterGUIInput discard = over_plugin_list->forward_3d_gui_input(camera, p_event, false);
  1408. if (discard == EditorPlugin::AFTER_GUI_INPUT_STOP) {
  1409. return;
  1410. }
  1411. if (discard == EditorPlugin::AFTER_GUI_INPUT_CUSTOM) {
  1412. after = EditorPlugin::AFTER_GUI_INPUT_CUSTOM;
  1413. }
  1414. }
  1415. }
  1416. Ref<InputEventMouseButton> b = p_event;
  1417. if (b.is_valid()) {
  1418. emit_signal(SNAME("clicked"));
  1419. ViewportNavMouseButton orbit_mouse_preference = (ViewportNavMouseButton)EDITOR_GET("editors/3d/navigation/orbit_mouse_button").operator int();
  1420. ViewportNavMouseButton pan_mouse_preference = (ViewportNavMouseButton)EDITOR_GET("editors/3d/navigation/pan_mouse_button").operator int();
  1421. ViewportNavMouseButton zoom_mouse_preference = (ViewportNavMouseButton)EDITOR_GET("editors/3d/navigation/zoom_mouse_button").operator int();
  1422. const real_t zoom_factor = 1 + (ZOOM_FREELOOK_MULTIPLIER - 1) * b->get_factor();
  1423. switch (b->get_button_index()) {
  1424. case MouseButton::WHEEL_UP: {
  1425. if (is_freelook_active()) {
  1426. scale_freelook_speed(zoom_factor);
  1427. } else {
  1428. scale_cursor_distance(1.0 / zoom_factor);
  1429. }
  1430. } break;
  1431. case MouseButton::WHEEL_DOWN: {
  1432. if (is_freelook_active()) {
  1433. scale_freelook_speed(1.0 / zoom_factor);
  1434. } else {
  1435. scale_cursor_distance(zoom_factor);
  1436. }
  1437. } break;
  1438. case MouseButton::RIGHT: {
  1439. if (b->is_pressed() && _edit.gizmo.is_valid()) {
  1440. //restore
  1441. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, _edit.gizmo_initial_value, true);
  1442. _edit.gizmo = Ref<EditorNode3DGizmo>();
  1443. }
  1444. if (_edit.mode == TRANSFORM_NONE && b->is_pressed()) {
  1445. if (orbit_mouse_preference == NAVIGATION_RIGHT_MOUSE && _is_nav_modifier_pressed("spatial_editor/viewport_orbit_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_orbit_modifier_2")) {
  1446. break;
  1447. } else if (pan_mouse_preference == NAVIGATION_RIGHT_MOUSE && _is_nav_modifier_pressed("spatial_editor/viewport_pan_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_pan_modifier_2")) {
  1448. break;
  1449. } else if (zoom_mouse_preference == NAVIGATION_RIGHT_MOUSE && _is_nav_modifier_pressed("spatial_editor/viewport_zoom_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_zoom_modifier_2")) {
  1450. break;
  1451. }
  1452. if (b->is_alt_pressed()) {
  1453. _list_select(b);
  1454. return;
  1455. }
  1456. }
  1457. if (_edit.mode != TRANSFORM_NONE && b->is_pressed()) {
  1458. cancel_transform();
  1459. break;
  1460. }
  1461. if (b->is_pressed()) {
  1462. const Key mod = _get_key_modifier(b);
  1463. if (!orthogonal) {
  1464. if (mod == _get_key_modifier_setting("editors/3d/freelook/freelook_activation_modifier")) {
  1465. set_freelook_active(true);
  1466. }
  1467. }
  1468. } else {
  1469. set_freelook_active(false);
  1470. }
  1471. if (freelook_active && !surface->has_focus()) {
  1472. // Focus usually doesn't trigger on right-click, but in case of freelook it should,
  1473. // otherwise using keyboard navigation would misbehave
  1474. surface->grab_focus();
  1475. }
  1476. } break;
  1477. case MouseButton::MIDDLE: {
  1478. if (b->is_pressed() && _edit.mode != TRANSFORM_NONE) {
  1479. if (orbit_mouse_preference == NAVIGATION_MIDDLE_MOUSE && _is_nav_modifier_pressed("spatial_editor/viewport_orbit_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_orbit_modifier_2")) {
  1480. break;
  1481. } else if (pan_mouse_preference == NAVIGATION_MIDDLE_MOUSE && _is_nav_modifier_pressed("spatial_editor/viewport_pan_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_pan_modifier_2")) {
  1482. break;
  1483. } else if (zoom_mouse_preference == NAVIGATION_MIDDLE_MOUSE && _is_nav_modifier_pressed("spatial_editor/viewport_zoom_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_zoom_modifier_2")) {
  1484. break;
  1485. }
  1486. switch (_edit.plane) {
  1487. case TRANSFORM_VIEW: {
  1488. _edit.plane = TRANSFORM_X_AXIS;
  1489. set_message(TTR("X-Axis Transform."), 2);
  1490. view_type = VIEW_TYPE_USER;
  1491. _update_name();
  1492. } break;
  1493. case TRANSFORM_X_AXIS: {
  1494. _edit.plane = TRANSFORM_Y_AXIS;
  1495. set_message(TTR("Y-Axis Transform."), 2);
  1496. } break;
  1497. case TRANSFORM_Y_AXIS: {
  1498. _edit.plane = TRANSFORM_Z_AXIS;
  1499. set_message(TTR("Z-Axis Transform."), 2);
  1500. } break;
  1501. case TRANSFORM_Z_AXIS: {
  1502. _edit.plane = TRANSFORM_VIEW;
  1503. // TRANSLATORS: This refers to the transform of the view plane.
  1504. set_message(TTR("View Plane Transform."), 2);
  1505. } break;
  1506. case TRANSFORM_YZ:
  1507. case TRANSFORM_XZ:
  1508. case TRANSFORM_XY: {
  1509. } break;
  1510. }
  1511. }
  1512. } break;
  1513. case MouseButton::LEFT: {
  1514. if (b->is_pressed()) {
  1515. clicked_wants_append = b->is_shift_pressed();
  1516. if (_edit.mode != TRANSFORM_NONE && (_edit.instant || collision_reposition)) {
  1517. commit_transform();
  1518. break; // just commit the edit, stop processing the event so we don't deselect the object
  1519. }
  1520. if (orbit_mouse_preference == NAVIGATION_LEFT_MOUSE && _is_nav_modifier_pressed("spatial_editor/viewport_orbit_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_orbit_modifier_2")) {
  1521. break;
  1522. } else if (pan_mouse_preference == NAVIGATION_LEFT_MOUSE && _is_nav_modifier_pressed("spatial_editor/viewport_pan_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_pan_modifier_2")) {
  1523. break;
  1524. } else if (zoom_mouse_preference == NAVIGATION_LEFT_MOUSE && _is_nav_modifier_pressed("spatial_editor/viewport_zoom_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_zoom_modifier_2")) {
  1525. break;
  1526. }
  1527. if (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_LIST_SELECT) {
  1528. _list_select(b);
  1529. break;
  1530. }
  1531. _edit.mouse_pos = b->get_position();
  1532. _edit.original_mouse_pos = b->get_position();
  1533. _edit.snap = spatial_editor->is_snap_enabled();
  1534. _edit.mode = TRANSFORM_NONE;
  1535. _edit.original = spatial_editor->get_gizmo_transform(); // To prevent to break when flipping with scale.
  1536. bool can_select_gizmos = spatial_editor->get_single_selected_node();
  1537. {
  1538. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  1539. int idx2 = view_menu->get_popup()->get_item_index(VIEW_TRANSFORM_GIZMO);
  1540. can_select_gizmos = can_select_gizmos && view_menu->get_popup()->is_item_checked(idx);
  1541. transform_gizmo_visible = view_menu->get_popup()->is_item_checked(idx2);
  1542. }
  1543. // Gizmo handles
  1544. if (can_select_gizmos) {
  1545. Vector<Ref<Node3DGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos();
  1546. bool intersected_handle = false;
  1547. for (int i = 0; i < gizmos.size(); i++) {
  1548. Ref<EditorNode3DGizmo> seg = gizmos[i];
  1549. if ((!seg.is_valid())) {
  1550. continue;
  1551. }
  1552. int gizmo_handle = -1;
  1553. bool gizmo_secondary = false;
  1554. seg->handles_intersect_ray(camera, _edit.mouse_pos, b->is_shift_pressed(), gizmo_handle, gizmo_secondary);
  1555. if (gizmo_handle != -1) {
  1556. _edit.gizmo = seg;
  1557. seg->begin_handle_action(gizmo_handle, gizmo_secondary);
  1558. _edit.gizmo_handle = gizmo_handle;
  1559. _edit.gizmo_handle_secondary = gizmo_secondary;
  1560. _edit.gizmo_initial_value = seg->get_handle_value(gizmo_handle, gizmo_secondary);
  1561. intersected_handle = true;
  1562. break;
  1563. }
  1564. }
  1565. if (intersected_handle) {
  1566. break;
  1567. }
  1568. }
  1569. // Transform gizmo
  1570. if (_transform_gizmo_select(_edit.mouse_pos)) {
  1571. break;
  1572. }
  1573. // Subgizmos
  1574. if (can_select_gizmos) {
  1575. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(spatial_editor->get_single_selected_node());
  1576. Vector<Ref<Node3DGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos();
  1577. bool intersected_subgizmo = false;
  1578. for (int i = 0; i < gizmos.size(); i++) {
  1579. Ref<EditorNode3DGizmo> seg = gizmos[i];
  1580. if ((!seg.is_valid())) {
  1581. continue;
  1582. }
  1583. int subgizmo_id = seg->subgizmos_intersect_ray(camera, _edit.mouse_pos);
  1584. if (subgizmo_id != -1) {
  1585. ERR_CONTINUE(!se);
  1586. if (b->is_shift_pressed()) {
  1587. if (se->subgizmos.has(subgizmo_id)) {
  1588. se->subgizmos.erase(subgizmo_id);
  1589. } else {
  1590. se->subgizmos.insert(subgizmo_id, seg->get_subgizmo_transform(subgizmo_id));
  1591. }
  1592. } else {
  1593. se->subgizmos.clear();
  1594. se->subgizmos.insert(subgizmo_id, seg->get_subgizmo_transform(subgizmo_id));
  1595. }
  1596. if (se->subgizmos.is_empty()) {
  1597. se->gizmo = Ref<EditorNode3DGizmo>();
  1598. } else {
  1599. se->gizmo = seg;
  1600. }
  1601. seg->redraw();
  1602. spatial_editor->update_transform_gizmo();
  1603. intersected_subgizmo = true;
  1604. break;
  1605. }
  1606. }
  1607. if (intersected_subgizmo) {
  1608. break;
  1609. }
  1610. }
  1611. clicked = ObjectID();
  1612. bool node_selected = get_selected_count() > 0;
  1613. if (node_selected && ((spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT && b->is_command_or_control_pressed()) || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE)) {
  1614. begin_transform(TRANSFORM_ROTATE, false);
  1615. break;
  1616. }
  1617. if (node_selected && spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE) {
  1618. begin_transform(TRANSFORM_TRANSLATE, false);
  1619. break;
  1620. }
  1621. if (node_selected && spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE) {
  1622. begin_transform(TRANSFORM_SCALE, false);
  1623. break;
  1624. }
  1625. if (after != EditorPlugin::AFTER_GUI_INPUT_CUSTOM) {
  1626. // Single item selection.
  1627. clicked = _select_ray(b->get_position());
  1628. selection_in_progress = true;
  1629. if (clicked.is_null()) {
  1630. // Default to region select.
  1631. cursor.region_select = true;
  1632. cursor.region_begin = b->get_position();
  1633. cursor.region_end = b->get_position();
  1634. }
  1635. }
  1636. surface->queue_redraw();
  1637. } else {
  1638. if (_edit.gizmo.is_valid()) {
  1639. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, _edit.gizmo_initial_value, false);
  1640. spatial_editor->get_single_selected_node()->update_gizmos();
  1641. _edit.gizmo = Ref<EditorNode3DGizmo>();
  1642. break;
  1643. }
  1644. if (after != EditorPlugin::AFTER_GUI_INPUT_CUSTOM) {
  1645. selection_in_progress = false;
  1646. if (clicked.is_valid()) {
  1647. _select_clicked(false);
  1648. }
  1649. if (cursor.region_select) {
  1650. _select_region();
  1651. cursor.region_select = false;
  1652. surface->queue_redraw();
  1653. }
  1654. }
  1655. if (!_edit.instant && _edit.mode != TRANSFORM_NONE) {
  1656. Node3D *selected = spatial_editor->get_single_selected_node();
  1657. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  1658. if (se && se->gizmo.is_valid()) {
  1659. Vector<int> ids;
  1660. Vector<Transform3D> restore;
  1661. for (const KeyValue<int, Transform3D> &GE : se->subgizmos) {
  1662. ids.push_back(GE.key);
  1663. restore.push_back(GE.value);
  1664. }
  1665. se->gizmo->commit_subgizmos(ids, restore, false);
  1666. } else {
  1667. if (_edit.original_mouse_pos != _edit.mouse_pos) {
  1668. commit_transform();
  1669. }
  1670. }
  1671. _edit.mode = TRANSFORM_NONE;
  1672. set_message("");
  1673. spatial_editor->update_transform_gizmo();
  1674. }
  1675. surface->queue_redraw();
  1676. }
  1677. } break;
  1678. default:
  1679. break;
  1680. }
  1681. }
  1682. ViewportNavMouseButton orbit_mouse_preference = (ViewportNavMouseButton)EDITOR_GET("editors/3d/navigation/orbit_mouse_button").operator int();
  1683. ViewportNavMouseButton pan_mouse_preference = (ViewportNavMouseButton)EDITOR_GET("editors/3d/navigation/pan_mouse_button").operator int();
  1684. ViewportNavMouseButton zoom_mouse_preference = (ViewportNavMouseButton)EDITOR_GET("editors/3d/navigation/zoom_mouse_button").operator int();
  1685. bool orbit_mod_pressed = _is_nav_modifier_pressed("spatial_editor/viewport_orbit_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_orbit_modifier_2");
  1686. bool pan_mod_pressed = _is_nav_modifier_pressed("spatial_editor/viewport_pan_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_pan_modifier_2");
  1687. bool zoom_mod_pressed = _is_nav_modifier_pressed("spatial_editor/viewport_zoom_modifier_1") && _is_nav_modifier_pressed("spatial_editor/viewport_zoom_modifier_2");
  1688. int orbit_mod_input_count = _get_shortcut_input_count("spatial_editor/viewport_orbit_modifier_1") + _get_shortcut_input_count("spatial_editor/viewport_orbit_modifier_2");
  1689. int pan_mod_input_count = _get_shortcut_input_count("spatial_editor/viewport_pan_modifier_1") + _get_shortcut_input_count("spatial_editor/viewport_pan_modifier_2");
  1690. int zoom_mod_input_count = _get_shortcut_input_count("spatial_editor/viewport_zoom_modifier_1") + _get_shortcut_input_count("spatial_editor/viewport_zoom_modifier_2");
  1691. bool orbit_not_empty = !_is_shortcut_empty("spatial_editor/viewport_orbit_modifier_1") || !_is_shortcut_empty("spatial_editor/viewport_orbit_modifier_2");
  1692. bool pan_not_empty = !_is_shortcut_empty("spatial_editor/viewport_pan_modifier_1") || !_is_shortcut_empty("spatial_editor/viewport_pan_modifier_2");
  1693. bool zoom_not_empty = !_is_shortcut_empty("spatial_editor/viewport_zoom_modifier_1") || !_is_shortcut_empty("spatial_editor/viewport_zoom_modifier_2");
  1694. Vector<ShortcutCheckSet> shortcut_check_sets;
  1695. shortcut_check_sets.push_back(ShortcutCheckSet(orbit_mod_pressed, orbit_not_empty, orbit_mod_input_count, orbit_mouse_preference, NAVIGATION_ORBIT));
  1696. shortcut_check_sets.push_back(ShortcutCheckSet(pan_mod_pressed, pan_not_empty, pan_mod_input_count, pan_mouse_preference, NAVIGATION_PAN));
  1697. shortcut_check_sets.push_back(ShortcutCheckSet(zoom_mod_pressed, zoom_not_empty, zoom_mod_input_count, zoom_mouse_preference, NAVIGATION_ZOOM));
  1698. shortcut_check_sets.sort_custom<ShortcutCheckSetComparator>();
  1699. Ref<InputEventMouseMotion> m = p_event;
  1700. // Instant transforms process mouse motion in input() to handle wrapping.
  1701. if (m.is_valid() && !_edit.instant) {
  1702. _edit.mouse_pos = m->get_position();
  1703. if (spatial_editor->get_single_selected_node()) {
  1704. Vector<Ref<Node3DGizmo>> gizmos = spatial_editor->get_single_selected_node()->get_gizmos();
  1705. Ref<EditorNode3DGizmo> found_gizmo;
  1706. int found_handle = -1;
  1707. bool found_handle_secondary = false;
  1708. for (int i = 0; i < gizmos.size(); i++) {
  1709. Ref<EditorNode3DGizmo> seg = gizmos[i];
  1710. if (!seg.is_valid()) {
  1711. continue;
  1712. }
  1713. seg->handles_intersect_ray(camera, _edit.mouse_pos, false, found_handle, found_handle_secondary);
  1714. if (found_handle != -1) {
  1715. found_gizmo = seg;
  1716. break;
  1717. }
  1718. }
  1719. if (found_gizmo.is_valid()) {
  1720. spatial_editor->select_gizmo_highlight_axis(-1);
  1721. }
  1722. bool current_hover_handle_secondary = false;
  1723. int curreny_hover_handle = spatial_editor->get_current_hover_gizmo_handle(current_hover_handle_secondary);
  1724. if (found_gizmo != spatial_editor->get_current_hover_gizmo() || found_handle != curreny_hover_handle || found_handle_secondary != current_hover_handle_secondary) {
  1725. spatial_editor->set_current_hover_gizmo(found_gizmo);
  1726. spatial_editor->set_current_hover_gizmo_handle(found_handle, found_handle_secondary);
  1727. spatial_editor->get_single_selected_node()->update_gizmos();
  1728. }
  1729. }
  1730. if (spatial_editor->get_current_hover_gizmo().is_null() && !m->get_button_mask().has_flag(MouseButtonMask::LEFT) && !_edit.gizmo.is_valid()) {
  1731. _transform_gizmo_select(_edit.mouse_pos, true);
  1732. }
  1733. NavigationMode nav_mode = NAVIGATION_NONE;
  1734. if (_edit.gizmo.is_valid()) {
  1735. _edit.gizmo->set_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, camera, m->get_position());
  1736. Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle, _edit.gizmo_handle_secondary);
  1737. String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle, _edit.gizmo_handle_secondary);
  1738. set_message(n + ": " + String(v));
  1739. } else if (m->get_button_mask().has_flag(MouseButtonMask::LEFT)) {
  1740. NavigationMode change_nav_from_shortcut = _get_nav_mode_from_shortcut_check(NAVIGATION_LEFT_MOUSE, shortcut_check_sets, false);
  1741. if (change_nav_from_shortcut != NAVIGATION_NONE) {
  1742. nav_mode = change_nav_from_shortcut;
  1743. } else {
  1744. const bool movement_threshold_passed = _edit.original_mouse_pos.distance_to(_edit.mouse_pos) > 8 * EDSCALE;
  1745. if (selection_in_progress && movement_threshold_passed && clicked.is_valid()) {
  1746. if (clicked_wants_append || !editor_selection->is_selected(Object::cast_to<Node>(ObjectDB::get_instance(clicked)))) {
  1747. cursor.region_select = true;
  1748. cursor.region_begin = _edit.original_mouse_pos;
  1749. clicked = ObjectID();
  1750. }
  1751. }
  1752. if (cursor.region_select) {
  1753. cursor.region_end = m->get_position();
  1754. surface->queue_redraw();
  1755. return;
  1756. }
  1757. if (clicked.is_valid() && movement_threshold_passed) {
  1758. _compute_edit(_edit.original_mouse_pos);
  1759. clicked = ObjectID();
  1760. _edit.mode = TRANSFORM_TRANSLATE;
  1761. }
  1762. if (_edit.mode == TRANSFORM_NONE || _edit.numeric_input != 0 || _edit.numeric_next_decimal != 0) {
  1763. return;
  1764. }
  1765. update_transform(_get_key_modifier(m) == Key::SHIFT);
  1766. }
  1767. } else if (m->get_button_mask().has_flag(MouseButtonMask::RIGHT) || freelook_active) {
  1768. NavigationMode change_nav_from_shortcut = _get_nav_mode_from_shortcut_check(NAVIGATION_RIGHT_MOUSE, shortcut_check_sets, false);
  1769. if (m->get_button_mask().has_flag(MouseButtonMask::RIGHT) && change_nav_from_shortcut != NAVIGATION_NONE) {
  1770. nav_mode = change_nav_from_shortcut;
  1771. } else if (freelook_active) {
  1772. nav_mode = NAVIGATION_LOOK;
  1773. } else if (orthogonal) {
  1774. nav_mode = NAVIGATION_PAN;
  1775. }
  1776. } else if (m->get_button_mask().has_flag(MouseButtonMask::MIDDLE)) {
  1777. NavigationMode change_nav_from_shortcut = _get_nav_mode_from_shortcut_check(NAVIGATION_MIDDLE_MOUSE, shortcut_check_sets, false);
  1778. if (change_nav_from_shortcut != NAVIGATION_NONE) {
  1779. nav_mode = change_nav_from_shortcut;
  1780. }
  1781. } else if (EDITOR_GET("editors/3d/navigation/emulate_3_button_mouse")) {
  1782. // Handle trackpad (no external mouse) use case
  1783. NavigationMode change_nav_from_shortcut = _get_nav_mode_from_shortcut_check(NAVIGATION_LEFT_MOUSE, shortcut_check_sets, true);
  1784. if (change_nav_from_shortcut != NAVIGATION_NONE) {
  1785. nav_mode = change_nav_from_shortcut;
  1786. }
  1787. }
  1788. switch (nav_mode) {
  1789. case NAVIGATION_PAN: {
  1790. _nav_pan(m, _get_warped_mouse_motion(m));
  1791. } break;
  1792. case NAVIGATION_ZOOM: {
  1793. _nav_zoom(m, m->get_relative());
  1794. } break;
  1795. case NAVIGATION_ORBIT: {
  1796. _nav_orbit(m, _get_warped_mouse_motion(m));
  1797. } break;
  1798. case NAVIGATION_LOOK: {
  1799. _nav_look(m, _get_warped_mouse_motion(m));
  1800. } break;
  1801. default: {
  1802. }
  1803. }
  1804. }
  1805. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  1806. if (magnify_gesture.is_valid()) {
  1807. if (is_freelook_active()) {
  1808. scale_freelook_speed(magnify_gesture->get_factor());
  1809. } else {
  1810. scale_cursor_distance(1.0 / magnify_gesture->get_factor());
  1811. }
  1812. }
  1813. Ref<InputEventPanGesture> pan_gesture = p_event;
  1814. if (pan_gesture.is_valid()) {
  1815. NavigationMode nav_mode = NAVIGATION_NONE;
  1816. for (const ShortcutCheckSet &shortcut_check_set : shortcut_check_sets) {
  1817. if (shortcut_check_set.mod_pressed) {
  1818. nav_mode = shortcut_check_set.result_nav_mode;
  1819. break;
  1820. }
  1821. }
  1822. switch (nav_mode) {
  1823. case NAVIGATION_PAN: {
  1824. _nav_pan(pan_gesture, -pan_gesture->get_delta());
  1825. } break;
  1826. case NAVIGATION_ZOOM: {
  1827. _nav_zoom(pan_gesture, pan_gesture->get_delta());
  1828. } break;
  1829. case NAVIGATION_ORBIT: {
  1830. _nav_orbit(pan_gesture, -pan_gesture->get_delta());
  1831. } break;
  1832. case NAVIGATION_LOOK: {
  1833. _nav_look(pan_gesture, pan_gesture->get_delta());
  1834. } break;
  1835. default: {
  1836. }
  1837. }
  1838. }
  1839. Ref<InputEventKey> k = p_event;
  1840. if (k.is_valid()) {
  1841. if (!k->is_pressed()) {
  1842. return;
  1843. }
  1844. if (_edit.instant) {
  1845. // In a Blender-style transform, numbers set the magnitude of the transform.
  1846. // E.g. pressing g4.5x means "translate 4.5 units along the X axis".
  1847. // Use the Unicode value because we care about the text, not the actual keycode.
  1848. // This ensures numbers work consistently across different keyboard language layouts.
  1849. bool processed = true;
  1850. Key key = k->get_physical_keycode();
  1851. char32_t unicode = k->get_unicode();
  1852. if (unicode >= '0' && unicode <= '9') {
  1853. uint32_t value = uint32_t(unicode - Key::KEY_0);
  1854. if (_edit.numeric_next_decimal < 0) {
  1855. _edit.numeric_input = _edit.numeric_input + value * Math::pow(10.0, _edit.numeric_next_decimal--);
  1856. } else {
  1857. _edit.numeric_input = _edit.numeric_input * 10 + value;
  1858. }
  1859. update_transform_numeric();
  1860. } else if (unicode == '-') {
  1861. _edit.numeric_negate = !_edit.numeric_negate;
  1862. update_transform_numeric();
  1863. } else if (unicode == '.') {
  1864. if (_edit.numeric_next_decimal == 0) {
  1865. _edit.numeric_next_decimal = -1;
  1866. }
  1867. } else if (key == Key::ENTER || key == Key::KP_ENTER || key == Key::SPACE) {
  1868. commit_transform();
  1869. } else {
  1870. processed = false;
  1871. }
  1872. if (processed) {
  1873. // Ignore mouse inputs once we receive a numeric input.
  1874. set_process_input(false);
  1875. accept_event();
  1876. return;
  1877. }
  1878. }
  1879. if (EDITOR_GET("editors/3d/navigation/emulate_numpad")) {
  1880. const Key code = k->get_physical_keycode();
  1881. if (code >= Key::KEY_0 && code <= Key::KEY_9) {
  1882. k->set_keycode(code - Key::KEY_0 + Key::KP_0);
  1883. }
  1884. }
  1885. if (_edit.mode == TRANSFORM_NONE) {
  1886. if (_edit.gizmo.is_null() && is_freelook_active() && k->get_keycode() == Key::ESCAPE) {
  1887. set_freelook_active(false);
  1888. return;
  1889. }
  1890. if (_edit.gizmo.is_valid() && (k->get_keycode() == Key::ESCAPE || k->get_keycode() == Key::BACKSPACE)) {
  1891. // Restore.
  1892. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_handle_secondary, _edit.gizmo_initial_value, true);
  1893. _edit.gizmo = Ref<EditorNode3DGizmo>();
  1894. }
  1895. if (k->get_keycode() == Key::ESCAPE && !cursor.region_select) {
  1896. _clear_selected();
  1897. return;
  1898. }
  1899. } else {
  1900. // We're actively transforming, handle keys specially
  1901. TransformPlane new_plane = TRANSFORM_VIEW;
  1902. if (ED_IS_SHORTCUT("spatial_editor/lock_transform_x", p_event)) {
  1903. new_plane = TRANSFORM_X_AXIS;
  1904. } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_y", p_event)) {
  1905. new_plane = TRANSFORM_Y_AXIS;
  1906. } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_z", p_event)) {
  1907. new_plane = TRANSFORM_Z_AXIS;
  1908. } else if (_edit.mode != TRANSFORM_ROTATE) { // rotating on a plane doesn't make sense
  1909. if (ED_IS_SHORTCUT("spatial_editor/lock_transform_yz", p_event)) {
  1910. new_plane = TRANSFORM_YZ;
  1911. } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_xz", p_event)) {
  1912. new_plane = TRANSFORM_XZ;
  1913. } else if (ED_IS_SHORTCUT("spatial_editor/lock_transform_xy", p_event)) {
  1914. new_plane = TRANSFORM_XY;
  1915. }
  1916. }
  1917. if (new_plane != TRANSFORM_VIEW) {
  1918. if (new_plane != _edit.plane) {
  1919. // lock me once and get a global constraint
  1920. _edit.plane = new_plane;
  1921. spatial_editor->set_local_coords_enabled(false);
  1922. } else if (!spatial_editor->are_local_coords_enabled()) {
  1923. // lock me twice and get a local constraint
  1924. spatial_editor->set_local_coords_enabled(true);
  1925. } else {
  1926. // lock me thrice and we're back where we started
  1927. _edit.plane = TRANSFORM_VIEW;
  1928. spatial_editor->set_local_coords_enabled(false);
  1929. }
  1930. if (_edit.numeric_input != 0 || _edit.numeric_next_decimal != 0) {
  1931. update_transform_numeric();
  1932. } else {
  1933. update_transform(Input::get_singleton()->is_key_pressed(Key::SHIFT));
  1934. }
  1935. accept_event();
  1936. return;
  1937. }
  1938. }
  1939. if (ED_IS_SHORTCUT("spatial_editor/snap", p_event)) {
  1940. if (_edit.mode != TRANSFORM_NONE) {
  1941. _edit.snap = !_edit.snap;
  1942. }
  1943. }
  1944. if (ED_IS_SHORTCUT("spatial_editor/bottom_view", p_event)) {
  1945. _menu_option(VIEW_BOTTOM);
  1946. }
  1947. if (ED_IS_SHORTCUT("spatial_editor/top_view", p_event)) {
  1948. _menu_option(VIEW_TOP);
  1949. }
  1950. if (ED_IS_SHORTCUT("spatial_editor/rear_view", p_event)) {
  1951. _menu_option(VIEW_REAR);
  1952. }
  1953. if (ED_IS_SHORTCUT("spatial_editor/front_view", p_event)) {
  1954. _menu_option(VIEW_FRONT);
  1955. }
  1956. if (ED_IS_SHORTCUT("spatial_editor/left_view", p_event)) {
  1957. _menu_option(VIEW_LEFT);
  1958. }
  1959. if (ED_IS_SHORTCUT("spatial_editor/right_view", p_event)) {
  1960. _menu_option(VIEW_RIGHT);
  1961. }
  1962. if (ED_IS_SHORTCUT("spatial_editor/orbit_view_down", p_event)) {
  1963. // Clamp rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1964. cursor.x_rot = CLAMP(cursor.x_rot - Math_PI / 12.0, -1.57, 1.57);
  1965. view_type = VIEW_TYPE_USER;
  1966. _update_name();
  1967. }
  1968. if (ED_IS_SHORTCUT("spatial_editor/orbit_view_up", p_event)) {
  1969. // Clamp rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  1970. cursor.x_rot = CLAMP(cursor.x_rot + Math_PI / 12.0, -1.57, 1.57);
  1971. view_type = VIEW_TYPE_USER;
  1972. _update_name();
  1973. }
  1974. if (ED_IS_SHORTCUT("spatial_editor/orbit_view_right", p_event)) {
  1975. cursor.y_rot -= Math_PI / 12.0;
  1976. view_type = VIEW_TYPE_USER;
  1977. _update_name();
  1978. }
  1979. if (ED_IS_SHORTCUT("spatial_editor/orbit_view_left", p_event)) {
  1980. cursor.y_rot += Math_PI / 12.0;
  1981. view_type = VIEW_TYPE_USER;
  1982. _update_name();
  1983. }
  1984. if (ED_IS_SHORTCUT("spatial_editor/orbit_view_180", p_event)) {
  1985. cursor.y_rot += Math_PI;
  1986. view_type = VIEW_TYPE_USER;
  1987. _update_name();
  1988. }
  1989. if (ED_IS_SHORTCUT("spatial_editor/focus_origin", p_event)) {
  1990. _menu_option(VIEW_CENTER_TO_ORIGIN);
  1991. }
  1992. if (ED_IS_SHORTCUT("spatial_editor/focus_selection", p_event)) {
  1993. _menu_option(VIEW_CENTER_TO_SELECTION);
  1994. }
  1995. if (ED_IS_SHORTCUT("spatial_editor/align_transform_with_view", p_event)) {
  1996. _menu_option(VIEW_ALIGN_TRANSFORM_WITH_VIEW);
  1997. }
  1998. if (ED_IS_SHORTCUT("spatial_editor/align_rotation_with_view", p_event)) {
  1999. _menu_option(VIEW_ALIGN_ROTATION_WITH_VIEW);
  2000. }
  2001. if (ED_IS_SHORTCUT("spatial_editor/insert_anim_key", p_event)) {
  2002. if (!get_selected_count() || _edit.mode != TRANSFORM_NONE) {
  2003. return;
  2004. }
  2005. if (!AnimationPlayerEditor::get_singleton()->get_track_editor()->has_keying()) {
  2006. set_message(TTR("Keying is disabled (no key inserted)."));
  2007. return;
  2008. }
  2009. List<Node *> &selection = editor_selection->get_selected_node_list();
  2010. for (Node *E : selection) {
  2011. Node3D *sp = Object::cast_to<Node3D>(E);
  2012. if (!sp) {
  2013. continue;
  2014. }
  2015. spatial_editor->emit_signal(SNAME("transform_key_request"), sp, "", sp->get_transform());
  2016. }
  2017. set_message(TTR("Animation Key Inserted."));
  2018. }
  2019. if (ED_IS_SHORTCUT("spatial_editor/cancel_transform", p_event) && _edit.mode != TRANSFORM_NONE) {
  2020. cancel_transform();
  2021. }
  2022. if (!is_freelook_active() && !k->is_echo()) {
  2023. if (ED_IS_SHORTCUT("spatial_editor/instant_translate", p_event) && (_edit.mode != TRANSFORM_TRANSLATE || collision_reposition)) {
  2024. if (_edit.mode == TRANSFORM_NONE) {
  2025. begin_transform(TRANSFORM_TRANSLATE, true);
  2026. } else if (_edit.instant || collision_reposition) {
  2027. commit_transform();
  2028. begin_transform(TRANSFORM_TRANSLATE, true);
  2029. }
  2030. }
  2031. if (ED_IS_SHORTCUT("spatial_editor/instant_rotate", p_event) && _edit.mode != TRANSFORM_ROTATE) {
  2032. if (_edit.mode == TRANSFORM_NONE) {
  2033. begin_transform(TRANSFORM_ROTATE, true);
  2034. } else if (_edit.instant || collision_reposition) {
  2035. commit_transform();
  2036. begin_transform(TRANSFORM_ROTATE, true);
  2037. }
  2038. }
  2039. if (ED_IS_SHORTCUT("spatial_editor/instant_scale", p_event) && _edit.mode != TRANSFORM_SCALE) {
  2040. if (_edit.mode == TRANSFORM_NONE) {
  2041. begin_transform(TRANSFORM_SCALE, true);
  2042. } else if (_edit.instant || collision_reposition) {
  2043. commit_transform();
  2044. begin_transform(TRANSFORM_SCALE, true);
  2045. }
  2046. }
  2047. if (ED_IS_SHORTCUT("spatial_editor/collision_reposition", p_event) && editor_selection->get_selected_node_list().size() == 1 && !collision_reposition) {
  2048. if (_edit.mode == TRANSFORM_NONE || _edit.instant) {
  2049. if (_edit.mode == TRANSFORM_NONE) {
  2050. _compute_edit(_edit.mouse_pos);
  2051. } else {
  2052. commit_transform();
  2053. _compute_edit(_edit.mouse_pos);
  2054. }
  2055. _edit.mode = TRANSFORM_TRANSLATE;
  2056. collision_reposition = true;
  2057. }
  2058. }
  2059. }
  2060. // Freelook doesn't work in orthogonal mode.
  2061. if (!orthogonal && ED_IS_SHORTCUT("spatial_editor/freelook_toggle", p_event)) {
  2062. set_freelook_active(!is_freelook_active());
  2063. } else if (k->get_keycode() == Key::ESCAPE) {
  2064. set_freelook_active(false);
  2065. }
  2066. if (k->get_keycode() == Key::SPACE) {
  2067. if (!k->is_pressed()) {
  2068. emit_signal(SNAME("toggle_maximize_view"), this);
  2069. }
  2070. }
  2071. if (ED_IS_SHORTCUT("spatial_editor/decrease_fov", p_event)) {
  2072. scale_fov(-0.05);
  2073. }
  2074. if (ED_IS_SHORTCUT("spatial_editor/increase_fov", p_event)) {
  2075. scale_fov(0.05);
  2076. }
  2077. if (ED_IS_SHORTCUT("spatial_editor/reset_fov", p_event)) {
  2078. reset_fov();
  2079. }
  2080. }
  2081. // freelook uses most of the useful shortcuts, like save, so its ok
  2082. // to consider freelook active as end of the line for future events.
  2083. if (freelook_active) {
  2084. accept_event();
  2085. }
  2086. }
  2087. int Node3DEditorViewport::_get_shortcut_input_count(const String &p_name) {
  2088. Ref<Shortcut> check_shortcut = ED_GET_SHORTCUT(p_name);
  2089. ERR_FAIL_COND_V_MSG(check_shortcut.is_null(), 0, "The Shortcut was null, possible name mismatch.");
  2090. return check_shortcut->get_events().size();
  2091. }
  2092. Node3DEditorViewport::NavigationMode Node3DEditorViewport::_get_nav_mode_from_shortcut_check(ViewportNavMouseButton p_mouse_button, Vector<ShortcutCheckSet> p_shortcut_check_sets, bool p_use_not_empty) {
  2093. if (p_use_not_empty) {
  2094. for (const ShortcutCheckSet &shortcut_check_set : p_shortcut_check_sets) {
  2095. if (shortcut_check_set.mod_pressed && shortcut_check_set.shortcut_not_empty) {
  2096. return shortcut_check_set.result_nav_mode;
  2097. }
  2098. }
  2099. } else {
  2100. for (const ShortcutCheckSet &shortcut_check_set : p_shortcut_check_sets) {
  2101. if (shortcut_check_set.mouse_preference == p_mouse_button && shortcut_check_set.mod_pressed) {
  2102. return shortcut_check_set.result_nav_mode;
  2103. }
  2104. }
  2105. }
  2106. return NAVIGATION_NONE;
  2107. }
  2108. void Node3DEditorViewport::_nav_pan(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  2109. const NavigationScheme nav_scheme = (NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme").operator int();
  2110. real_t pan_speed = 1 / 150.0;
  2111. if (p_event.is_valid() && nav_scheme == NAVIGATION_MAYA && p_event->is_shift_pressed()) {
  2112. pan_speed *= 10;
  2113. }
  2114. Transform3D camera_transform;
  2115. camera_transform.translate_local(cursor.pos);
  2116. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  2117. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  2118. const bool invert_x_axis = EDITOR_GET("editors/3d/navigation/invert_x_axis");
  2119. const bool invert_y_axis = EDITOR_GET("editors/3d/navigation/invert_y_axis");
  2120. Vector3 translation(
  2121. (invert_x_axis ? -1 : 1) * -p_relative.x * pan_speed,
  2122. (invert_y_axis ? -1 : 1) * p_relative.y * pan_speed,
  2123. 0);
  2124. translation *= cursor.distance / DISTANCE_DEFAULT;
  2125. camera_transform.translate_local(translation);
  2126. cursor.pos = camera_transform.origin;
  2127. }
  2128. void Node3DEditorViewport::_nav_zoom(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  2129. const NavigationScheme nav_scheme = (NavigationScheme)EDITOR_GET("editors/3d/navigation/navigation_scheme").operator int();
  2130. real_t zoom_speed = 1 / 80.0;
  2131. if (p_event.is_valid() && nav_scheme == NAVIGATION_MAYA && p_event->is_shift_pressed()) {
  2132. zoom_speed *= 10;
  2133. }
  2134. NavigationZoomStyle zoom_style = (NavigationZoomStyle)EDITOR_GET("editors/3d/navigation/zoom_style").operator int();
  2135. if (zoom_style == NAVIGATION_ZOOM_HORIZONTAL) {
  2136. if (p_relative.x > 0) {
  2137. scale_cursor_distance(1 - p_relative.x * zoom_speed);
  2138. } else if (p_relative.x < 0) {
  2139. scale_cursor_distance(1.0 / (1 + p_relative.x * zoom_speed));
  2140. }
  2141. } else {
  2142. if (p_relative.y > 0) {
  2143. scale_cursor_distance(1 + p_relative.y * zoom_speed);
  2144. } else if (p_relative.y < 0) {
  2145. scale_cursor_distance(1.0 / (1 - p_relative.y * zoom_speed));
  2146. }
  2147. }
  2148. }
  2149. void Node3DEditorViewport::_nav_orbit(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  2150. if (lock_rotation) {
  2151. _nav_pan(p_event, p_relative);
  2152. return;
  2153. }
  2154. if (orthogonal && auto_orthogonal) {
  2155. _menu_option(VIEW_PERSPECTIVE);
  2156. }
  2157. const real_t degrees_per_pixel = EDITOR_GET("editors/3d/navigation_feel/orbit_sensitivity");
  2158. const real_t radians_per_pixel = Math::deg_to_rad(degrees_per_pixel);
  2159. const bool invert_y_axis = EDITOR_GET("editors/3d/navigation/invert_y_axis");
  2160. const bool invert_x_axis = EDITOR_GET("editors/3d/navigation/invert_x_axis");
  2161. if (invert_y_axis) {
  2162. cursor.x_rot -= p_relative.y * radians_per_pixel;
  2163. } else {
  2164. cursor.x_rot += p_relative.y * radians_per_pixel;
  2165. }
  2166. // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  2167. cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57);
  2168. if (invert_x_axis) {
  2169. cursor.y_rot -= p_relative.x * radians_per_pixel;
  2170. } else {
  2171. cursor.y_rot += p_relative.x * radians_per_pixel;
  2172. }
  2173. view_type = VIEW_TYPE_USER;
  2174. _update_name();
  2175. }
  2176. void Node3DEditorViewport::_nav_look(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  2177. if (orthogonal) {
  2178. _nav_pan(p_event, p_relative);
  2179. return;
  2180. }
  2181. if (orthogonal && auto_orthogonal) {
  2182. _menu_option(VIEW_PERSPECTIVE);
  2183. }
  2184. // Scale mouse sensitivity with camera FOV scale when zoomed in to make it easier to point at things.
  2185. const real_t degrees_per_pixel = real_t(EDITOR_GET("editors/3d/freelook/freelook_sensitivity")) * MIN(1.0, cursor.fov_scale);
  2186. const real_t radians_per_pixel = Math::deg_to_rad(degrees_per_pixel);
  2187. const bool invert_y_axis = EDITOR_GET("editors/3d/navigation/invert_y_axis");
  2188. // Note: do NOT assume the camera has the "current" transform, because it is interpolated and may have "lag".
  2189. const Transform3D prev_camera_transform = to_camera_transform(cursor);
  2190. if (invert_y_axis) {
  2191. cursor.x_rot -= p_relative.y * radians_per_pixel;
  2192. } else {
  2193. cursor.x_rot += p_relative.y * radians_per_pixel;
  2194. }
  2195. // Clamp the Y rotation to roughly -90..90 degrees so the user can't look upside-down and end up disoriented.
  2196. cursor.x_rot = CLAMP(cursor.x_rot, -1.57, 1.57);
  2197. cursor.y_rot += p_relative.x * radians_per_pixel;
  2198. // Look is like the opposite of Orbit: the focus point rotates around the camera
  2199. Transform3D camera_transform = to_camera_transform(cursor);
  2200. Vector3 pos = camera_transform.xform(Vector3(0, 0, 0));
  2201. Vector3 prev_pos = prev_camera_transform.xform(Vector3(0, 0, 0));
  2202. Vector3 diff = prev_pos - pos;
  2203. cursor.pos += diff;
  2204. view_type = VIEW_TYPE_USER;
  2205. _update_name();
  2206. }
  2207. void Node3DEditorViewport::set_freelook_active(bool active_now) {
  2208. if (!freelook_active && active_now) {
  2209. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  2210. cursor = camera_cursor;
  2211. // Make sure eye_pos is synced, because freelook referential is eye pos rather than orbit pos
  2212. Vector3 forward = to_camera_transform(cursor).basis.xform(Vector3(0, 0, -1));
  2213. cursor.eye_pos = cursor.pos - cursor.distance * forward;
  2214. // Also sync the camera cursor, otherwise switching to freelook will be trippy if inertia is active
  2215. camera_cursor.eye_pos = cursor.eye_pos;
  2216. if (EDITOR_GET("editors/3d/freelook/freelook_speed_zoom_link")) {
  2217. // Re-adjust freelook speed from the current zoom level
  2218. real_t base_speed = EDITOR_GET("editors/3d/freelook/freelook_base_speed");
  2219. freelook_speed = base_speed * cursor.distance;
  2220. }
  2221. previous_mouse_position = get_local_mouse_position();
  2222. // Hide mouse like in an FPS (warping doesn't work)
  2223. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_CAPTURED);
  2224. } else if (freelook_active && !active_now) {
  2225. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  2226. cursor = camera_cursor;
  2227. // Restore mouse
  2228. Input::get_singleton()->set_mouse_mode(Input::MOUSE_MODE_VISIBLE);
  2229. // Restore the previous mouse position when leaving freelook mode.
  2230. // This is done because leaving `Input.MOUSE_MODE_CAPTURED` will center the cursor
  2231. // due to OS limitations.
  2232. warp_mouse(previous_mouse_position);
  2233. }
  2234. freelook_active = active_now;
  2235. }
  2236. void Node3DEditorViewport::scale_fov(real_t p_fov_offset) {
  2237. cursor.fov_scale = CLAMP(cursor.fov_scale + p_fov_offset, 0.1, 2.5);
  2238. surface->queue_redraw();
  2239. }
  2240. void Node3DEditorViewport::reset_fov() {
  2241. cursor.fov_scale = 1.0;
  2242. surface->queue_redraw();
  2243. }
  2244. void Node3DEditorViewport::scale_cursor_distance(real_t scale) {
  2245. real_t min_distance = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN);
  2246. real_t max_distance = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX);
  2247. if (unlikely(min_distance > max_distance)) {
  2248. cursor.distance = (min_distance + max_distance) / 2;
  2249. } else {
  2250. cursor.distance = CLAMP(cursor.distance * scale, min_distance, max_distance);
  2251. }
  2252. if (cursor.distance == max_distance || cursor.distance == min_distance) {
  2253. zoom_failed_attempts_count++;
  2254. } else {
  2255. zoom_failed_attempts_count = 0;
  2256. }
  2257. zoom_indicator_delay = ZOOM_FREELOOK_INDICATOR_DELAY_S;
  2258. surface->queue_redraw();
  2259. }
  2260. void Node3DEditorViewport::scale_freelook_speed(real_t scale) {
  2261. real_t min_speed = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN);
  2262. real_t max_speed = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX);
  2263. if (unlikely(min_speed > max_speed)) {
  2264. freelook_speed = (min_speed + max_speed) / 2;
  2265. } else {
  2266. freelook_speed = CLAMP(freelook_speed * scale, min_speed, max_speed);
  2267. }
  2268. zoom_indicator_delay = ZOOM_FREELOOK_INDICATOR_DELAY_S;
  2269. surface->queue_redraw();
  2270. }
  2271. bool Node3DEditorViewport::_is_nav_modifier_pressed(const String &p_name) {
  2272. return _is_shortcut_empty(p_name) || Input::get_singleton()->is_action_pressed(p_name);
  2273. }
  2274. bool Node3DEditorViewport::_is_shortcut_empty(const String &p_name) {
  2275. Ref<Shortcut> check_shortcut = ED_GET_SHORTCUT(p_name);
  2276. ERR_FAIL_COND_V_MSG(check_shortcut.is_null(), true, "The Shortcut was null, possible name mismatch.");
  2277. return check_shortcut->get_events().is_empty();
  2278. }
  2279. Point2 Node3DEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const {
  2280. Point2 relative;
  2281. if (bool(EDITOR_GET("editors/3d/navigation/warped_mouse_panning"))) {
  2282. relative = Input::get_singleton()->warp_mouse_motion(p_ev_mouse_motion, surface->get_global_rect());
  2283. } else {
  2284. relative = p_ev_mouse_motion->get_relative();
  2285. }
  2286. return relative;
  2287. }
  2288. void Node3DEditorViewport::_update_freelook(real_t delta) {
  2289. if (!is_freelook_active()) {
  2290. return;
  2291. }
  2292. const FreelookNavigationScheme navigation_scheme = (FreelookNavigationScheme)EDITOR_GET("editors/3d/freelook/freelook_navigation_scheme").operator int();
  2293. Vector3 forward;
  2294. if (navigation_scheme == FREELOOK_FULLY_AXIS_LOCKED) {
  2295. // Forward/backward keys will always go straight forward/backward, never moving on the Y axis.
  2296. forward = Vector3(0, 0, -1).rotated(Vector3(0, 1, 0), camera->get_rotation().y);
  2297. } else {
  2298. // Forward/backward keys will be relative to the camera pitch.
  2299. forward = camera->get_transform().basis.xform(Vector3(0, 0, -1));
  2300. }
  2301. const Vector3 right = camera->get_transform().basis.xform(Vector3(1, 0, 0));
  2302. Vector3 up;
  2303. if (navigation_scheme == FREELOOK_PARTIALLY_AXIS_LOCKED || navigation_scheme == FREELOOK_FULLY_AXIS_LOCKED) {
  2304. // Up/down keys will always go up/down regardless of camera pitch.
  2305. up = Vector3(0, 1, 0);
  2306. } else {
  2307. // Up/down keys will be relative to the camera pitch.
  2308. up = camera->get_transform().basis.xform(Vector3(0, 1, 0));
  2309. }
  2310. Vector3 direction;
  2311. // Use actions from the inputmap, as this is the only way to reliably detect input in this method.
  2312. // See #54469 for more discussion and explanation.
  2313. Input *inp = Input::get_singleton();
  2314. if (inp->is_action_pressed("spatial_editor/freelook_left")) {
  2315. direction -= right;
  2316. }
  2317. if (inp->is_action_pressed("spatial_editor/freelook_right")) {
  2318. direction += right;
  2319. }
  2320. if (inp->is_action_pressed("spatial_editor/freelook_forward")) {
  2321. direction += forward;
  2322. }
  2323. if (inp->is_action_pressed("spatial_editor/freelook_backwards")) {
  2324. direction -= forward;
  2325. }
  2326. if (inp->is_action_pressed("spatial_editor/freelook_up")) {
  2327. direction += up;
  2328. }
  2329. if (inp->is_action_pressed("spatial_editor/freelook_down")) {
  2330. direction -= up;
  2331. }
  2332. real_t speed = freelook_speed;
  2333. if (inp->is_action_pressed("spatial_editor/freelook_speed_modifier")) {
  2334. speed *= 3.0;
  2335. }
  2336. if (inp->is_action_pressed("spatial_editor/freelook_slow_modifier")) {
  2337. speed *= 0.333333;
  2338. }
  2339. const Vector3 motion = direction * speed * delta;
  2340. cursor.pos += motion;
  2341. cursor.eye_pos += motion;
  2342. }
  2343. void Node3DEditorViewport::set_message(const String &p_message, float p_time) {
  2344. message = p_message;
  2345. message_time = p_time;
  2346. }
  2347. void Node3DEditorPlugin::edited_scene_changed() {
  2348. for (uint32_t i = 0; i < Node3DEditor::VIEWPORTS_COUNT; i++) {
  2349. Node3DEditorViewport *viewport = Node3DEditor::get_singleton()->get_editor_viewport(i);
  2350. if (viewport->is_visible()) {
  2351. viewport->notification(Control::NOTIFICATION_VISIBILITY_CHANGED);
  2352. }
  2353. }
  2354. }
  2355. void Node3DEditorViewport::_project_settings_changed() {
  2356. // Update shadow atlas if changed.
  2357. int shadowmap_size = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_size");
  2358. bool shadowmap_16_bits = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_16_bits");
  2359. int atlas_q0 = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_0_subdiv");
  2360. int atlas_q1 = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_1_subdiv");
  2361. int atlas_q2 = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_2_subdiv");
  2362. int atlas_q3 = GLOBAL_GET("rendering/lights_and_shadows/positional_shadow/atlas_quadrant_3_subdiv");
  2363. viewport->set_positional_shadow_atlas_size(shadowmap_size);
  2364. viewport->set_positional_shadow_atlas_16_bits(shadowmap_16_bits);
  2365. viewport->set_positional_shadow_atlas_quadrant_subdiv(0, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q0));
  2366. viewport->set_positional_shadow_atlas_quadrant_subdiv(1, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q1));
  2367. viewport->set_positional_shadow_atlas_quadrant_subdiv(2, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q2));
  2368. viewport->set_positional_shadow_atlas_quadrant_subdiv(3, Viewport::PositionalShadowAtlasQuadrantSubdiv(atlas_q3));
  2369. _update_shrink();
  2370. // Update MSAA, screen-space AA and debanding if changed
  2371. const int msaa_mode = GLOBAL_GET("rendering/anti_aliasing/quality/msaa_3d");
  2372. viewport->set_msaa_3d(Viewport::MSAA(msaa_mode));
  2373. const int ssaa_mode = GLOBAL_GET("rendering/anti_aliasing/quality/screen_space_aa");
  2374. viewport->set_screen_space_aa(Viewport::ScreenSpaceAA(ssaa_mode));
  2375. const bool use_taa = GLOBAL_GET("rendering/anti_aliasing/quality/use_taa");
  2376. viewport->set_use_taa(use_taa);
  2377. const bool transparent_background = GLOBAL_GET("rendering/viewport/transparent_background");
  2378. viewport->set_transparent_background(transparent_background);
  2379. const bool use_hdr_2d = GLOBAL_GET("rendering/viewport/hdr_2d");
  2380. viewport->set_use_hdr_2d(use_hdr_2d);
  2381. const bool use_debanding = GLOBAL_GET("rendering/anti_aliasing/quality/use_debanding");
  2382. viewport->set_use_debanding(use_debanding);
  2383. const bool use_occlusion_culling = GLOBAL_GET("rendering/occlusion_culling/use_occlusion_culling");
  2384. viewport->set_use_occlusion_culling(use_occlusion_culling);
  2385. const float mesh_lod_threshold = GLOBAL_GET("rendering/mesh_lod/lod_change/threshold_pixels");
  2386. viewport->set_mesh_lod_threshold(mesh_lod_threshold);
  2387. const Viewport::Scaling3DMode scaling_3d_mode = Viewport::Scaling3DMode(int(GLOBAL_GET("rendering/scaling_3d/mode")));
  2388. viewport->set_scaling_3d_mode(scaling_3d_mode);
  2389. const float scaling_3d_scale = GLOBAL_GET("rendering/scaling_3d/scale");
  2390. viewport->set_scaling_3d_scale(scaling_3d_scale);
  2391. const float fsr_sharpness = GLOBAL_GET("rendering/scaling_3d/fsr_sharpness");
  2392. viewport->set_fsr_sharpness(fsr_sharpness);
  2393. const float texture_mipmap_bias = GLOBAL_GET("rendering/textures/default_filters/texture_mipmap_bias");
  2394. viewport->set_texture_mipmap_bias(texture_mipmap_bias);
  2395. }
  2396. void Node3DEditorViewport::_notification(int p_what) {
  2397. switch (p_what) {
  2398. case NOTIFICATION_READY: {
  2399. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Node3DEditorViewport::_project_settings_changed));
  2400. } break;
  2401. case NOTIFICATION_VISIBILITY_CHANGED: {
  2402. bool vp_visible = is_visible_in_tree();
  2403. set_process(vp_visible);
  2404. set_physics_process(vp_visible);
  2405. if (vp_visible) {
  2406. orthogonal = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL));
  2407. _update_name();
  2408. _update_camera(0);
  2409. } else {
  2410. set_freelook_active(false);
  2411. }
  2412. callable_mp(this, &Node3DEditorViewport::update_transform_gizmo_view).call_deferred();
  2413. } break;
  2414. case NOTIFICATION_RESIZED: {
  2415. callable_mp(this, &Node3DEditorViewport::update_transform_gizmo_view).call_deferred();
  2416. } break;
  2417. case NOTIFICATION_PROCESS: {
  2418. real_t delta = get_process_delta_time();
  2419. if (zoom_indicator_delay > 0) {
  2420. zoom_indicator_delay -= delta;
  2421. if (zoom_indicator_delay <= 0) {
  2422. surface->queue_redraw();
  2423. zoom_limit_label->hide();
  2424. }
  2425. }
  2426. _update_navigation_controls_visibility();
  2427. _update_freelook(delta);
  2428. Node *scene_root = SceneTreeDock::get_singleton()->get_editor_data()->get_edited_scene_root();
  2429. if (previewing_cinema && scene_root != nullptr) {
  2430. Camera3D *cam = scene_root->get_viewport()->get_camera_3d();
  2431. if (cam != nullptr && cam != previewing) {
  2432. //then switch the viewport's camera to the scene's viewport camera
  2433. if (previewing != nullptr) {
  2434. previewing->disconnect(SceneStringName(tree_exited), callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  2435. }
  2436. previewing = cam;
  2437. previewing->connect(SceneStringName(tree_exited), callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  2438. RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), cam->get_camera());
  2439. surface->queue_redraw();
  2440. }
  2441. }
  2442. _update_camera(delta);
  2443. const HashMap<Node *, Object *> &selection = editor_selection->get_selection();
  2444. bool changed = false;
  2445. bool exist = false;
  2446. for (const KeyValue<Node *, Object *> &E : selection) {
  2447. Node3D *sp = Object::cast_to<Node3D>(E.key);
  2448. if (!sp) {
  2449. continue;
  2450. }
  2451. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  2452. if (!se) {
  2453. continue;
  2454. }
  2455. Transform3D t = sp->get_global_gizmo_transform();
  2456. AABB new_aabb = _calculate_spatial_bounds(sp);
  2457. exist = true;
  2458. if (se->last_xform == t && se->aabb == new_aabb && !se->last_xform_dirty) {
  2459. continue;
  2460. }
  2461. changed = true;
  2462. se->last_xform_dirty = false;
  2463. se->last_xform = t;
  2464. se->aabb = new_aabb;
  2465. Transform3D t_offset = t;
  2466. // apply AABB scaling before item's global transform
  2467. {
  2468. const Vector3 offset(0.005, 0.005, 0.005);
  2469. Basis aabb_s;
  2470. aabb_s.scale(se->aabb.size + offset);
  2471. t.translate_local(se->aabb.position - offset / 2);
  2472. t.basis = t.basis * aabb_s;
  2473. }
  2474. {
  2475. const Vector3 offset(0.01, 0.01, 0.01);
  2476. Basis aabb_s;
  2477. aabb_s.scale(se->aabb.size + offset);
  2478. t_offset.translate_local(se->aabb.position - offset / 2);
  2479. t_offset.basis = t_offset.basis * aabb_s;
  2480. }
  2481. RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance, t);
  2482. RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_offset, t_offset);
  2483. RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_xray, t);
  2484. RenderingServer::get_singleton()->instance_set_transform(se->sbox_instance_xray_offset, t_offset);
  2485. }
  2486. if (changed || (spatial_editor->is_gizmo_visible() && !exist)) {
  2487. spatial_editor->update_transform_gizmo();
  2488. }
  2489. if (message_time > 0) {
  2490. if (message != last_message) {
  2491. surface->queue_redraw();
  2492. last_message = message;
  2493. }
  2494. message_time -= get_process_delta_time();
  2495. if (message_time < 0) {
  2496. surface->queue_redraw();
  2497. }
  2498. }
  2499. bool show_info = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  2500. if (show_info != info_panel->is_visible()) {
  2501. info_panel->set_visible(show_info);
  2502. }
  2503. Camera3D *current_camera;
  2504. if (previewing) {
  2505. current_camera = previewing;
  2506. } else {
  2507. current_camera = camera;
  2508. }
  2509. if (show_info) {
  2510. const String viewport_size = vformat(U"%d × %d", viewport->get_size().x, viewport->get_size().y);
  2511. String text;
  2512. text += vformat(TTR("X: %s\n"), rtos(current_camera->get_position().x).pad_decimals(1));
  2513. text += vformat(TTR("Y: %s\n"), rtos(current_camera->get_position().y).pad_decimals(1));
  2514. text += vformat(TTR("Z: %s\n"), rtos(current_camera->get_position().z).pad_decimals(1));
  2515. text += "\n";
  2516. text += vformat(
  2517. TTR("Size: %s (%.1fMP)\n"),
  2518. viewport_size,
  2519. viewport->get_size().x * viewport->get_size().y * 0.000001);
  2520. text += "\n";
  2521. text += vformat(TTR("Objects: %d\n"), viewport->get_render_info(Viewport::RENDER_INFO_TYPE_VISIBLE, Viewport::RENDER_INFO_OBJECTS_IN_FRAME));
  2522. text += vformat(TTR("Primitives: %d\n"), viewport->get_render_info(Viewport::RENDER_INFO_TYPE_VISIBLE, Viewport::RENDER_INFO_PRIMITIVES_IN_FRAME));
  2523. text += vformat(TTR("Draw Calls: %d"), viewport->get_render_info(Viewport::RENDER_INFO_TYPE_VISIBLE, Viewport::RENDER_INFO_DRAW_CALLS_IN_FRAME));
  2524. info_label->set_text(text);
  2525. }
  2526. // FPS Counter.
  2527. bool show_fps = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME));
  2528. if (show_fps != frame_time_panel->is_visible()) {
  2529. frame_time_panel->set_visible(show_fps);
  2530. RS::get_singleton()->viewport_set_measure_render_time(viewport->get_viewport_rid(), show_fps);
  2531. for (int i = 0; i < FRAME_TIME_HISTORY; i++) {
  2532. // Initialize to 120 FPS, so that the initial estimation until we get enough data is always reasonable.
  2533. cpu_time_history[i] = 8.333333;
  2534. gpu_time_history[i] = 8.333333;
  2535. }
  2536. cpu_time_history_index = 0;
  2537. gpu_time_history_index = 0;
  2538. }
  2539. if (show_fps) {
  2540. cpu_time_history[cpu_time_history_index] = RS::get_singleton()->viewport_get_measured_render_time_cpu(viewport->get_viewport_rid());
  2541. cpu_time_history_index = (cpu_time_history_index + 1) % FRAME_TIME_HISTORY;
  2542. double cpu_time = 0.0;
  2543. for (int i = 0; i < FRAME_TIME_HISTORY; i++) {
  2544. cpu_time += cpu_time_history[i];
  2545. }
  2546. cpu_time /= FRAME_TIME_HISTORY;
  2547. // Prevent unrealistically low values.
  2548. cpu_time = MAX(0.01, cpu_time);
  2549. gpu_time_history[gpu_time_history_index] = RS::get_singleton()->viewport_get_measured_render_time_gpu(viewport->get_viewport_rid());
  2550. gpu_time_history_index = (gpu_time_history_index + 1) % FRAME_TIME_HISTORY;
  2551. double gpu_time = 0.0;
  2552. for (int i = 0; i < FRAME_TIME_HISTORY; i++) {
  2553. gpu_time += gpu_time_history[i];
  2554. }
  2555. gpu_time /= FRAME_TIME_HISTORY;
  2556. // Prevent division by zero for the FPS counter (and unrealistically low values).
  2557. // This limits the reported FPS to 100000.
  2558. gpu_time = MAX(0.01, gpu_time);
  2559. // Color labels depending on performance level ("good" = green, "OK" = yellow, "bad" = red).
  2560. // Middle point is at 15 ms.
  2561. cpu_time_label->set_text(vformat(TTR("CPU Time: %s ms"), rtos(cpu_time).pad_decimals(2)));
  2562. cpu_time_label->add_theme_color_override(
  2563. SceneStringName(font_color),
  2564. frame_time_gradient->get_color_at_offset(
  2565. Math::remap(cpu_time, 0, 30, 0, 1)));
  2566. gpu_time_label->set_text(vformat(TTR("GPU Time: %s ms"), rtos(gpu_time).pad_decimals(2)));
  2567. // Middle point is at 15 ms.
  2568. gpu_time_label->add_theme_color_override(
  2569. SceneStringName(font_color),
  2570. frame_time_gradient->get_color_at_offset(
  2571. Math::remap(gpu_time, 0, 30, 0, 1)));
  2572. const double fps = 1000.0 / gpu_time;
  2573. fps_label->set_text(vformat(TTR("FPS: %d"), fps));
  2574. // Middle point is at 60 FPS.
  2575. fps_label->add_theme_color_override(
  2576. SceneStringName(font_color),
  2577. frame_time_gradient->get_color_at_offset(
  2578. Math::remap(fps, 110, 10, 0, 1)));
  2579. }
  2580. bool show_cinema = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW));
  2581. cinema_label->set_visible(show_cinema);
  2582. if (show_cinema) {
  2583. float cinema_half_width = cinema_label->get_size().width / 2.0f;
  2584. cinema_label->set_anchor_and_offset(SIDE_LEFT, 0.5f, -cinema_half_width);
  2585. }
  2586. if (lock_rotation) {
  2587. float locked_half_width = locked_label->get_size().width / 2.0f;
  2588. locked_label->set_anchor_and_offset(SIDE_LEFT, 0.5f, -locked_half_width);
  2589. }
  2590. } break;
  2591. case NOTIFICATION_PHYSICS_PROCESS: {
  2592. if (collision_reposition) {
  2593. List<Node *> &selection = editor_selection->get_selected_node_list();
  2594. if (selection.size() == 1) {
  2595. Node3D *first_selected_node = Object::cast_to<Node3D>(selection.front()->get());
  2596. double snap = EDITOR_GET("interface/inspector/default_float_step");
  2597. int snap_step_decimals = Math::range_step_decimals(snap);
  2598. set_message(TTR("Translating:") + " (" + String::num(first_selected_node->get_global_position().x, snap_step_decimals) + ", " +
  2599. String::num(first_selected_node->get_global_position().y, snap_step_decimals) + ", " + String::num(first_selected_node->get_global_position().z, snap_step_decimals) + ")");
  2600. first_selected_node->set_global_position(spatial_editor->snap_point(_get_instance_position(_edit.mouse_pos, first_selected_node)));
  2601. }
  2602. }
  2603. if (!update_preview_node) {
  2604. return;
  2605. }
  2606. if (preview_node->is_inside_tree()) {
  2607. preview_node_pos = spatial_editor->snap_point(_get_instance_position(preview_node_viewport_pos, preview_node));
  2608. double snap = EDITOR_GET("interface/inspector/default_float_step");
  2609. int snap_step_decimals = Math::range_step_decimals(snap);
  2610. set_message(TTR("Instantiating:") + " (" + String::num(preview_node_pos.x, snap_step_decimals) + ", " +
  2611. String::num(preview_node_pos.y, snap_step_decimals) + ", " + String::num(preview_node_pos.z, snap_step_decimals) + ")");
  2612. Transform3D preview_gl_transform = Transform3D(Basis(), preview_node_pos);
  2613. preview_node->set_global_transform(preview_gl_transform);
  2614. if (!preview_node->is_visible()) {
  2615. preview_node->show();
  2616. }
  2617. }
  2618. update_preview_node = false;
  2619. } break;
  2620. case NOTIFICATION_APPLICATION_FOCUS_OUT:
  2621. case NOTIFICATION_WM_WINDOW_FOCUS_OUT: {
  2622. set_freelook_active(false);
  2623. cursor.region_select = false;
  2624. surface->queue_redraw();
  2625. } break;
  2626. case NOTIFICATION_ENTER_TREE: {
  2627. surface->connect(SceneStringName(draw), callable_mp(this, &Node3DEditorViewport::_draw));
  2628. surface->connect(SceneStringName(gui_input), callable_mp(this, &Node3DEditorViewport::_sinput));
  2629. surface->connect(SceneStringName(mouse_entered), callable_mp(this, &Node3DEditorViewport::_surface_mouse_enter));
  2630. surface->connect(SceneStringName(mouse_exited), callable_mp(this, &Node3DEditorViewport::_surface_mouse_exit));
  2631. surface->connect(SceneStringName(focus_entered), callable_mp(this, &Node3DEditorViewport::_surface_focus_enter));
  2632. surface->connect(SceneStringName(focus_exited), callable_mp(this, &Node3DEditorViewport::_surface_focus_exit));
  2633. _init_gizmo_instance(index);
  2634. } break;
  2635. case NOTIFICATION_EXIT_TREE: {
  2636. _finish_gizmo_instances();
  2637. } break;
  2638. case NOTIFICATION_THEME_CHANGED: {
  2639. view_menu->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
  2640. preview_camera->set_button_icon(get_editor_theme_icon(SNAME("Camera3D")));
  2641. Control *gui_base = EditorNode::get_singleton()->get_gui_base();
  2642. const Ref<StyleBox> &information_3d_stylebox = gui_base->get_theme_stylebox(SNAME("Information3dViewport"), EditorStringName(EditorStyles));
  2643. view_menu->begin_bulk_theme_override();
  2644. view_menu->add_theme_style_override(CoreStringName(normal), information_3d_stylebox);
  2645. view_menu->add_theme_style_override("normal_mirrored", information_3d_stylebox);
  2646. view_menu->add_theme_style_override("hover", information_3d_stylebox);
  2647. view_menu->add_theme_style_override("hover_mirrored", information_3d_stylebox);
  2648. view_menu->add_theme_style_override("hover_pressed", information_3d_stylebox);
  2649. view_menu->add_theme_style_override("hover_pressed_mirrored", information_3d_stylebox);
  2650. view_menu->add_theme_style_override(SceneStringName(pressed), information_3d_stylebox);
  2651. view_menu->add_theme_style_override("pressed_mirrored", information_3d_stylebox);
  2652. view_menu->add_theme_style_override("focus", information_3d_stylebox);
  2653. view_menu->add_theme_style_override("focus_mirrored", information_3d_stylebox);
  2654. view_menu->add_theme_style_override("disabled", information_3d_stylebox);
  2655. view_menu->add_theme_style_override("disabled_mirrored", information_3d_stylebox);
  2656. view_menu->end_bulk_theme_override();
  2657. preview_camera->begin_bulk_theme_override();
  2658. preview_camera->add_theme_style_override(CoreStringName(normal), information_3d_stylebox);
  2659. preview_camera->add_theme_style_override("normal_mirrored", information_3d_stylebox);
  2660. preview_camera->add_theme_style_override("hover", information_3d_stylebox);
  2661. preview_camera->add_theme_style_override("hover_mirrored", information_3d_stylebox);
  2662. preview_camera->add_theme_style_override("hover_pressed", information_3d_stylebox);
  2663. preview_camera->add_theme_style_override("hover_pressed_mirrored", information_3d_stylebox);
  2664. preview_camera->add_theme_style_override(SceneStringName(pressed), information_3d_stylebox);
  2665. preview_camera->add_theme_style_override("pressed_mirrored", information_3d_stylebox);
  2666. preview_camera->add_theme_style_override("focus", information_3d_stylebox);
  2667. preview_camera->add_theme_style_override("focus_mirrored", information_3d_stylebox);
  2668. preview_camera->add_theme_style_override("disabled", information_3d_stylebox);
  2669. preview_camera->add_theme_style_override("disabled_mirrored", information_3d_stylebox);
  2670. preview_camera->end_bulk_theme_override();
  2671. frame_time_gradient->set_color(0, get_theme_color(SNAME("success_color"), EditorStringName(Editor)));
  2672. frame_time_gradient->set_color(1, get_theme_color(SNAME("warning_color"), EditorStringName(Editor)));
  2673. frame_time_gradient->set_color(2, get_theme_color(SNAME("error_color"), EditorStringName(Editor)));
  2674. info_panel->add_theme_style_override(SceneStringName(panel), information_3d_stylebox);
  2675. frame_time_panel->add_theme_style_override(SceneStringName(panel), information_3d_stylebox);
  2676. // Set a minimum width to prevent the width from changing all the time
  2677. // when numbers vary rapidly. This minimum width is set based on a
  2678. // GPU time of 999.99 ms in the current editor language.
  2679. const float min_width = get_theme_font(SNAME("main"), EditorStringName(EditorFonts))->get_string_size(vformat(TTR("GPU Time: %s ms"), 999.99)).x;
  2680. frame_time_panel->set_custom_minimum_size(Size2(min_width, 0) * EDSCALE);
  2681. frame_time_vbox->add_theme_constant_override("separation", Math::round(-1 * EDSCALE));
  2682. cinema_label->add_theme_style_override(CoreStringName(normal), information_3d_stylebox);
  2683. locked_label->add_theme_style_override(CoreStringName(normal), information_3d_stylebox);
  2684. } break;
  2685. case NOTIFICATION_DRAG_END: {
  2686. // Clear preview material when dropped outside applicable object.
  2687. if (spatial_editor->get_preview_material().is_valid() && !is_drag_successful()) {
  2688. _reset_preview_material();
  2689. _remove_preview_material();
  2690. } else {
  2691. _remove_preview_node();
  2692. }
  2693. } break;
  2694. }
  2695. }
  2696. static void draw_indicator_bar(Control &p_surface, real_t p_fill, const Ref<Texture2D> p_icon, const Ref<Font> p_font, int p_font_size, const String &p_text, const Color &p_color) {
  2697. // Adjust bar size from control height
  2698. const Vector2 surface_size = p_surface.get_size();
  2699. const real_t h = surface_size.y / 2.0;
  2700. const real_t y = (surface_size.y - h) / 2.0;
  2701. const Rect2 r(10 * EDSCALE, y, 6 * EDSCALE, h);
  2702. const real_t sy = r.size.y * p_fill;
  2703. // Note: because this bar appears over the viewport, it has to stay readable for any background color
  2704. // Draw both neutral dark and bright colors to account this
  2705. p_surface.draw_rect(r, p_color * Color(1, 1, 1, 0.2));
  2706. p_surface.draw_rect(Rect2(r.position.x, r.position.y + r.size.y - sy, r.size.x, sy), p_color * Color(1, 1, 1, 0.6));
  2707. p_surface.draw_rect(r.grow(1), Color(0, 0, 0, 0.7), false, Math::round(EDSCALE));
  2708. const Vector2 icon_size = p_icon->get_size();
  2709. const Vector2 icon_pos = Vector2(r.position.x - (icon_size.x - r.size.x) / 2, r.position.y + r.size.y + 2 * EDSCALE);
  2710. p_surface.draw_texture(p_icon, icon_pos, p_color);
  2711. // Draw text below the bar (for speed/zoom information).
  2712. p_surface.draw_string_outline(p_font, Vector2(icon_pos.x, icon_pos.y + icon_size.y + 16 * EDSCALE), p_text, HORIZONTAL_ALIGNMENT_LEFT, -1.f, p_font_size, Math::round(2 * EDSCALE), Color(0, 0, 0));
  2713. p_surface.draw_string(p_font, Vector2(icon_pos.x, icon_pos.y + icon_size.y + 16 * EDSCALE), p_text, HORIZONTAL_ALIGNMENT_LEFT, -1.f, p_font_size, p_color);
  2714. }
  2715. void Node3DEditorViewport::_draw() {
  2716. EditorPluginList *over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_over();
  2717. if (!over_plugin_list->is_empty()) {
  2718. over_plugin_list->forward_3d_draw_over_viewport(surface);
  2719. }
  2720. EditorPluginList *force_over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_force_over();
  2721. if (!force_over_plugin_list->is_empty()) {
  2722. force_over_plugin_list->forward_3d_force_draw_over_viewport(surface);
  2723. }
  2724. if (surface->has_focus()) {
  2725. Size2 size = surface->get_size();
  2726. Rect2 r = Rect2(Point2(), size);
  2727. get_theme_stylebox(SNAME("FocusViewport"), EditorStringName(EditorStyles))->draw(surface->get_canvas_item(), r);
  2728. }
  2729. if (cursor.region_select) {
  2730. const Rect2 selection_rect = Rect2(cursor.region_begin, cursor.region_end - cursor.region_begin);
  2731. surface->draw_rect(
  2732. selection_rect,
  2733. get_theme_color(SNAME("box_selection_fill_color"), EditorStringName(Editor)));
  2734. surface->draw_rect(
  2735. selection_rect,
  2736. get_theme_color(SNAME("box_selection_stroke_color"), EditorStringName(Editor)),
  2737. false,
  2738. Math::round(EDSCALE));
  2739. }
  2740. RID ci = surface->get_canvas_item();
  2741. if (message_time > 0) {
  2742. Ref<Font> font = get_theme_font(SceneStringName(font), SNAME("Label"));
  2743. int font_size = get_theme_font_size(SceneStringName(font_size), SNAME("Label"));
  2744. Point2 msgpos = Point2(5, get_size().y - 20);
  2745. font->draw_string(ci, msgpos + Point2(1, 1), message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
  2746. font->draw_string(ci, msgpos + Point2(-1, -1), message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(0, 0, 0, 0.8));
  2747. font->draw_string(ci, msgpos, message, HORIZONTAL_ALIGNMENT_LEFT, -1, font_size, Color(1, 1, 1, 1));
  2748. }
  2749. if (_edit.mode == TRANSFORM_ROTATE && _edit.show_rotation_line) {
  2750. Point2 center = point_to_screen(_edit.center);
  2751. Color handle_color;
  2752. switch (_edit.plane) {
  2753. case TRANSFORM_X_AXIS:
  2754. handle_color = get_theme_color(SNAME("axis_x_color"), EditorStringName(Editor));
  2755. break;
  2756. case TRANSFORM_Y_AXIS:
  2757. handle_color = get_theme_color(SNAME("axis_y_color"), EditorStringName(Editor));
  2758. break;
  2759. case TRANSFORM_Z_AXIS:
  2760. handle_color = get_theme_color(SNAME("axis_z_color"), EditorStringName(Editor));
  2761. break;
  2762. default:
  2763. handle_color = get_theme_color(SNAME("accent_color"), EditorStringName(Editor));
  2764. break;
  2765. }
  2766. handle_color = handle_color.from_hsv(handle_color.get_h(), 0.25, 1.0, 1);
  2767. RenderingServer::get_singleton()->canvas_item_add_line(
  2768. ci,
  2769. _edit.mouse_pos,
  2770. center,
  2771. handle_color,
  2772. Math::round(2 * EDSCALE));
  2773. }
  2774. if (previewing) {
  2775. Size2 ss = Size2(GLOBAL_GET("display/window/size/viewport_width"), GLOBAL_GET("display/window/size/viewport_height"));
  2776. float aspect = ss.aspect();
  2777. Size2 s = get_size();
  2778. Rect2 draw_rect;
  2779. switch (previewing->get_keep_aspect_mode()) {
  2780. case Camera3D::KEEP_WIDTH: {
  2781. draw_rect.size = Size2(s.width, s.width / aspect);
  2782. draw_rect.position.x = 0;
  2783. draw_rect.position.y = (s.height - draw_rect.size.y) * 0.5;
  2784. } break;
  2785. case Camera3D::KEEP_HEIGHT: {
  2786. draw_rect.size = Size2(s.height * aspect, s.height);
  2787. draw_rect.position.y = 0;
  2788. draw_rect.position.x = (s.width - draw_rect.size.x) * 0.5;
  2789. } break;
  2790. }
  2791. draw_rect = Rect2(Vector2(), s).intersection(draw_rect);
  2792. surface->draw_rect(draw_rect, Color(0.6, 0.6, 0.1, 0.5), false, Math::round(2 * EDSCALE));
  2793. } else {
  2794. if (zoom_indicator_delay > 0.0) {
  2795. if (is_freelook_active()) {
  2796. // Show speed
  2797. real_t min_speed = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN);
  2798. real_t max_speed = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX);
  2799. real_t scale_length = (max_speed - min_speed);
  2800. if (!Math::is_zero_approx(scale_length)) {
  2801. real_t logscale_t = 1.0 - Math::log1p(freelook_speed - min_speed) / Math::log1p(scale_length);
  2802. // Display the freelook speed to help the user get a better sense of scale.
  2803. const int precision = freelook_speed < 1.0 ? 2 : 1;
  2804. draw_indicator_bar(
  2805. *surface,
  2806. 1.0 - logscale_t,
  2807. get_editor_theme_icon(SNAME("ViewportSpeed")),
  2808. get_theme_font(SceneStringName(font), SNAME("Label")),
  2809. get_theme_font_size(SceneStringName(font_size), SNAME("Label")),
  2810. vformat("%s m/s", String::num(freelook_speed).pad_decimals(precision)),
  2811. Color(1.0, 0.95, 0.7));
  2812. }
  2813. } else {
  2814. // Show zoom
  2815. zoom_limit_label->set_visible(zoom_failed_attempts_count > 15);
  2816. real_t min_distance = MAX(camera->get_near() * 4, ZOOM_FREELOOK_MIN);
  2817. real_t max_distance = MIN(camera->get_far() / 4, ZOOM_FREELOOK_MAX);
  2818. real_t scale_length = (max_distance - min_distance);
  2819. if (!Math::is_zero_approx(scale_length)) {
  2820. real_t logscale_t = 1.0 - Math::log1p(cursor.distance - min_distance) / Math::log1p(scale_length);
  2821. // Display the zoom center distance to help the user get a better sense of scale.
  2822. const int precision = cursor.distance < 1.0 ? 2 : 1;
  2823. draw_indicator_bar(
  2824. *surface,
  2825. logscale_t,
  2826. get_editor_theme_icon(SNAME("ViewportZoom")),
  2827. get_theme_font(SceneStringName(font), SNAME("Label")),
  2828. get_theme_font_size(SceneStringName(font_size), SNAME("Label")),
  2829. vformat("%s m", String::num(cursor.distance).pad_decimals(precision)),
  2830. Color(0.7, 0.95, 1.0));
  2831. }
  2832. }
  2833. }
  2834. }
  2835. }
  2836. void Node3DEditorViewport::_menu_option(int p_option) {
  2837. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  2838. switch (p_option) {
  2839. case VIEW_TOP: {
  2840. cursor.y_rot = 0;
  2841. cursor.x_rot = Math_PI / 2.0;
  2842. set_message(TTR("Top View."), 2);
  2843. view_type = VIEW_TYPE_TOP;
  2844. _set_auto_orthogonal();
  2845. _update_name();
  2846. } break;
  2847. case VIEW_BOTTOM: {
  2848. cursor.y_rot = 0;
  2849. cursor.x_rot = -Math_PI / 2.0;
  2850. set_message(TTR("Bottom View."), 2);
  2851. view_type = VIEW_TYPE_BOTTOM;
  2852. _set_auto_orthogonal();
  2853. _update_name();
  2854. } break;
  2855. case VIEW_LEFT: {
  2856. cursor.x_rot = 0;
  2857. cursor.y_rot = Math_PI / 2.0;
  2858. set_message(TTR("Left View."), 2);
  2859. view_type = VIEW_TYPE_LEFT;
  2860. _set_auto_orthogonal();
  2861. _update_name();
  2862. } break;
  2863. case VIEW_RIGHT: {
  2864. cursor.x_rot = 0;
  2865. cursor.y_rot = -Math_PI / 2.0;
  2866. set_message(TTR("Right View."), 2);
  2867. view_type = VIEW_TYPE_RIGHT;
  2868. _set_auto_orthogonal();
  2869. _update_name();
  2870. } break;
  2871. case VIEW_FRONT: {
  2872. cursor.x_rot = 0;
  2873. cursor.y_rot = 0;
  2874. set_message(TTR("Front View."), 2);
  2875. view_type = VIEW_TYPE_FRONT;
  2876. _set_auto_orthogonal();
  2877. _update_name();
  2878. } break;
  2879. case VIEW_REAR: {
  2880. cursor.x_rot = 0;
  2881. cursor.y_rot = Math_PI;
  2882. set_message(TTR("Rear View."), 2);
  2883. view_type = VIEW_TYPE_REAR;
  2884. _set_auto_orthogonal();
  2885. _update_name();
  2886. } break;
  2887. case VIEW_CENTER_TO_ORIGIN: {
  2888. cursor.pos = Vector3(0, 0, 0);
  2889. } break;
  2890. case VIEW_CENTER_TO_SELECTION: {
  2891. focus_selection();
  2892. } break;
  2893. case VIEW_ALIGN_TRANSFORM_WITH_VIEW: {
  2894. if (!get_selected_count()) {
  2895. break;
  2896. }
  2897. Transform3D camera_transform = camera->get_global_transform();
  2898. List<Node *> &selection = editor_selection->get_selected_node_list();
  2899. undo_redo->create_action(TTR("Align Transform with View"));
  2900. for (Node *E : selection) {
  2901. Node3D *sp = Object::cast_to<Node3D>(E);
  2902. if (!sp) {
  2903. continue;
  2904. }
  2905. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  2906. if (!se) {
  2907. continue;
  2908. }
  2909. Transform3D xform;
  2910. if (orthogonal) {
  2911. xform = sp->get_global_transform();
  2912. xform.basis = Basis::from_euler(camera_transform.basis.get_euler());
  2913. } else {
  2914. xform = camera_transform;
  2915. xform.scale_basis(sp->get_scale());
  2916. }
  2917. if (Object::cast_to<Decal>(E)) {
  2918. // Adjust rotation to match Decal's default orientation.
  2919. // This makes the decal "look" in the same direction as the camera,
  2920. // rather than pointing down relative to the camera orientation.
  2921. xform.basis.rotate_local(Vector3(1, 0, 0), Math_TAU * 0.25);
  2922. }
  2923. Node3D *parent = sp->get_parent_node_3d();
  2924. Transform3D local_xform = parent ? parent->get_global_transform().affine_inverse() * xform : xform;
  2925. undo_redo->add_do_method(sp, "set_transform", local_xform);
  2926. undo_redo->add_undo_method(sp, "set_transform", sp->get_local_gizmo_transform());
  2927. }
  2928. undo_redo->commit_action();
  2929. } break;
  2930. case VIEW_ALIGN_ROTATION_WITH_VIEW: {
  2931. if (!get_selected_count()) {
  2932. break;
  2933. }
  2934. Transform3D camera_transform = camera->get_global_transform();
  2935. List<Node *> &selection = editor_selection->get_selected_node_list();
  2936. undo_redo->create_action(TTR("Align Rotation with View"));
  2937. for (Node *E : selection) {
  2938. Node3D *sp = Object::cast_to<Node3D>(E);
  2939. if (!sp) {
  2940. continue;
  2941. }
  2942. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  2943. if (!se) {
  2944. continue;
  2945. }
  2946. Basis basis = camera_transform.basis;
  2947. if (Object::cast_to<Decal>(E)) {
  2948. // Adjust rotation to match Decal's default orientation.
  2949. // This makes the decal "look" in the same direction as the camera,
  2950. // rather than pointing down relative to the camera orientation.
  2951. basis.rotate_local(Vector3(1, 0, 0), Math_TAU * 0.25);
  2952. }
  2953. undo_redo->add_do_method(sp, "set_rotation", basis.get_euler_normalized());
  2954. undo_redo->add_undo_method(sp, "set_rotation", sp->get_rotation());
  2955. }
  2956. undo_redo->commit_action();
  2957. } break;
  2958. case VIEW_ENVIRONMENT: {
  2959. int idx = view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT);
  2960. bool current = view_menu->get_popup()->is_item_checked(idx);
  2961. current = !current;
  2962. if (current) {
  2963. camera->set_environment(Ref<Resource>());
  2964. } else {
  2965. camera->set_environment(Node3DEditor::get_singleton()->get_viewport_environment());
  2966. }
  2967. view_menu->get_popup()->set_item_checked(idx, current);
  2968. } break;
  2969. case VIEW_PERSPECTIVE: {
  2970. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  2971. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), false);
  2972. orthogonal = false;
  2973. auto_orthogonal = false;
  2974. callable_mp(this, &Node3DEditorViewport::update_transform_gizmo_view).call_deferred();
  2975. _update_camera(0);
  2976. _update_name();
  2977. } break;
  2978. case VIEW_ORTHOGONAL: {
  2979. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), false);
  2980. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), true);
  2981. orthogonal = true;
  2982. auto_orthogonal = false;
  2983. callable_mp(this, &Node3DEditorViewport::update_transform_gizmo_view).call_deferred();
  2984. _update_camera(0);
  2985. _update_name();
  2986. } break;
  2987. case VIEW_SWITCH_PERSPECTIVE_ORTHOGONAL: {
  2988. _menu_option(orthogonal ? VIEW_PERSPECTIVE : VIEW_ORTHOGONAL);
  2989. } break;
  2990. case VIEW_AUTO_ORTHOGONAL: {
  2991. int idx = view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL);
  2992. bool current = view_menu->get_popup()->is_item_checked(idx);
  2993. current = !current;
  2994. view_menu->get_popup()->set_item_checked(idx, current);
  2995. if (auto_orthogonal) {
  2996. auto_orthogonal = false;
  2997. _update_name();
  2998. }
  2999. } break;
  3000. case VIEW_LOCK_ROTATION: {
  3001. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  3002. bool current = view_menu->get_popup()->is_item_checked(idx);
  3003. _set_lock_view_rotation(!current);
  3004. } break;
  3005. case VIEW_AUDIO_LISTENER: {
  3006. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  3007. bool current = view_menu->get_popup()->is_item_checked(idx);
  3008. current = !current;
  3009. viewport->set_as_audio_listener_3d(current);
  3010. view_menu->get_popup()->set_item_checked(idx, current);
  3011. } break;
  3012. case VIEW_AUDIO_DOPPLER: {
  3013. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  3014. bool current = view_menu->get_popup()->is_item_checked(idx);
  3015. current = !current;
  3016. camera->set_doppler_tracking(current ? Camera3D::DOPPLER_TRACKING_IDLE_STEP : Camera3D::DOPPLER_TRACKING_DISABLED);
  3017. view_menu->get_popup()->set_item_checked(idx, current);
  3018. } break;
  3019. case VIEW_CINEMATIC_PREVIEW: {
  3020. int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW);
  3021. bool current = view_menu->get_popup()->is_item_checked(idx);
  3022. current = !current;
  3023. view_menu->get_popup()->set_item_checked(idx, current);
  3024. previewing_cinema = true;
  3025. _toggle_cinema_preview(current);
  3026. if (current) {
  3027. preview_camera->hide();
  3028. } else {
  3029. if (previewing != nullptr) {
  3030. preview_camera->show();
  3031. }
  3032. }
  3033. } break;
  3034. case VIEW_GIZMOS: {
  3035. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  3036. bool current = view_menu->get_popup()->is_item_checked(idx);
  3037. current = !current;
  3038. uint32_t layers = camera->get_cull_mask();
  3039. layers &= ~(1 << GIZMO_EDIT_LAYER);
  3040. if (current) {
  3041. layers |= (1 << GIZMO_EDIT_LAYER);
  3042. }
  3043. camera->set_cull_mask(layers);
  3044. view_menu->get_popup()->set_item_checked(idx, current);
  3045. } break;
  3046. case VIEW_TRANSFORM_GIZMO: {
  3047. int idx = view_menu->get_popup()->get_item_index(VIEW_TRANSFORM_GIZMO);
  3048. bool current = view_menu->get_popup()->is_item_checked(idx);
  3049. current = !current;
  3050. transform_gizmo_visible = current;
  3051. spatial_editor->update_transform_gizmo();
  3052. view_menu->get_popup()->set_item_checked(idx, current);
  3053. } break;
  3054. case VIEW_HALF_RESOLUTION: {
  3055. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  3056. bool current = view_menu->get_popup()->is_item_checked(idx);
  3057. view_menu->get_popup()->set_item_checked(idx, !current);
  3058. _update_shrink();
  3059. } break;
  3060. case VIEW_INFORMATION: {
  3061. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  3062. bool current = view_menu->get_popup()->is_item_checked(idx);
  3063. view_menu->get_popup()->set_item_checked(idx, !current);
  3064. } break;
  3065. case VIEW_FRAME_TIME: {
  3066. int idx = view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME);
  3067. bool current = view_menu->get_popup()->is_item_checked(idx);
  3068. view_menu->get_popup()->set_item_checked(idx, !current);
  3069. } break;
  3070. case VIEW_GRID: {
  3071. int idx = view_menu->get_popup()->get_item_index(VIEW_GRID);
  3072. bool current = view_menu->get_popup()->is_item_checked(idx);
  3073. current = !current;
  3074. uint32_t layers = camera->get_cull_mask();
  3075. layers &= ~(1 << GIZMO_GRID_LAYER);
  3076. if (current) {
  3077. layers |= (1 << GIZMO_GRID_LAYER);
  3078. }
  3079. camera->set_cull_mask(layers);
  3080. view_menu->get_popup()->set_item_checked(idx, current);
  3081. } break;
  3082. case VIEW_DISPLAY_NORMAL:
  3083. case VIEW_DISPLAY_WIREFRAME:
  3084. case VIEW_DISPLAY_OVERDRAW:
  3085. case VIEW_DISPLAY_UNSHADED:
  3086. case VIEW_DISPLAY_LIGHTING:
  3087. case VIEW_DISPLAY_NORMAL_BUFFER:
  3088. case VIEW_DISPLAY_DEBUG_SHADOW_ATLAS:
  3089. case VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS:
  3090. case VIEW_DISPLAY_DEBUG_VOXEL_GI_ALBEDO:
  3091. case VIEW_DISPLAY_DEBUG_VOXEL_GI_LIGHTING:
  3092. case VIEW_DISPLAY_DEBUG_VOXEL_GI_EMISSION:
  3093. case VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE:
  3094. case VIEW_DISPLAY_DEBUG_SSAO:
  3095. case VIEW_DISPLAY_DEBUG_SSIL:
  3096. case VIEW_DISPLAY_DEBUG_PSSM_SPLITS:
  3097. case VIEW_DISPLAY_DEBUG_DECAL_ATLAS:
  3098. case VIEW_DISPLAY_DEBUG_SDFGI:
  3099. case VIEW_DISPLAY_DEBUG_SDFGI_PROBES:
  3100. case VIEW_DISPLAY_DEBUG_GI_BUFFER:
  3101. case VIEW_DISPLAY_DEBUG_DISABLE_LOD:
  3102. case VIEW_DISPLAY_DEBUG_CLUSTER_OMNI_LIGHTS:
  3103. case VIEW_DISPLAY_DEBUG_CLUSTER_SPOT_LIGHTS:
  3104. case VIEW_DISPLAY_DEBUG_CLUSTER_DECALS:
  3105. case VIEW_DISPLAY_DEBUG_CLUSTER_REFLECTION_PROBES:
  3106. case VIEW_DISPLAY_DEBUG_OCCLUDERS:
  3107. case VIEW_DISPLAY_MOTION_VECTORS:
  3108. case VIEW_DISPLAY_INTERNAL_BUFFER: {
  3109. static const int display_options[] = {
  3110. VIEW_DISPLAY_NORMAL,
  3111. VIEW_DISPLAY_WIREFRAME,
  3112. VIEW_DISPLAY_OVERDRAW,
  3113. VIEW_DISPLAY_UNSHADED,
  3114. VIEW_DISPLAY_LIGHTING,
  3115. VIEW_DISPLAY_NORMAL_BUFFER,
  3116. VIEW_DISPLAY_DEBUG_SHADOW_ATLAS,
  3117. VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS,
  3118. VIEW_DISPLAY_DEBUG_VOXEL_GI_ALBEDO,
  3119. VIEW_DISPLAY_DEBUG_VOXEL_GI_LIGHTING,
  3120. VIEW_DISPLAY_DEBUG_VOXEL_GI_EMISSION,
  3121. VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE,
  3122. VIEW_DISPLAY_DEBUG_SSAO,
  3123. VIEW_DISPLAY_DEBUG_SSIL,
  3124. VIEW_DISPLAY_DEBUG_GI_BUFFER,
  3125. VIEW_DISPLAY_DEBUG_DISABLE_LOD,
  3126. VIEW_DISPLAY_DEBUG_PSSM_SPLITS,
  3127. VIEW_DISPLAY_DEBUG_DECAL_ATLAS,
  3128. VIEW_DISPLAY_DEBUG_SDFGI,
  3129. VIEW_DISPLAY_DEBUG_SDFGI_PROBES,
  3130. VIEW_DISPLAY_DEBUG_CLUSTER_OMNI_LIGHTS,
  3131. VIEW_DISPLAY_DEBUG_CLUSTER_SPOT_LIGHTS,
  3132. VIEW_DISPLAY_DEBUG_CLUSTER_DECALS,
  3133. VIEW_DISPLAY_DEBUG_CLUSTER_REFLECTION_PROBES,
  3134. VIEW_DISPLAY_DEBUG_OCCLUDERS,
  3135. VIEW_DISPLAY_MOTION_VECTORS,
  3136. VIEW_DISPLAY_INTERNAL_BUFFER,
  3137. VIEW_MAX
  3138. };
  3139. static const Viewport::DebugDraw debug_draw_modes[] = {
  3140. Viewport::DEBUG_DRAW_DISABLED,
  3141. Viewport::DEBUG_DRAW_WIREFRAME,
  3142. Viewport::DEBUG_DRAW_OVERDRAW,
  3143. Viewport::DEBUG_DRAW_UNSHADED,
  3144. Viewport::DEBUG_DRAW_LIGHTING,
  3145. Viewport::DEBUG_DRAW_NORMAL_BUFFER,
  3146. Viewport::DEBUG_DRAW_SHADOW_ATLAS,
  3147. Viewport::DEBUG_DRAW_DIRECTIONAL_SHADOW_ATLAS,
  3148. Viewport::DEBUG_DRAW_VOXEL_GI_ALBEDO,
  3149. Viewport::DEBUG_DRAW_VOXEL_GI_LIGHTING,
  3150. Viewport::DEBUG_DRAW_VOXEL_GI_EMISSION,
  3151. Viewport::DEBUG_DRAW_SCENE_LUMINANCE,
  3152. Viewport::DEBUG_DRAW_SSAO,
  3153. Viewport::DEBUG_DRAW_SSIL,
  3154. Viewport::DEBUG_DRAW_GI_BUFFER,
  3155. Viewport::DEBUG_DRAW_DISABLE_LOD,
  3156. Viewport::DEBUG_DRAW_PSSM_SPLITS,
  3157. Viewport::DEBUG_DRAW_DECAL_ATLAS,
  3158. Viewport::DEBUG_DRAW_SDFGI,
  3159. Viewport::DEBUG_DRAW_SDFGI_PROBES,
  3160. Viewport::DEBUG_DRAW_CLUSTER_OMNI_LIGHTS,
  3161. Viewport::DEBUG_DRAW_CLUSTER_SPOT_LIGHTS,
  3162. Viewport::DEBUG_DRAW_CLUSTER_DECALS,
  3163. Viewport::DEBUG_DRAW_CLUSTER_REFLECTION_PROBES,
  3164. Viewport::DEBUG_DRAW_OCCLUDERS,
  3165. Viewport::DEBUG_DRAW_MOTION_VECTORS,
  3166. Viewport::DEBUG_DRAW_INTERNAL_BUFFER,
  3167. };
  3168. for (int idx = 0; display_options[idx] != VIEW_MAX; idx++) {
  3169. int id = display_options[idx];
  3170. int item_idx = view_menu->get_popup()->get_item_index(id);
  3171. if (item_idx != -1) {
  3172. view_menu->get_popup()->set_item_checked(item_idx, id == p_option);
  3173. }
  3174. item_idx = display_submenu->get_item_index(id);
  3175. if (item_idx != -1) {
  3176. display_submenu->set_item_checked(item_idx, id == p_option);
  3177. }
  3178. if (id == p_option) {
  3179. viewport->set_debug_draw(debug_draw_modes[idx]);
  3180. }
  3181. }
  3182. } break;
  3183. }
  3184. }
  3185. void Node3DEditorViewport::_set_auto_orthogonal() {
  3186. if (!orthogonal && view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL))) {
  3187. _menu_option(VIEW_ORTHOGONAL);
  3188. auto_orthogonal = true;
  3189. }
  3190. }
  3191. void Node3DEditorViewport::_preview_exited_scene() {
  3192. preview_camera->disconnect(SceneStringName(toggled), callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview));
  3193. preview_camera->set_pressed(false);
  3194. _toggle_camera_preview(false);
  3195. preview_camera->connect(SceneStringName(toggled), callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview));
  3196. view_menu->show();
  3197. }
  3198. void Node3DEditorViewport::_init_gizmo_instance(int p_idx) {
  3199. uint32_t layer = 1 << (GIZMO_BASE_LAYER + p_idx);
  3200. for (int i = 0; i < 3; i++) {
  3201. move_gizmo_instance[i] = RS::get_singleton()->instance_create();
  3202. RS::get_singleton()->instance_set_base(move_gizmo_instance[i], spatial_editor->get_move_gizmo(i)->get_rid());
  3203. RS::get_singleton()->instance_set_scenario(move_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  3204. RS::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  3205. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  3206. RS::get_singleton()->instance_set_layer_mask(move_gizmo_instance[i], layer);
  3207. RS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  3208. RS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  3209. move_plane_gizmo_instance[i] = RS::get_singleton()->instance_create();
  3210. RS::get_singleton()->instance_set_base(move_plane_gizmo_instance[i], spatial_editor->get_move_plane_gizmo(i)->get_rid());
  3211. RS::get_singleton()->instance_set_scenario(move_plane_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  3212. RS::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  3213. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_plane_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  3214. RS::get_singleton()->instance_set_layer_mask(move_plane_gizmo_instance[i], layer);
  3215. RS::get_singleton()->instance_geometry_set_flag(move_plane_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  3216. RS::get_singleton()->instance_geometry_set_flag(move_plane_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  3217. rotate_gizmo_instance[i] = RS::get_singleton()->instance_create();
  3218. RS::get_singleton()->instance_set_base(rotate_gizmo_instance[i], spatial_editor->get_rotate_gizmo(i)->get_rid());
  3219. RS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  3220. RS::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  3221. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  3222. RS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[i], layer);
  3223. RS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  3224. RS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  3225. scale_gizmo_instance[i] = RS::get_singleton()->instance_create();
  3226. RS::get_singleton()->instance_set_base(scale_gizmo_instance[i], spatial_editor->get_scale_gizmo(i)->get_rid());
  3227. RS::get_singleton()->instance_set_scenario(scale_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  3228. RS::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  3229. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  3230. RS::get_singleton()->instance_set_layer_mask(scale_gizmo_instance[i], layer);
  3231. RS::get_singleton()->instance_geometry_set_flag(scale_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  3232. RS::get_singleton()->instance_geometry_set_flag(scale_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  3233. scale_plane_gizmo_instance[i] = RS::get_singleton()->instance_create();
  3234. RS::get_singleton()->instance_set_base(scale_plane_gizmo_instance[i], spatial_editor->get_scale_plane_gizmo(i)->get_rid());
  3235. RS::get_singleton()->instance_set_scenario(scale_plane_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  3236. RS::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  3237. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_plane_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  3238. RS::get_singleton()->instance_set_layer_mask(scale_plane_gizmo_instance[i], layer);
  3239. RS::get_singleton()->instance_geometry_set_flag(scale_plane_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  3240. RS::get_singleton()->instance_geometry_set_flag(scale_plane_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  3241. axis_gizmo_instance[i] = RS::get_singleton()->instance_create();
  3242. RS::get_singleton()->instance_set_base(axis_gizmo_instance[i], spatial_editor->get_axis_gizmo(i)->get_rid());
  3243. RS::get_singleton()->instance_set_scenario(axis_gizmo_instance[i], get_tree()->get_root()->get_world_3d()->get_scenario());
  3244. RS::get_singleton()->instance_set_visible(axis_gizmo_instance[i], true);
  3245. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(axis_gizmo_instance[i], RS::SHADOW_CASTING_SETTING_OFF);
  3246. RS::get_singleton()->instance_set_layer_mask(axis_gizmo_instance[i], layer);
  3247. RS::get_singleton()->instance_geometry_set_flag(axis_gizmo_instance[i], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  3248. RS::get_singleton()->instance_geometry_set_flag(axis_gizmo_instance[i], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  3249. }
  3250. // Rotation white outline
  3251. rotate_gizmo_instance[3] = RS::get_singleton()->instance_create();
  3252. RS::get_singleton()->instance_set_base(rotate_gizmo_instance[3], spatial_editor->get_rotate_gizmo(3)->get_rid());
  3253. RS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[3], get_tree()->get_root()->get_world_3d()->get_scenario());
  3254. RS::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false);
  3255. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[3], RS::SHADOW_CASTING_SETTING_OFF);
  3256. RS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[3], layer);
  3257. RS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[3], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  3258. RS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[3], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  3259. }
  3260. void Node3DEditorViewport::_finish_gizmo_instances() {
  3261. ERR_FAIL_NULL(RenderingServer::get_singleton());
  3262. for (int i = 0; i < 3; i++) {
  3263. RS::get_singleton()->free(move_gizmo_instance[i]);
  3264. RS::get_singleton()->free(move_plane_gizmo_instance[i]);
  3265. RS::get_singleton()->free(rotate_gizmo_instance[i]);
  3266. RS::get_singleton()->free(scale_gizmo_instance[i]);
  3267. RS::get_singleton()->free(scale_plane_gizmo_instance[i]);
  3268. RS::get_singleton()->free(axis_gizmo_instance[i]);
  3269. }
  3270. // Rotation white outline
  3271. RS::get_singleton()->free(rotate_gizmo_instance[3]);
  3272. }
  3273. void Node3DEditorViewport::_toggle_camera_preview(bool p_activate) {
  3274. ERR_FAIL_COND(p_activate && !preview);
  3275. ERR_FAIL_COND(!p_activate && !previewing);
  3276. previewing_camera = p_activate;
  3277. _update_navigation_controls_visibility();
  3278. if (!p_activate) {
  3279. previewing->disconnect(SceneStringName(tree_exiting), callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  3280. previewing = nullptr;
  3281. RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  3282. if (!preview) {
  3283. preview_camera->hide();
  3284. }
  3285. surface->queue_redraw();
  3286. } else {
  3287. previewing = preview;
  3288. previewing->connect(SceneStringName(tree_exiting), callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  3289. RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), preview->get_camera()); //replace
  3290. surface->queue_redraw();
  3291. }
  3292. }
  3293. void Node3DEditorViewport::_toggle_cinema_preview(bool p_activate) {
  3294. previewing_cinema = p_activate;
  3295. _update_navigation_controls_visibility();
  3296. if (!previewing_cinema) {
  3297. if (previewing != nullptr) {
  3298. previewing->disconnect(SceneStringName(tree_exited), callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  3299. }
  3300. previewing = nullptr;
  3301. RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  3302. preview_camera->set_pressed(false);
  3303. if (!preview) {
  3304. preview_camera->hide();
  3305. } else {
  3306. preview_camera->show();
  3307. }
  3308. view_menu->show();
  3309. surface->queue_redraw();
  3310. }
  3311. }
  3312. void Node3DEditorViewport::_selection_result_pressed(int p_result) {
  3313. if (selection_results_menu.size() <= p_result) {
  3314. return;
  3315. }
  3316. clicked = selection_results_menu[p_result]->get_instance_id();
  3317. if (clicked.is_valid()) {
  3318. _select_clicked(spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT);
  3319. }
  3320. selection_results_menu.clear();
  3321. }
  3322. void Node3DEditorViewport::_selection_menu_hide() {
  3323. selection_results.clear();
  3324. selection_menu->clear();
  3325. selection_menu->reset_size();
  3326. }
  3327. void Node3DEditorViewport::set_can_preview(Camera3D *p_preview) {
  3328. preview = p_preview;
  3329. if (!preview_camera->is_pressed() && !previewing_cinema) {
  3330. preview_camera->set_visible(p_preview);
  3331. }
  3332. }
  3333. void Node3DEditorViewport::update_transform_gizmo_view() {
  3334. if (!is_visible_in_tree()) {
  3335. return;
  3336. }
  3337. Transform3D xform = spatial_editor->get_gizmo_transform();
  3338. Transform3D camera_xform = camera->get_transform();
  3339. if (xform.origin.is_equal_approx(camera_xform.origin)) {
  3340. for (int i = 0; i < 3; i++) {
  3341. RenderingServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  3342. RenderingServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  3343. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  3344. RenderingServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  3345. RenderingServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  3346. RenderingServer::get_singleton()->instance_set_visible(axis_gizmo_instance[i], false);
  3347. }
  3348. // Rotation white outline
  3349. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false);
  3350. return;
  3351. }
  3352. const Vector3 camz = -camera_xform.get_basis().get_column(2).normalized();
  3353. const Vector3 camy = -camera_xform.get_basis().get_column(1).normalized();
  3354. const Plane p = Plane(camz, camera_xform.origin);
  3355. const real_t gizmo_d = MAX(Math::abs(p.distance_to(xform.origin)), CMP_EPSILON);
  3356. const real_t d0 = camera->unproject_position(camera_xform.origin + camz * gizmo_d).y;
  3357. const real_t d1 = camera->unproject_position(camera_xform.origin + camz * gizmo_d + camy).y;
  3358. const real_t dd = MAX(Math::abs(d0 - d1), CMP_EPSILON);
  3359. const real_t gizmo_size = EDITOR_GET("editors/3d/manipulator_gizmo_size");
  3360. // At low viewport heights, multiply the gizmo scale based on the viewport height.
  3361. // This prevents the gizmo from growing very large and going outside the viewport.
  3362. const int viewport_base_height = 400 * MAX(1, EDSCALE);
  3363. gizmo_scale =
  3364. (gizmo_size / Math::abs(dd)) * MAX(1, EDSCALE) *
  3365. MIN(viewport_base_height, subviewport_container->get_size().height) / viewport_base_height /
  3366. subviewport_container->get_stretch_shrink();
  3367. Vector3 scale = Vector3(1, 1, 1) * gizmo_scale;
  3368. // if the determinant is zero, we should disable the gizmo from being rendered
  3369. // this prevents supplying bad values to the renderer and then having to filter it out again
  3370. if (xform.basis.determinant() == 0) {
  3371. for (int i = 0; i < 3; i++) {
  3372. RenderingServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  3373. RenderingServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  3374. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  3375. RenderingServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  3376. RenderingServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  3377. }
  3378. // Rotation white outline
  3379. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], false);
  3380. return;
  3381. }
  3382. bool show_gizmo = spatial_editor->is_gizmo_visible() && !_edit.instant && transform_gizmo_visible && !collision_reposition;
  3383. for (int i = 0; i < 3; i++) {
  3384. Transform3D axis_angle;
  3385. if (xform.basis.get_column(i).normalized().dot(xform.basis.get_column((i + 1) % 3).normalized()) < 1.0) {
  3386. axis_angle = axis_angle.looking_at(xform.basis.get_column(i).normalized(), xform.basis.get_column((i + 1) % 3).normalized());
  3387. }
  3388. axis_angle.basis.scale(scale);
  3389. axis_angle.origin = xform.origin;
  3390. RenderingServer::get_singleton()->instance_set_transform(move_gizmo_instance[i], axis_angle);
  3391. RenderingServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], show_gizmo && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE));
  3392. RenderingServer::get_singleton()->instance_set_transform(move_plane_gizmo_instance[i], axis_angle);
  3393. RenderingServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], show_gizmo && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_MOVE));
  3394. RenderingServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[i], axis_angle);
  3395. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], show_gizmo && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE));
  3396. RenderingServer::get_singleton()->instance_set_transform(scale_gizmo_instance[i], axis_angle);
  3397. RenderingServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], show_gizmo && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE));
  3398. RenderingServer::get_singleton()->instance_set_transform(scale_plane_gizmo_instance[i], axis_angle);
  3399. RenderingServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], show_gizmo && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SCALE));
  3400. RenderingServer::get_singleton()->instance_set_transform(axis_gizmo_instance[i], xform);
  3401. }
  3402. bool show_axes = spatial_editor->is_gizmo_visible() && _edit.mode != TRANSFORM_NONE;
  3403. RenderingServer *rs = RenderingServer::get_singleton();
  3404. rs->instance_set_visible(axis_gizmo_instance[0], show_axes && (_edit.plane == TRANSFORM_X_AXIS || _edit.plane == TRANSFORM_XY || _edit.plane == TRANSFORM_XZ));
  3405. rs->instance_set_visible(axis_gizmo_instance[1], show_axes && (_edit.plane == TRANSFORM_Y_AXIS || _edit.plane == TRANSFORM_XY || _edit.plane == TRANSFORM_YZ));
  3406. rs->instance_set_visible(axis_gizmo_instance[2], show_axes && (_edit.plane == TRANSFORM_Z_AXIS || _edit.plane == TRANSFORM_XZ || _edit.plane == TRANSFORM_YZ));
  3407. // Rotation white outline
  3408. xform.orthonormalize();
  3409. xform.basis.scale(scale);
  3410. RenderingServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[3], xform);
  3411. RenderingServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[3], spatial_editor->is_gizmo_visible() && !_edit.instant && transform_gizmo_visible && !collision_reposition && (spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == Node3DEditor::TOOL_MODE_ROTATE));
  3412. }
  3413. void Node3DEditorViewport::set_state(const Dictionary &p_state) {
  3414. if (p_state.has("position")) {
  3415. cursor.pos = p_state["position"];
  3416. }
  3417. if (p_state.has("x_rotation")) {
  3418. cursor.x_rot = p_state["x_rotation"];
  3419. }
  3420. if (p_state.has("y_rotation")) {
  3421. cursor.y_rot = p_state["y_rotation"];
  3422. }
  3423. if (p_state.has("distance")) {
  3424. cursor.distance = p_state["distance"];
  3425. }
  3426. if (p_state.has("orthogonal")) {
  3427. bool orth = p_state["orthogonal"];
  3428. _menu_option(orth ? VIEW_ORTHOGONAL : VIEW_PERSPECTIVE);
  3429. }
  3430. if (p_state.has("view_type")) {
  3431. view_type = ViewType(p_state["view_type"].operator int());
  3432. _update_name();
  3433. }
  3434. if (p_state.has("auto_orthogonal")) {
  3435. auto_orthogonal = p_state["auto_orthogonal"];
  3436. _update_name();
  3437. }
  3438. if (p_state.has("auto_orthogonal_enabled")) {
  3439. bool enabled = p_state["auto_orthogonal_enabled"];
  3440. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL), enabled);
  3441. }
  3442. if (p_state.has("display_mode")) {
  3443. int display = p_state["display_mode"];
  3444. int idx = view_menu->get_popup()->get_item_index(display);
  3445. if (idx != -1 && !view_menu->get_popup()->is_item_checked(idx)) {
  3446. _menu_option(display);
  3447. } else {
  3448. idx = display_submenu->get_item_index(display);
  3449. if (idx != -1 && !display_submenu->is_item_checked(idx)) {
  3450. _menu_option(display);
  3451. }
  3452. }
  3453. }
  3454. if (p_state.has("lock_rotation")) {
  3455. _set_lock_view_rotation(p_state["lock_rotation"]);
  3456. }
  3457. if (p_state.has("use_environment")) {
  3458. bool env = p_state["use_environment"];
  3459. if (env != camera->get_environment().is_valid()) {
  3460. _menu_option(VIEW_ENVIRONMENT);
  3461. }
  3462. }
  3463. if (p_state.has("listener")) {
  3464. bool listener = p_state["listener"];
  3465. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  3466. viewport->set_as_audio_listener_3d(listener);
  3467. view_menu->get_popup()->set_item_checked(idx, listener);
  3468. }
  3469. if (p_state.has("doppler")) {
  3470. bool doppler = p_state["doppler"];
  3471. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  3472. camera->set_doppler_tracking(doppler ? Camera3D::DOPPLER_TRACKING_IDLE_STEP : Camera3D::DOPPLER_TRACKING_DISABLED);
  3473. view_menu->get_popup()->set_item_checked(idx, doppler);
  3474. }
  3475. if (p_state.has("gizmos")) {
  3476. bool gizmos = p_state["gizmos"];
  3477. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  3478. if (view_menu->get_popup()->is_item_checked(idx) != gizmos) {
  3479. _menu_option(VIEW_GIZMOS);
  3480. }
  3481. }
  3482. if (p_state.has("transform_gizmo")) {
  3483. bool transform_gizmo = p_state["transform_gizmo"];
  3484. int idx = view_menu->get_popup()->get_item_index(VIEW_TRANSFORM_GIZMO);
  3485. if (view_menu->get_popup()->is_item_checked(idx) != transform_gizmo) {
  3486. _menu_option(VIEW_TRANSFORM_GIZMO);
  3487. }
  3488. }
  3489. if (p_state.has("grid")) {
  3490. bool grid = p_state["grid"];
  3491. int idx = view_menu->get_popup()->get_item_index(VIEW_GRID);
  3492. if (view_menu->get_popup()->is_item_checked(idx) != grid) {
  3493. _menu_option(VIEW_GRID);
  3494. }
  3495. }
  3496. if (p_state.has("information")) {
  3497. bool information = p_state["information"];
  3498. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  3499. if (view_menu->get_popup()->is_item_checked(idx) != information) {
  3500. _menu_option(VIEW_INFORMATION);
  3501. }
  3502. }
  3503. if (p_state.has("frame_time")) {
  3504. bool fps = p_state["frame_time"];
  3505. int idx = view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME);
  3506. if (view_menu->get_popup()->is_item_checked(idx) != fps) {
  3507. _menu_option(VIEW_FRAME_TIME);
  3508. }
  3509. }
  3510. if (p_state.has("half_res")) {
  3511. bool half_res = p_state["half_res"];
  3512. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  3513. view_menu->get_popup()->set_item_checked(idx, half_res);
  3514. _update_shrink();
  3515. }
  3516. if (p_state.has("cinematic_preview")) {
  3517. previewing_cinema = p_state["cinematic_preview"];
  3518. int idx = view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW);
  3519. view_menu->get_popup()->set_item_checked(idx, previewing_cinema);
  3520. }
  3521. if (preview_camera->is_connected(SceneStringName(toggled), callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview))) {
  3522. preview_camera->disconnect(SceneStringName(toggled), callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview));
  3523. }
  3524. if (p_state.has("previewing")) {
  3525. Node *pv = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["previewing"]);
  3526. if (Object::cast_to<Camera3D>(pv)) {
  3527. previewing = Object::cast_to<Camera3D>(pv);
  3528. previewing->connect(SceneStringName(tree_exiting), callable_mp(this, &Node3DEditorViewport::_preview_exited_scene));
  3529. RS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), previewing->get_camera()); //replace
  3530. surface->queue_redraw();
  3531. preview_camera->set_pressed(true);
  3532. preview_camera->show();
  3533. }
  3534. }
  3535. preview_camera->connect(SceneStringName(toggled), callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview));
  3536. }
  3537. Dictionary Node3DEditorViewport::get_state() const {
  3538. Dictionary d;
  3539. d["position"] = cursor.pos;
  3540. d["x_rotation"] = cursor.x_rot;
  3541. d["y_rotation"] = cursor.y_rot;
  3542. d["distance"] = cursor.distance;
  3543. d["use_environment"] = camera->get_environment().is_valid();
  3544. d["orthogonal"] = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL;
  3545. d["view_type"] = view_type;
  3546. d["auto_orthogonal"] = auto_orthogonal;
  3547. d["auto_orthogonal_enabled"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL));
  3548. // Find selected display mode.
  3549. int display_mode = VIEW_DISPLAY_NORMAL;
  3550. for (int i = VIEW_DISPLAY_NORMAL; i < VIEW_DISPLAY_ADVANCED; i++) {
  3551. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(i))) {
  3552. display_mode = i;
  3553. break;
  3554. }
  3555. }
  3556. for (int i = VIEW_DISPLAY_ADVANCED + 1; i < VIEW_DISPLAY_MAX; i++) {
  3557. if (display_submenu->is_item_checked(display_submenu->get_item_index(i))) {
  3558. display_mode = i;
  3559. break;
  3560. }
  3561. }
  3562. d["display_mode"] = display_mode;
  3563. d["listener"] = viewport->is_audio_listener_3d();
  3564. d["doppler"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER));
  3565. d["gizmos"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS));
  3566. d["transform_gizmo"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_TRANSFORM_GIZMO));
  3567. d["grid"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_GRID));
  3568. d["information"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  3569. d["frame_time"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FRAME_TIME));
  3570. d["half_res"] = subviewport_container->get_stretch_shrink() > 1;
  3571. d["cinematic_preview"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_CINEMATIC_PREVIEW));
  3572. if (previewing) {
  3573. d["previewing"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(previewing);
  3574. }
  3575. d["lock_rotation"] = lock_rotation;
  3576. return d;
  3577. }
  3578. void Node3DEditorViewport::_bind_methods() {
  3579. ADD_SIGNAL(MethodInfo("toggle_maximize_view", PropertyInfo(Variant::OBJECT, "viewport")));
  3580. ADD_SIGNAL(MethodInfo("clicked"));
  3581. }
  3582. void Node3DEditorViewport::reset() {
  3583. orthogonal = false;
  3584. auto_orthogonal = false;
  3585. lock_rotation = false;
  3586. message_time = 0;
  3587. message = "";
  3588. last_message = "";
  3589. view_type = VIEW_TYPE_USER;
  3590. cursor = Cursor();
  3591. _update_name();
  3592. }
  3593. void Node3DEditorViewport::focus_selection() {
  3594. Vector3 center;
  3595. int count = 0;
  3596. const List<Node *> &selection = editor_selection->get_selected_node_list();
  3597. for (Node *E : selection) {
  3598. Node3D *sp = Object::cast_to<Node3D>(E);
  3599. if (!sp) {
  3600. continue;
  3601. }
  3602. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  3603. if (!se) {
  3604. continue;
  3605. }
  3606. if (se->gizmo.is_valid()) {
  3607. for (const KeyValue<int, Transform3D> &GE : se->subgizmos) {
  3608. center += se->gizmo->get_subgizmo_transform(GE.key).origin;
  3609. count++;
  3610. }
  3611. }
  3612. center += sp->get_global_gizmo_transform().origin;
  3613. count++;
  3614. }
  3615. if (count != 0) {
  3616. center /= count;
  3617. }
  3618. cursor.pos = center;
  3619. }
  3620. void Node3DEditorViewport::assign_pending_data_pointers(Node3D *p_preview_node, AABB *p_preview_bounds, AcceptDialog *p_accept) {
  3621. preview_node = p_preview_node;
  3622. preview_bounds = p_preview_bounds;
  3623. accept = p_accept;
  3624. }
  3625. void _insert_rid_recursive(Node *node, HashSet<RID> &rids) {
  3626. CollisionObject3D *co = Object::cast_to<CollisionObject3D>(node);
  3627. if (co) {
  3628. rids.insert(co->get_rid());
  3629. }
  3630. for (int i = 0; i < node->get_child_count(); i++) {
  3631. Node *child = node->get_child(i);
  3632. _insert_rid_recursive(child, rids);
  3633. }
  3634. }
  3635. Vector3 Node3DEditorViewport::_get_instance_position(const Point2 &p_pos, Node3D *p_node) const {
  3636. const float MAX_DISTANCE = 50.0;
  3637. const float FALLBACK_DISTANCE = 5.0;
  3638. Vector3 world_ray = get_ray(p_pos);
  3639. Vector3 world_pos = get_ray_pos(p_pos);
  3640. PhysicsDirectSpaceState3D *ss = get_tree()->get_root()->get_world_3d()->get_direct_space_state();
  3641. HashSet<RID> rids;
  3642. if (!preview_node->is_inside_tree()) {
  3643. List<Node *> &selection = editor_selection->get_selected_node_list();
  3644. Node3D *first_selected_node = Object::cast_to<Node3D>(selection.front()->get());
  3645. Array children = first_selected_node->get_children();
  3646. if (first_selected_node) {
  3647. _insert_rid_recursive(first_selected_node, rids);
  3648. }
  3649. }
  3650. PhysicsDirectSpaceState3D::RayParameters ray_params;
  3651. ray_params.exclude = rids;
  3652. ray_params.from = world_pos;
  3653. ray_params.to = world_pos + world_ray * camera->get_far();
  3654. PhysicsDirectSpaceState3D::RayResult result;
  3655. if (ss->intersect_ray(ray_params, result) && (preview_node->get_child_count() > 0 || !preview_node->is_inside_tree())) {
  3656. // Calculate an offset for the `p_node` such that the its bounding box is on top of and touching the contact surface's plane.
  3657. // Use the Gram-Schmidt process to get an orthonormal Basis aligned with the surface normal.
  3658. const Vector3 bb_basis_x = result.normal;
  3659. Vector3 bb_basis_y = Vector3(0, 1, 0);
  3660. bb_basis_y = bb_basis_y - bb_basis_y.project(bb_basis_x);
  3661. if (bb_basis_y.is_zero_approx()) {
  3662. bb_basis_y = Vector3(0, 0, 1);
  3663. bb_basis_y = bb_basis_y - bb_basis_y.project(bb_basis_x);
  3664. }
  3665. bb_basis_y = bb_basis_y.normalized();
  3666. const Vector3 bb_basis_z = bb_basis_x.cross(bb_basis_y);
  3667. const Basis bb_basis = Basis(bb_basis_x, bb_basis_y, bb_basis_z);
  3668. // This normal-aligned Basis allows us to create an AABB that can fit on the surface plane as snugly as possible.
  3669. const Transform3D bb_transform = Transform3D(bb_basis, p_node->get_transform().origin);
  3670. const AABB p_node_bb = _calculate_spatial_bounds(p_node, true, &bb_transform);
  3671. // The x-axis's alignment with the surface normal also makes it trivial to get the distance from `p_node`'s origin at (0, 0, 0) to the correct AABB face.
  3672. const float offset_distance = -p_node_bb.position.x;
  3673. // `result_offset` is in global space.
  3674. const Vector3 result_offset = result.position + result.normal * offset_distance;
  3675. return result_offset;
  3676. }
  3677. const bool is_orthogonal = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL;
  3678. // The XZ plane.
  3679. Vector3 intersection;
  3680. Plane plane(Vector3(0, 1, 0));
  3681. if (plane.intersects_ray(world_pos, world_ray, &intersection)) {
  3682. if (is_orthogonal || world_pos.distance_to(intersection) <= MAX_DISTANCE) {
  3683. return intersection;
  3684. }
  3685. }
  3686. // Plane facing the camera using fallback distance.
  3687. if (is_orthogonal) {
  3688. plane = Plane(world_ray, cursor.pos - world_ray * (cursor.distance - FALLBACK_DISTANCE));
  3689. } else {
  3690. plane = Plane(world_ray, world_pos + world_ray * FALLBACK_DISTANCE);
  3691. }
  3692. if (plane.intersects_ray(world_pos, world_ray, &intersection)) {
  3693. return intersection;
  3694. }
  3695. // Not likely, but just in case...
  3696. return world_pos + world_ray * FALLBACK_DISTANCE;
  3697. }
  3698. AABB Node3DEditorViewport::_calculate_spatial_bounds(const Node3D *p_parent, bool p_omit_top_level, const Transform3D *p_bounds_orientation) {
  3699. AABB bounds;
  3700. Transform3D bounds_orientation;
  3701. if (p_bounds_orientation) {
  3702. bounds_orientation = *p_bounds_orientation;
  3703. } else {
  3704. bounds_orientation = p_parent->get_global_transform();
  3705. }
  3706. if (!p_parent) {
  3707. return AABB(Vector3(-0.2, -0.2, -0.2), Vector3(0.4, 0.4, 0.4));
  3708. }
  3709. const Transform3D xform_to_top_level_parent_space = bounds_orientation.affine_inverse() * p_parent->get_global_transform();
  3710. const VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(p_parent);
  3711. if (visual_instance) {
  3712. bounds = visual_instance->get_aabb();
  3713. } else {
  3714. bounds = AABB();
  3715. }
  3716. bounds = xform_to_top_level_parent_space.xform(bounds);
  3717. for (int i = 0; i < p_parent->get_child_count(); i++) {
  3718. const Node3D *child = Object::cast_to<Node3D>(p_parent->get_child(i));
  3719. if (child && !(p_omit_top_level && child->is_set_as_top_level())) {
  3720. const AABB child_bounds = _calculate_spatial_bounds(child, p_omit_top_level, &bounds_orientation);
  3721. bounds.merge_with(child_bounds);
  3722. }
  3723. }
  3724. return bounds;
  3725. }
  3726. Node *Node3DEditorViewport::_sanitize_preview_node(Node *p_node) const {
  3727. Node3D *node_3d = Object::cast_to<Node3D>(p_node);
  3728. if (node_3d == nullptr) {
  3729. Node3D *replacement_node = memnew(Node3D);
  3730. replacement_node->set_name(p_node->get_name());
  3731. p_node->replace_by(replacement_node);
  3732. memdelete(p_node);
  3733. p_node = replacement_node;
  3734. } else {
  3735. VisualInstance3D *visual_instance = Object::cast_to<VisualInstance3D>(node_3d);
  3736. if (visual_instance == nullptr) {
  3737. Node3D *replacement_node = memnew(Node3D);
  3738. replacement_node->set_name(node_3d->get_name());
  3739. replacement_node->set_visible(node_3d->is_visible());
  3740. replacement_node->set_transform(node_3d->get_transform());
  3741. replacement_node->set_rotation_edit_mode(node_3d->get_rotation_edit_mode());
  3742. replacement_node->set_rotation_order(node_3d->get_rotation_order());
  3743. replacement_node->set_as_top_level(node_3d->is_set_as_top_level());
  3744. p_node->replace_by(replacement_node);
  3745. memdelete(p_node);
  3746. p_node = replacement_node;
  3747. }
  3748. }
  3749. for (int i = 0; i < p_node->get_child_count(); i++) {
  3750. _sanitize_preview_node(p_node->get_child(i));
  3751. }
  3752. return p_node;
  3753. }
  3754. void Node3DEditorViewport::_create_preview_node(const Vector<String> &files) const {
  3755. bool add_preview = false;
  3756. for (const String &path : files) {
  3757. Ref<Resource> res = ResourceLoader::load(path);
  3758. ERR_CONTINUE(res.is_null());
  3759. Ref<PackedScene> scene = res;
  3760. if (scene.is_valid()) {
  3761. Node *instance = scene->instantiate();
  3762. if (instance) {
  3763. instance = _sanitize_preview_node(instance);
  3764. preview_node->add_child(instance);
  3765. Node3D *node_3d = Object::cast_to<Node3D>(instance);
  3766. if (node_3d) {
  3767. node_3d->set_as_top_level(false);
  3768. }
  3769. }
  3770. add_preview = true;
  3771. }
  3772. Ref<Mesh> mesh = res;
  3773. if (mesh.is_valid()) {
  3774. MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
  3775. mesh_instance->set_mesh(mesh);
  3776. preview_node->add_child(mesh_instance);
  3777. add_preview = true;
  3778. }
  3779. Ref<AudioStream> audio = res;
  3780. if (audio.is_valid()) {
  3781. Sprite3D *sprite = memnew(Sprite3D);
  3782. sprite->set_texture(get_editor_theme_icon(SNAME("Gizmo3DSamplePlayer")));
  3783. sprite->set_billboard_mode(StandardMaterial3D::BILLBOARD_ENABLED);
  3784. sprite->set_pixel_size(0.005);
  3785. preview_node->add_child(sprite);
  3786. add_preview = true;
  3787. }
  3788. }
  3789. if (add_preview) {
  3790. EditorNode::get_singleton()->get_scene_root()->add_child(preview_node);
  3791. }
  3792. *preview_bounds = _calculate_spatial_bounds(preview_node);
  3793. }
  3794. void Node3DEditorViewport::_remove_preview_node() {
  3795. set_message("");
  3796. if (preview_node->get_parent()) {
  3797. for (int i = preview_node->get_child_count() - 1; i >= 0; i--) {
  3798. Node *node = preview_node->get_child(i);
  3799. node->queue_free();
  3800. preview_node->remove_child(node);
  3801. }
  3802. EditorNode::get_singleton()->get_scene_root()->remove_child(preview_node);
  3803. }
  3804. }
  3805. bool Node3DEditorViewport::_apply_preview_material(ObjectID p_target, const Point2 &p_point) const {
  3806. _reset_preview_material();
  3807. if (p_target.is_null()) {
  3808. return false;
  3809. }
  3810. spatial_editor->set_preview_material_target(p_target);
  3811. Object *target_inst = ObjectDB::get_instance(p_target);
  3812. bool is_ctrl = Input::get_singleton()->is_key_pressed(Key::CTRL);
  3813. MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(target_inst);
  3814. if (is_ctrl && mesh_instance) {
  3815. Ref<Mesh> mesh = mesh_instance->get_mesh();
  3816. int surface_count = mesh->get_surface_count();
  3817. Vector3 world_ray = get_ray(p_point);
  3818. Vector3 world_pos = get_ray_pos(p_point);
  3819. int closest_surface = -1;
  3820. float closest_dist = 1e20;
  3821. Transform3D gt = mesh_instance->get_global_transform();
  3822. Transform3D ai = gt.affine_inverse();
  3823. Vector3 xform_ray = ai.basis.xform(world_ray).normalized();
  3824. Vector3 xform_pos = ai.xform(world_pos);
  3825. for (int surface_idx = 0; surface_idx < surface_count; surface_idx++) {
  3826. Ref<TriangleMesh> surface_mesh = mesh->generate_surface_triangle_mesh(surface_idx);
  3827. Vector3 rpos, rnorm;
  3828. if (surface_mesh->intersect_ray(xform_pos, xform_ray, rpos, rnorm)) {
  3829. Vector3 hitpos = gt.xform(rpos);
  3830. const real_t dist = world_pos.distance_to(hitpos);
  3831. if (dist < 0) {
  3832. continue;
  3833. }
  3834. if (dist < closest_dist) {
  3835. closest_surface = surface_idx;
  3836. closest_dist = dist;
  3837. }
  3838. }
  3839. }
  3840. if (closest_surface == -1) {
  3841. return false;
  3842. }
  3843. spatial_editor->set_preview_material_surface(closest_surface);
  3844. spatial_editor->set_preview_reset_material(mesh_instance->get_surface_override_material(closest_surface));
  3845. mesh_instance->set_surface_override_material(closest_surface, spatial_editor->get_preview_material());
  3846. return true;
  3847. }
  3848. GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(target_inst);
  3849. if (geometry_instance) {
  3850. spatial_editor->set_preview_material_surface(-1);
  3851. spatial_editor->set_preview_reset_material(geometry_instance->get_material_override());
  3852. geometry_instance->set_material_override(spatial_editor->get_preview_material());
  3853. return true;
  3854. }
  3855. return false;
  3856. }
  3857. void Node3DEditorViewport::_reset_preview_material() const {
  3858. ObjectID last_target = spatial_editor->get_preview_material_target();
  3859. if (last_target.is_null()) {
  3860. return;
  3861. }
  3862. Object *last_target_inst = ObjectDB::get_instance(last_target);
  3863. MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(last_target_inst);
  3864. GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(last_target_inst);
  3865. if (mesh_instance && spatial_editor->get_preview_material_surface() != -1) {
  3866. mesh_instance->set_surface_override_material(spatial_editor->get_preview_material_surface(), spatial_editor->get_preview_reset_material());
  3867. } else if (geometry_instance) {
  3868. geometry_instance->set_material_override(spatial_editor->get_preview_reset_material());
  3869. }
  3870. }
  3871. void Node3DEditorViewport::_remove_preview_material() {
  3872. preview_material_label->hide();
  3873. preview_material_label_desc->hide();
  3874. spatial_editor->set_preview_material(Ref<Material>());
  3875. spatial_editor->set_preview_reset_material(Ref<Material>());
  3876. spatial_editor->set_preview_material_target(ObjectID());
  3877. spatial_editor->set_preview_material_surface(-1);
  3878. }
  3879. bool Node3DEditorViewport::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) const {
  3880. if (p_desired_node->get_scene_file_path() == p_target_scene_path) {
  3881. return true;
  3882. }
  3883. int childCount = p_desired_node->get_child_count();
  3884. for (int i = 0; i < childCount; i++) {
  3885. Node *child = p_desired_node->get_child(i);
  3886. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  3887. return true;
  3888. }
  3889. }
  3890. return false;
  3891. }
  3892. bool Node3DEditorViewport::_create_instance(Node *p_parent, const String &p_path, const Point2 &p_point) {
  3893. Ref<Resource> res = ResourceLoader::load(p_path);
  3894. ERR_FAIL_COND_V(res.is_null(), false);
  3895. Ref<PackedScene> scene = res;
  3896. Ref<Mesh> mesh = res;
  3897. Node *instantiated_scene = nullptr;
  3898. if (mesh.is_valid() || scene.is_valid()) {
  3899. if (mesh.is_valid()) {
  3900. MeshInstance3D *mesh_instance = memnew(MeshInstance3D);
  3901. mesh_instance->set_mesh(mesh);
  3902. // Adjust casing according to project setting. The file name is expected to be in snake_case, but will work for others.
  3903. const String &node_name = Node::adjust_name_casing(p_path.get_file().get_basename());
  3904. if (!node_name.is_empty()) {
  3905. mesh_instance->set_name(node_name);
  3906. }
  3907. instantiated_scene = mesh_instance;
  3908. } else {
  3909. if (!scene.is_valid()) { // invalid scene
  3910. return false;
  3911. } else {
  3912. instantiated_scene = scene->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
  3913. }
  3914. }
  3915. }
  3916. if (instantiated_scene == nullptr) {
  3917. return false;
  3918. }
  3919. if (!EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path().is_empty()) { // Cyclic instantiation.
  3920. if (_cyclical_dependency_exists(EditorNode::get_singleton()->get_edited_scene()->get_scene_file_path(), instantiated_scene)) {
  3921. memdelete(instantiated_scene);
  3922. return false;
  3923. }
  3924. }
  3925. if (scene.is_valid()) {
  3926. instantiated_scene->set_scene_file_path(ProjectSettings::get_singleton()->localize_path(p_path));
  3927. }
  3928. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3929. undo_redo->add_do_method(p_parent, "add_child", instantiated_scene, true);
  3930. undo_redo->add_do_method(instantiated_scene, "set_owner", EditorNode::get_singleton()->get_edited_scene());
  3931. undo_redo->add_do_reference(instantiated_scene);
  3932. undo_redo->add_undo_method(p_parent, "remove_child", instantiated_scene);
  3933. undo_redo->add_do_method(editor_selection, "add_node", instantiated_scene);
  3934. String new_name = p_parent->validate_child_name(instantiated_scene);
  3935. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  3936. undo_redo->add_do_method(ed, "live_debug_instantiate_node", EditorNode::get_singleton()->get_edited_scene()->get_path_to(p_parent), p_path, new_name);
  3937. undo_redo->add_undo_method(ed, "live_debug_remove_node", NodePath(String(EditorNode::get_singleton()->get_edited_scene()->get_path_to(p_parent)) + "/" + new_name));
  3938. Node3D *node3d = Object::cast_to<Node3D>(instantiated_scene);
  3939. if (node3d) {
  3940. Transform3D parent_tf;
  3941. Node3D *parent_node3d = Object::cast_to<Node3D>(p_parent);
  3942. if (parent_node3d) {
  3943. parent_tf = parent_node3d->get_global_gizmo_transform();
  3944. }
  3945. Transform3D new_tf = node3d->get_transform();
  3946. if (node3d->is_set_as_top_level()) {
  3947. new_tf.origin += preview_node_pos;
  3948. } else {
  3949. new_tf.origin = parent_tf.affine_inverse().xform(preview_node_pos + node3d->get_position());
  3950. new_tf.basis = parent_tf.affine_inverse().basis * new_tf.basis;
  3951. }
  3952. undo_redo->add_do_method(instantiated_scene, "set_transform", new_tf);
  3953. }
  3954. return true;
  3955. }
  3956. bool Node3DEditorViewport::_create_audio_node(Node *p_parent, const String &p_path, const Point2 &p_point) {
  3957. Ref<AudioStream> audio = ResourceLoader::load(p_path);
  3958. ERR_FAIL_COND_V(audio.is_null(), false);
  3959. AudioStreamPlayer3D *audio_player = memnew(AudioStreamPlayer3D);
  3960. audio_player->set_stream(audio);
  3961. // Adjust casing according to project setting. The file name is expected to be in snake_case, but will work for others.
  3962. const String &node_name = Node::adjust_name_casing(p_path.get_file().get_basename());
  3963. if (!node_name.is_empty()) {
  3964. audio_player->set_name(node_name);
  3965. }
  3966. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3967. undo_redo->add_do_method(p_parent, "add_child", audio_player, true);
  3968. undo_redo->add_do_method(audio_player, "set_owner", EditorNode::get_singleton()->get_edited_scene());
  3969. undo_redo->add_do_reference(audio_player);
  3970. undo_redo->add_undo_method(p_parent, "remove_child", audio_player);
  3971. undo_redo->add_do_method(editor_selection, "add_node", audio_player);
  3972. const String new_name = p_parent->validate_child_name(audio_player);
  3973. EditorDebuggerNode *ed = EditorDebuggerNode::get_singleton();
  3974. undo_redo->add_do_method(ed, "live_debug_create_node", EditorNode::get_singleton()->get_edited_scene()->get_path_to(p_parent), audio_player->get_class(), new_name);
  3975. undo_redo->add_undo_method(ed, "live_debug_remove_node", NodePath(String(EditorNode::get_singleton()->get_edited_scene()->get_path_to(p_parent)) + "/" + new_name));
  3976. Transform3D parent_tf;
  3977. Node3D *parent_node3d = Object::cast_to<Node3D>(p_parent);
  3978. if (parent_node3d) {
  3979. parent_tf = parent_node3d->get_global_gizmo_transform();
  3980. }
  3981. Transform3D new_tf = audio_player->get_transform();
  3982. new_tf.origin = parent_tf.affine_inverse().xform(preview_node_pos + audio_player->get_position());
  3983. new_tf.basis = parent_tf.affine_inverse().basis * new_tf.basis;
  3984. undo_redo->add_do_method(audio_player, "set_transform", new_tf);
  3985. return true;
  3986. }
  3987. void Node3DEditorViewport::_perform_drop_data() {
  3988. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  3989. if (spatial_editor->get_preview_material_target().is_valid()) {
  3990. GeometryInstance3D *geometry_instance = Object::cast_to<GeometryInstance3D>(ObjectDB::get_instance(spatial_editor->get_preview_material_target()));
  3991. MeshInstance3D *mesh_instance = Object::cast_to<MeshInstance3D>(ObjectDB::get_instance(spatial_editor->get_preview_material_target()));
  3992. if (mesh_instance && spatial_editor->get_preview_material_surface() != -1) {
  3993. undo_redo->create_action(vformat(TTR("Set Surface %d Override Material"), spatial_editor->get_preview_material_surface()));
  3994. undo_redo->add_do_method(geometry_instance, "set_surface_override_material", spatial_editor->get_preview_material_surface(), spatial_editor->get_preview_material());
  3995. undo_redo->add_undo_method(geometry_instance, "set_surface_override_material", spatial_editor->get_preview_material_surface(), spatial_editor->get_preview_reset_material());
  3996. undo_redo->commit_action();
  3997. } else if (geometry_instance) {
  3998. undo_redo->create_action(TTR("Set Material Override"));
  3999. undo_redo->add_do_method(geometry_instance, "set_material_override", spatial_editor->get_preview_material());
  4000. undo_redo->add_undo_method(geometry_instance, "set_material_override", spatial_editor->get_preview_reset_material());
  4001. undo_redo->commit_action();
  4002. }
  4003. _remove_preview_material();
  4004. return;
  4005. }
  4006. _remove_preview_node();
  4007. PackedStringArray error_files;
  4008. undo_redo->create_action(TTR("Create Node"), UndoRedo::MERGE_DISABLE, target_node);
  4009. undo_redo->add_do_method(editor_selection, "clear");
  4010. for (int i = 0; i < selected_files.size(); i++) {
  4011. String path = selected_files[i];
  4012. Ref<Resource> res = ResourceLoader::load(path);
  4013. if (res.is_null()) {
  4014. continue;
  4015. }
  4016. Ref<PackedScene> scene = res;
  4017. Ref<Mesh> mesh = res;
  4018. if (mesh.is_valid() || scene.is_valid()) {
  4019. if (!_create_instance(target_node, path, drop_pos)) {
  4020. error_files.push_back(path.get_file());
  4021. }
  4022. }
  4023. Ref<AudioStream> audio = res;
  4024. if (audio.is_valid()) {
  4025. if (!_create_audio_node(target_node, path, drop_pos)) {
  4026. error_files.push_back(path.get_file());
  4027. }
  4028. }
  4029. }
  4030. undo_redo->commit_action();
  4031. if (error_files.size() > 0) {
  4032. accept->set_text(vformat(TTR("Error instantiating scene from %s."), String(", ").join(error_files)));
  4033. accept->popup_centered();
  4034. }
  4035. }
  4036. bool Node3DEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  4037. preview_node_viewport_pos = p_point;
  4038. bool can_instantiate = false;
  4039. bool is_cyclical_dep = false;
  4040. String error_file;
  4041. if (!preview_node->is_inside_tree() && spatial_editor->get_preview_material().is_null()) {
  4042. Dictionary d = p_data;
  4043. if (d.has("type") && (String(d["type"]) == "files")) {
  4044. Vector<String> files = d["files"];
  4045. // Track whether a type other than PackedScene is valid to stop checking them and only
  4046. // continue to check if the rest of the scenes are valid (don't have cyclic dependencies).
  4047. bool is_other_valid = false;
  4048. // Check if at least one of the dragged files is a mesh, material, texture or scene.
  4049. for (int i = 0; i < files.size(); i++) {
  4050. const String &res_type = ResourceLoader::get_resource_type(files[i]);
  4051. bool is_scene = ClassDB::is_parent_class(res_type, "PackedScene");
  4052. bool is_mesh = ClassDB::is_parent_class(res_type, "Mesh");
  4053. bool is_material = ClassDB::is_parent_class(res_type, "Material");
  4054. bool is_texture = ClassDB::is_parent_class(res_type, "Texture");
  4055. bool is_audio = ClassDB::is_parent_class(res_type, "AudioStream");
  4056. if (is_mesh || is_scene || is_material || is_texture || is_audio) {
  4057. Ref<Resource> res = ResourceLoader::load(files[i]);
  4058. if (res.is_null()) {
  4059. continue;
  4060. }
  4061. Ref<PackedScene> scn = res;
  4062. Ref<Mesh> mesh = res;
  4063. Ref<Material> mat = res;
  4064. Ref<Texture2D> tex = res;
  4065. Ref<AudioStream> audio = res;
  4066. if (scn.is_valid()) {
  4067. Node *instantiated_scene = scn->instantiate(PackedScene::GEN_EDIT_STATE_INSTANCE);
  4068. if (!instantiated_scene) {
  4069. continue;
  4070. }
  4071. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  4072. if (edited_scene && !edited_scene->get_scene_file_path().is_empty() && _cyclical_dependency_exists(edited_scene->get_scene_file_path(), instantiated_scene)) {
  4073. memdelete(instantiated_scene);
  4074. can_instantiate = false;
  4075. is_cyclical_dep = true;
  4076. error_file = files[i].get_file();
  4077. break;
  4078. }
  4079. memdelete(instantiated_scene);
  4080. } else if (!is_other_valid && mat.is_valid()) {
  4081. Ref<BaseMaterial3D> base_mat = res;
  4082. Ref<ShaderMaterial> shader_mat = res;
  4083. if (base_mat.is_null() && shader_mat.is_null()) {
  4084. continue;
  4085. }
  4086. spatial_editor->set_preview_material(mat);
  4087. is_other_valid = true;
  4088. continue;
  4089. } else if (!is_other_valid && mesh.is_valid()) {
  4090. // Let the mesh pass.
  4091. is_other_valid = true;
  4092. } else if (!is_other_valid && tex.is_valid()) {
  4093. Ref<StandardMaterial3D> new_mat = memnew(StandardMaterial3D);
  4094. new_mat->set_texture(BaseMaterial3D::TEXTURE_ALBEDO, tex);
  4095. spatial_editor->set_preview_material(new_mat);
  4096. is_other_valid = true;
  4097. continue;
  4098. } else if (!is_other_valid && audio.is_valid()) {
  4099. is_other_valid = true;
  4100. } else {
  4101. continue;
  4102. }
  4103. can_instantiate = true;
  4104. }
  4105. }
  4106. if (can_instantiate) {
  4107. _create_preview_node(files);
  4108. preview_node->hide();
  4109. }
  4110. }
  4111. } else {
  4112. if (preview_node->is_inside_tree()) {
  4113. can_instantiate = true;
  4114. }
  4115. }
  4116. if (is_cyclical_dep) {
  4117. set_message(vformat(TTR("Can't instantiate: %s."), vformat(TTR("Circular dependency found at %s"), error_file)));
  4118. return false;
  4119. }
  4120. if (can_instantiate) {
  4121. update_preview_node = true;
  4122. return true;
  4123. }
  4124. if (spatial_editor->get_preview_material().is_valid()) {
  4125. preview_material_label->show();
  4126. preview_material_label_desc->show();
  4127. ObjectID new_preview_material_target = _select_ray(p_point);
  4128. return _apply_preview_material(new_preview_material_target, p_point);
  4129. }
  4130. return false;
  4131. }
  4132. void Node3DEditorViewport::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  4133. if (!can_drop_data_fw(p_point, p_data, p_from)) {
  4134. return;
  4135. }
  4136. bool is_shift = Input::get_singleton()->is_key_pressed(Key::SHIFT);
  4137. bool is_alt = Input::get_singleton()->is_key_pressed(Key::ALT);
  4138. selected_files.clear();
  4139. Dictionary d = p_data;
  4140. if (d.has("type") && String(d["type"]) == "files") {
  4141. selected_files = d["files"];
  4142. }
  4143. List<Node *> selected_nodes = EditorNode::get_singleton()->get_editor_selection()->get_selected_node_list();
  4144. Node *root_node = EditorNode::get_singleton()->get_edited_scene();
  4145. if (selected_nodes.size() > 0) {
  4146. Node *selected_node = selected_nodes.front()->get();
  4147. if (is_alt) {
  4148. target_node = root_node;
  4149. } else if (is_shift) {
  4150. target_node = selected_node;
  4151. } else { // Default behavior.
  4152. target_node = (selected_node != root_node) ? selected_node->get_parent() : root_node;
  4153. }
  4154. } else {
  4155. if (root_node) {
  4156. target_node = root_node;
  4157. } else {
  4158. // Create a root node so we can add child nodes to it.
  4159. SceneTreeDock::get_singleton()->add_root_node(memnew(Node3D));
  4160. target_node = get_tree()->get_edited_scene_root();
  4161. }
  4162. }
  4163. drop_pos = p_point;
  4164. _perform_drop_data();
  4165. }
  4166. void Node3DEditorViewport::begin_transform(TransformMode p_mode, bool instant) {
  4167. if (get_selected_count() > 0) {
  4168. _edit.mode = p_mode;
  4169. _compute_edit(_edit.mouse_pos);
  4170. _edit.instant = instant;
  4171. _edit.snap = spatial_editor->is_snap_enabled();
  4172. update_transform_gizmo_view();
  4173. set_process_input(instant);
  4174. }
  4175. }
  4176. // Apply the current transform operation.
  4177. void Node3DEditorViewport::commit_transform() {
  4178. ERR_FAIL_COND(_edit.mode == TRANSFORM_NONE);
  4179. static const char *_transform_name[4] = {
  4180. TTRC("None"),
  4181. TTRC("Rotate"),
  4182. // TRANSLATORS: This refers to the movement that changes the position of an object.
  4183. TTRC("Translate"),
  4184. TTRC("Scale"),
  4185. };
  4186. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  4187. undo_redo->create_action(_transform_name[_edit.mode]);
  4188. List<Node *> &selection = editor_selection->get_selected_node_list();
  4189. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  4190. Node3D *sp = Object::cast_to<Node3D>(E->get());
  4191. if (!sp) {
  4192. continue;
  4193. }
  4194. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  4195. if (!se) {
  4196. continue;
  4197. }
  4198. undo_redo->add_do_method(sp, "set_transform", sp->get_local_gizmo_transform());
  4199. undo_redo->add_undo_method(sp, "set_transform", se->original_local);
  4200. }
  4201. undo_redo->commit_action();
  4202. collision_reposition = false;
  4203. finish_transform();
  4204. set_message("");
  4205. }
  4206. void Node3DEditorViewport::apply_transform(Vector3 p_motion, double p_snap) {
  4207. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  4208. List<Node *> &selection = editor_selection->get_selected_node_list();
  4209. for (Node *E : selection) {
  4210. Node3D *sp = Object::cast_to<Node3D>(E);
  4211. if (!sp) {
  4212. continue;
  4213. }
  4214. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  4215. if (!se) {
  4216. continue;
  4217. }
  4218. if (sp->has_meta("_edit_lock_")) {
  4219. continue;
  4220. }
  4221. if (se->gizmo.is_valid()) {
  4222. for (KeyValue<int, Transform3D> &GE : se->subgizmos) {
  4223. Transform3D xform = GE.value;
  4224. Transform3D new_xform = _compute_transform(_edit.mode, se->original * xform, xform, p_motion, p_snap, local_coords, _edit.plane != TRANSFORM_VIEW); // Force orthogonal with subgizmo.
  4225. if (!local_coords) {
  4226. new_xform = se->original.affine_inverse() * new_xform;
  4227. }
  4228. se->gizmo->set_subgizmo_transform(GE.key, new_xform);
  4229. }
  4230. } else {
  4231. Transform3D new_xform = _compute_transform(_edit.mode, se->original, se->original_local, p_motion, p_snap, local_coords, sp->get_rotation_edit_mode() != Node3D::ROTATION_EDIT_MODE_BASIS && _edit.plane != TRANSFORM_VIEW);
  4232. _transform_gizmo_apply(se->sp, new_xform, local_coords);
  4233. }
  4234. }
  4235. spatial_editor->update_transform_gizmo();
  4236. surface->queue_redraw();
  4237. }
  4238. // Update the current transform operation in response to an input.
  4239. void Node3DEditorViewport::update_transform(bool p_shift) {
  4240. Vector3 ray_pos = get_ray_pos(_edit.mouse_pos);
  4241. Vector3 ray = get_ray(_edit.mouse_pos);
  4242. double snap = EDITOR_GET("interface/inspector/default_float_step");
  4243. int snap_step_decimals = Math::range_step_decimals(snap);
  4244. switch (_edit.mode) {
  4245. case TRANSFORM_SCALE: {
  4246. Vector3 motion_mask;
  4247. Plane plane;
  4248. bool plane_mv = false;
  4249. switch (_edit.plane) {
  4250. case TRANSFORM_VIEW:
  4251. motion_mask = Vector3(0, 0, 0);
  4252. plane = Plane(_get_camera_normal(), _edit.center);
  4253. break;
  4254. case TRANSFORM_X_AXIS:
  4255. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(0).normalized();
  4256. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  4257. break;
  4258. case TRANSFORM_Y_AXIS:
  4259. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(1).normalized();
  4260. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  4261. break;
  4262. case TRANSFORM_Z_AXIS:
  4263. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized();
  4264. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  4265. break;
  4266. case TRANSFORM_YZ:
  4267. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized() + spatial_editor->get_gizmo_transform().basis.get_column(1).normalized();
  4268. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(0).normalized(), _edit.center);
  4269. plane_mv = true;
  4270. break;
  4271. case TRANSFORM_XZ:
  4272. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized() + spatial_editor->get_gizmo_transform().basis.get_column(0).normalized();
  4273. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(1).normalized(), _edit.center);
  4274. plane_mv = true;
  4275. break;
  4276. case TRANSFORM_XY:
  4277. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(0).normalized() + spatial_editor->get_gizmo_transform().basis.get_column(1).normalized();
  4278. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(2).normalized(), _edit.center);
  4279. plane_mv = true;
  4280. break;
  4281. }
  4282. Vector3 intersection;
  4283. if (!plane.intersects_ray(ray_pos, ray, &intersection)) {
  4284. break;
  4285. }
  4286. Vector3 click;
  4287. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) {
  4288. break;
  4289. }
  4290. Vector3 motion = intersection - click;
  4291. if (_edit.plane != TRANSFORM_VIEW) {
  4292. if (!plane_mv) {
  4293. motion = motion_mask.dot(motion) * motion_mask;
  4294. } else {
  4295. // Alternative planar scaling mode
  4296. if (p_shift) {
  4297. motion = motion_mask.dot(motion) * motion_mask;
  4298. }
  4299. }
  4300. } else {
  4301. const real_t center_click_dist = click.distance_to(_edit.center);
  4302. const real_t center_inters_dist = intersection.distance_to(_edit.center);
  4303. if (center_click_dist == 0) {
  4304. break;
  4305. }
  4306. const real_t scale = center_inters_dist - center_click_dist;
  4307. motion = Vector3(scale, scale, scale);
  4308. }
  4309. motion /= click.distance_to(_edit.center);
  4310. // Disable local transformation for TRANSFORM_VIEW
  4311. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  4312. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  4313. snap = spatial_editor->get_scale_snap() / 100;
  4314. }
  4315. Vector3 motion_snapped = motion;
  4316. motion_snapped.snapf(snap);
  4317. // This might not be necessary anymore after issue #288 is solved (in 4.0?).
  4318. // TRANSLATORS: Refers to changing the scale of a node in the 3D editor.
  4319. set_message(TTR("Scaling:") + " (" + String::num(motion_snapped.x, snap_step_decimals) + ", " +
  4320. String::num(motion_snapped.y, snap_step_decimals) + ", " + String::num(motion_snapped.z, snap_step_decimals) + ")");
  4321. if (local_coords) {
  4322. // TODO: needed?
  4323. motion = _edit.original.basis.inverse().xform(motion);
  4324. }
  4325. apply_transform(motion, snap);
  4326. } break;
  4327. case TRANSFORM_TRANSLATE: {
  4328. Vector3 motion_mask;
  4329. Plane plane;
  4330. bool plane_mv = false;
  4331. switch (_edit.plane) {
  4332. case TRANSFORM_VIEW:
  4333. plane = Plane(_get_camera_normal(), _edit.center);
  4334. break;
  4335. case TRANSFORM_X_AXIS:
  4336. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(0).normalized();
  4337. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  4338. break;
  4339. case TRANSFORM_Y_AXIS:
  4340. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(1).normalized();
  4341. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  4342. break;
  4343. case TRANSFORM_Z_AXIS:
  4344. motion_mask = spatial_editor->get_gizmo_transform().basis.get_column(2).normalized();
  4345. plane = Plane(motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized(), _edit.center);
  4346. break;
  4347. case TRANSFORM_YZ:
  4348. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(0).normalized(), _edit.center);
  4349. plane_mv = true;
  4350. break;
  4351. case TRANSFORM_XZ:
  4352. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(1).normalized(), _edit.center);
  4353. plane_mv = true;
  4354. break;
  4355. case TRANSFORM_XY:
  4356. plane = Plane(spatial_editor->get_gizmo_transform().basis.get_column(2).normalized(), _edit.center);
  4357. plane_mv = true;
  4358. break;
  4359. }
  4360. Vector3 intersection;
  4361. if (!plane.intersects_ray(ray_pos, ray, &intersection)) {
  4362. break;
  4363. }
  4364. Vector3 click;
  4365. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) {
  4366. break;
  4367. }
  4368. Vector3 motion = intersection - click;
  4369. if (_edit.plane != TRANSFORM_VIEW) {
  4370. if (!plane_mv) {
  4371. motion = motion_mask.dot(motion) * motion_mask;
  4372. }
  4373. }
  4374. // Disable local transformation for TRANSFORM_VIEW
  4375. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW);
  4376. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  4377. snap = spatial_editor->get_translate_snap();
  4378. }
  4379. Vector3 motion_snapped = motion;
  4380. motion_snapped.snapf(snap);
  4381. // TRANSLATORS: Refers to changing the position of a node in the 3D editor.
  4382. set_message(TTR("Translating:") + " (" + String::num(motion_snapped.x, snap_step_decimals) + ", " +
  4383. String::num(motion_snapped.y, snap_step_decimals) + ", " + String::num(motion_snapped.z, snap_step_decimals) + ")");
  4384. if (local_coords) {
  4385. motion = spatial_editor->get_gizmo_transform().basis.inverse().xform(motion);
  4386. }
  4387. apply_transform(motion, snap);
  4388. } break;
  4389. case TRANSFORM_ROTATE: {
  4390. Plane plane;
  4391. if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {
  4392. Vector3 cam_to_obj = _edit.center - _get_camera_position();
  4393. if (!cam_to_obj.is_zero_approx()) {
  4394. plane = Plane(cam_to_obj.normalized(), _edit.center);
  4395. } else {
  4396. plane = Plane(_get_camera_normal(), _edit.center);
  4397. }
  4398. } else {
  4399. plane = Plane(_get_camera_normal(), _edit.center);
  4400. }
  4401. Vector3 local_axis;
  4402. Vector3 global_axis;
  4403. switch (_edit.plane) {
  4404. case TRANSFORM_VIEW:
  4405. // local_axis unused
  4406. global_axis = plane.normal;
  4407. break;
  4408. case TRANSFORM_X_AXIS:
  4409. local_axis = Vector3(1, 0, 0);
  4410. break;
  4411. case TRANSFORM_Y_AXIS:
  4412. local_axis = Vector3(0, 1, 0);
  4413. break;
  4414. case TRANSFORM_Z_AXIS:
  4415. local_axis = Vector3(0, 0, 1);
  4416. break;
  4417. case TRANSFORM_YZ:
  4418. case TRANSFORM_XZ:
  4419. case TRANSFORM_XY:
  4420. break;
  4421. }
  4422. if (_edit.plane != TRANSFORM_VIEW) {
  4423. global_axis = spatial_editor->get_gizmo_transform().basis.xform(local_axis).normalized();
  4424. }
  4425. Vector3 intersection;
  4426. if (!plane.intersects_ray(ray_pos, ray, &intersection)) {
  4427. break;
  4428. }
  4429. Vector3 click;
  4430. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click)) {
  4431. break;
  4432. }
  4433. static const float orthogonal_threshold = Math::cos(Math::deg_to_rad(85.0f));
  4434. bool axis_is_orthogonal = ABS(plane.normal.dot(global_axis)) < orthogonal_threshold;
  4435. double angle = 0.0f;
  4436. if (axis_is_orthogonal) {
  4437. _edit.show_rotation_line = false;
  4438. Vector3 projection_axis = plane.normal.cross(global_axis);
  4439. Vector3 delta = intersection - click;
  4440. float projection = delta.dot(projection_axis);
  4441. angle = (projection * (Math_PI / 2.0f)) / (gizmo_scale * GIZMO_CIRCLE_SIZE);
  4442. } else {
  4443. _edit.show_rotation_line = true;
  4444. Vector3 click_axis = (click - _edit.center).normalized();
  4445. Vector3 current_axis = (intersection - _edit.center).normalized();
  4446. angle = click_axis.signed_angle_to(current_axis, global_axis);
  4447. }
  4448. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  4449. snap = spatial_editor->get_rotate_snap();
  4450. }
  4451. angle = Math::snapped(Math::rad_to_deg(angle), snap);
  4452. set_message(vformat(TTR("Rotating %s degrees."), String::num(angle, snap_step_decimals)));
  4453. angle = Math::deg_to_rad(angle);
  4454. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); // Disable local transformation for TRANSFORM_VIEW
  4455. Vector3 compute_axis = local_coords ? local_axis : global_axis;
  4456. apply_transform(compute_axis, angle);
  4457. } break;
  4458. default: {
  4459. }
  4460. }
  4461. }
  4462. void Node3DEditorViewport::update_transform_numeric() {
  4463. Vector3 motion;
  4464. switch (_edit.plane) {
  4465. case TRANSFORM_VIEW: {
  4466. switch (_edit.mode) {
  4467. case TRANSFORM_TRANSLATE:
  4468. motion = Vector3(1, 0, 0);
  4469. break;
  4470. case TRANSFORM_ROTATE:
  4471. motion = spatial_editor->get_gizmo_transform().basis.xform_inv(_get_camera_normal()).normalized();
  4472. break;
  4473. case TRANSFORM_SCALE:
  4474. motion = Vector3(1, 1, 1);
  4475. break;
  4476. case TRANSFORM_NONE:
  4477. ERR_FAIL_MSG("_edit.mode cannot be TRANSFORM_NONE in update_transform_numeric.");
  4478. }
  4479. break;
  4480. }
  4481. case TRANSFORM_X_AXIS:
  4482. motion = Vector3(1, 0, 0);
  4483. break;
  4484. case TRANSFORM_Y_AXIS:
  4485. motion = Vector3(0, 1, 0);
  4486. break;
  4487. case TRANSFORM_Z_AXIS:
  4488. motion = Vector3(0, 0, 1);
  4489. break;
  4490. case TRANSFORM_XY:
  4491. motion = Vector3(1, 1, 0);
  4492. break;
  4493. case TRANSFORM_XZ:
  4494. motion = Vector3(1, 0, 1);
  4495. break;
  4496. case TRANSFORM_YZ:
  4497. motion = Vector3(0, 1, 1);
  4498. break;
  4499. }
  4500. double value = _edit.numeric_input * (_edit.numeric_negate ? -1 : 1);
  4501. double extra = 0.0;
  4502. switch (_edit.mode) {
  4503. case TRANSFORM_TRANSLATE:
  4504. motion *= value;
  4505. set_message(vformat(TTR("Translating %s."), motion));
  4506. break;
  4507. case TRANSFORM_ROTATE:
  4508. extra = Math::deg_to_rad(value);
  4509. set_message(vformat(TTR("Rotating %f degrees."), value));
  4510. break;
  4511. case TRANSFORM_SCALE:
  4512. // To halve the size of an object in Blender, you scale it by 0.5.
  4513. // Doing the same in Godot is considered scaling it by -0.5.
  4514. motion *= (value - 1.0);
  4515. set_message(vformat(TTR("Scaling %s."), motion));
  4516. break;
  4517. case TRANSFORM_NONE:
  4518. ERR_FAIL_MSG("_edit.mode cannot be TRANSFORM_NONE in update_transform_numeric.");
  4519. }
  4520. apply_transform(motion, extra);
  4521. }
  4522. // Perform cleanup after a transform operation is committed or canceled.
  4523. void Node3DEditorViewport::finish_transform() {
  4524. _edit.mode = TRANSFORM_NONE;
  4525. _edit.instant = false;
  4526. _edit.numeric_input = 0;
  4527. _edit.numeric_next_decimal = 0;
  4528. _edit.numeric_negate = false;
  4529. spatial_editor->set_local_coords_enabled(_edit.original_local);
  4530. spatial_editor->update_transform_gizmo();
  4531. surface->queue_redraw();
  4532. set_process_input(false);
  4533. }
  4534. // Register a shortcut and also add it as an input action with the same events.
  4535. void Node3DEditorViewport::register_shortcut_action(const String &p_path, const String &p_name, Key p_keycode, bool p_physical) {
  4536. Ref<Shortcut> sc = ED_SHORTCUT(p_path, p_name, p_keycode, p_physical);
  4537. shortcut_changed_callback(sc, p_path);
  4538. // Connect to the change event on the shortcut so the input binding can be updated.
  4539. sc->connect_changed(callable_mp(this, &Node3DEditorViewport::shortcut_changed_callback).bind(sc, p_path));
  4540. }
  4541. // Update the action in the InputMap to the provided shortcut events.
  4542. void Node3DEditorViewport::shortcut_changed_callback(const Ref<Shortcut> p_shortcut, const String &p_shortcut_path) {
  4543. InputMap *im = InputMap::get_singleton();
  4544. if (im->has_action(p_shortcut_path)) {
  4545. im->action_erase_events(p_shortcut_path);
  4546. } else {
  4547. im->add_action(p_shortcut_path);
  4548. }
  4549. for (int i = 0; i < p_shortcut->get_events().size(); i++) {
  4550. im->action_add_event(p_shortcut_path, p_shortcut->get_events()[i]);
  4551. }
  4552. }
  4553. void Node3DEditorViewport::_set_lock_view_rotation(bool p_lock_rotation) {
  4554. lock_rotation = p_lock_rotation;
  4555. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  4556. view_menu->get_popup()->set_item_checked(idx, p_lock_rotation);
  4557. if (p_lock_rotation) {
  4558. locked_label->show();
  4559. } else {
  4560. locked_label->hide();
  4561. }
  4562. }
  4563. Node3DEditorViewport::Node3DEditorViewport(Node3DEditor *p_spatial_editor, int p_index) {
  4564. cpu_time_history_index = 0;
  4565. gpu_time_history_index = 0;
  4566. _edit.mode = TRANSFORM_NONE;
  4567. _edit.plane = TRANSFORM_VIEW;
  4568. _edit.snap = true;
  4569. _edit.show_rotation_line = true;
  4570. _edit.instant = false;
  4571. _edit.gizmo_handle = -1;
  4572. _edit.gizmo_handle_secondary = false;
  4573. index = p_index;
  4574. editor_selection = EditorNode::get_singleton()->get_editor_selection();
  4575. orthogonal = false;
  4576. auto_orthogonal = false;
  4577. lock_rotation = false;
  4578. message_time = 0;
  4579. zoom_indicator_delay = 0.0;
  4580. spatial_editor = p_spatial_editor;
  4581. SubViewportContainer *c = memnew(SubViewportContainer);
  4582. subviewport_container = c;
  4583. c->set_stretch(true);
  4584. add_child(c);
  4585. c->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  4586. viewport = memnew(SubViewport);
  4587. viewport->set_disable_input(true);
  4588. c->add_child(viewport);
  4589. surface = memnew(Control);
  4590. SET_DRAG_FORWARDING_CD(surface, Node3DEditorViewport);
  4591. add_child(surface);
  4592. surface->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  4593. surface->set_clip_contents(true);
  4594. camera = memnew(Camera3D);
  4595. camera->set_disable_gizmos(true);
  4596. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + p_index)) | (1 << GIZMO_EDIT_LAYER) | (1 << GIZMO_GRID_LAYER) | (1 << MISC_TOOL_LAYER));
  4597. viewport->add_child(camera);
  4598. camera->make_current();
  4599. surface->set_focus_mode(FOCUS_ALL);
  4600. VBoxContainer *vbox = memnew(VBoxContainer);
  4601. surface->add_child(vbox);
  4602. vbox->set_offset(SIDE_LEFT, 10 * EDSCALE);
  4603. vbox->set_offset(SIDE_TOP, 10 * EDSCALE);
  4604. view_menu = memnew(MenuButton);
  4605. view_menu->set_flat(false);
  4606. view_menu->set_h_size_flags(0);
  4607. view_menu->set_shortcut_context(this);
  4608. vbox->add_child(view_menu);
  4609. view_menu->get_popup()->set_hide_on_checkable_item_selection(false);
  4610. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/top_view"), VIEW_TOP);
  4611. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/bottom_view"), VIEW_BOTTOM);
  4612. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/left_view"), VIEW_LEFT);
  4613. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/right_view"), VIEW_RIGHT);
  4614. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/front_view"), VIEW_FRONT);
  4615. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/rear_view"), VIEW_REAR);
  4616. view_menu->get_popup()->add_separator();
  4617. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal"), VIEW_SWITCH_PERSPECTIVE_ORTHOGONAL);
  4618. view_menu->get_popup()->add_radio_check_item(TTR("Perspective"), VIEW_PERSPECTIVE);
  4619. view_menu->get_popup()->add_radio_check_item(TTR("Orthogonal"), VIEW_ORTHOGONAL);
  4620. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  4621. view_menu->get_popup()->add_check_item(TTR("Auto Orthogonal Enabled"), VIEW_AUTO_ORTHOGONAL);
  4622. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUTO_ORTHOGONAL), true);
  4623. view_menu->get_popup()->add_separator();
  4624. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_lock_rotation", TTR("Lock View Rotation")), VIEW_LOCK_ROTATION);
  4625. view_menu->get_popup()->add_separator();
  4626. // TRANSLATORS: "Normal" as in "normal life", not "normal vector".
  4627. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_normal", TTR("Display Normal")), VIEW_DISPLAY_NORMAL);
  4628. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_wireframe", TTR("Display Wireframe")), VIEW_DISPLAY_WIREFRAME);
  4629. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_overdraw", TTR("Display Overdraw")), VIEW_DISPLAY_OVERDRAW);
  4630. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_lighting", TTR("Display Lighting")), VIEW_DISPLAY_LIGHTING);
  4631. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_unshaded", TTR("Display Unshaded")), VIEW_DISPLAY_UNSHADED);
  4632. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true);
  4633. display_submenu = memnew(PopupMenu);
  4634. display_submenu->set_hide_on_checkable_item_selection(false);
  4635. display_submenu->add_radio_check_item(TTR("Directional Shadow Splits"), VIEW_DISPLAY_DEBUG_PSSM_SPLITS);
  4636. display_submenu->add_separator();
  4637. display_submenu->add_radio_check_item(TTR("Normal Buffer"), VIEW_DISPLAY_NORMAL_BUFFER);
  4638. display_submenu->add_separator();
  4639. display_submenu->add_radio_check_item(TTR("Shadow Atlas"), VIEW_DISPLAY_DEBUG_SHADOW_ATLAS);
  4640. display_submenu->add_radio_check_item(TTR("Directional Shadow Map"), VIEW_DISPLAY_DEBUG_DIRECTIONAL_SHADOW_ATLAS);
  4641. display_submenu->add_separator();
  4642. display_submenu->add_radio_check_item(TTR("Decal Atlas"), VIEW_DISPLAY_DEBUG_DECAL_ATLAS);
  4643. display_submenu->add_separator();
  4644. display_submenu->add_radio_check_item(TTR("VoxelGI Lighting"), VIEW_DISPLAY_DEBUG_VOXEL_GI_LIGHTING);
  4645. display_submenu->add_radio_check_item(TTR("VoxelGI Albedo"), VIEW_DISPLAY_DEBUG_VOXEL_GI_ALBEDO);
  4646. display_submenu->add_radio_check_item(TTR("VoxelGI Emission"), VIEW_DISPLAY_DEBUG_VOXEL_GI_EMISSION);
  4647. display_submenu->add_separator();
  4648. display_submenu->add_radio_check_item(TTR("SDFGI Cascades"), VIEW_DISPLAY_DEBUG_SDFGI);
  4649. display_submenu->add_radio_check_item(TTR("SDFGI Probes"), VIEW_DISPLAY_DEBUG_SDFGI_PROBES);
  4650. display_submenu->add_separator();
  4651. display_submenu->add_radio_check_item(TTR("Scene Luminance"), VIEW_DISPLAY_DEBUG_SCENE_LUMINANCE);
  4652. display_submenu->add_separator();
  4653. display_submenu->add_radio_check_item(TTR("SSAO"), VIEW_DISPLAY_DEBUG_SSAO);
  4654. display_submenu->add_radio_check_item(TTR("SSIL"), VIEW_DISPLAY_DEBUG_SSIL);
  4655. display_submenu->add_separator();
  4656. display_submenu->add_radio_check_item(TTR("VoxelGI/SDFGI Buffer"), VIEW_DISPLAY_DEBUG_GI_BUFFER);
  4657. display_submenu->add_separator();
  4658. display_submenu->add_radio_check_item(TTR("Disable Mesh LOD"), VIEW_DISPLAY_DEBUG_DISABLE_LOD);
  4659. display_submenu->add_separator();
  4660. display_submenu->add_radio_check_item(TTR("OmniLight3D Cluster"), VIEW_DISPLAY_DEBUG_CLUSTER_OMNI_LIGHTS);
  4661. display_submenu->add_radio_check_item(TTR("SpotLight3D Cluster"), VIEW_DISPLAY_DEBUG_CLUSTER_SPOT_LIGHTS);
  4662. display_submenu->add_radio_check_item(TTR("Decal Cluster"), VIEW_DISPLAY_DEBUG_CLUSTER_DECALS);
  4663. display_submenu->add_radio_check_item(TTR("ReflectionProbe Cluster"), VIEW_DISPLAY_DEBUG_CLUSTER_REFLECTION_PROBES);
  4664. display_submenu->add_radio_check_item(TTR("Occlusion Culling Buffer"), VIEW_DISPLAY_DEBUG_OCCLUDERS);
  4665. display_submenu->add_radio_check_item(TTR("Motion Vectors"), VIEW_DISPLAY_MOTION_VECTORS);
  4666. display_submenu->add_radio_check_item(TTR("Internal Buffer"), VIEW_DISPLAY_INTERNAL_BUFFER);
  4667. view_menu->get_popup()->add_submenu_node_item(TTR("Display Advanced..."), display_submenu, VIEW_DISPLAY_ADVANCED);
  4668. view_menu->get_popup()->add_separator();
  4669. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment", TTR("View Environment")), VIEW_ENVIRONMENT);
  4670. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos", TTR("View Gizmos")), VIEW_GIZMOS);
  4671. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_transform_gizmo", TTR("View Transform Gizmo")), VIEW_TRANSFORM_GIZMO);
  4672. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_grid_lines", TTR("View Grid")), VIEW_GRID);
  4673. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_information", TTR("View Information")), VIEW_INFORMATION);
  4674. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_fps", TTR("View Frame Time")), VIEW_FRAME_TIME);
  4675. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT), true);
  4676. view_menu->get_popup()->add_separator();
  4677. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_half_resolution", TTR("Half Resolution")), VIEW_HALF_RESOLUTION);
  4678. view_menu->get_popup()->add_separator();
  4679. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_listener", TTR("Audio Listener")), VIEW_AUDIO_LISTENER);
  4680. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_doppler", TTR("Enable Doppler")), VIEW_AUDIO_DOPPLER);
  4681. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS), true);
  4682. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_TRANSFORM_GIZMO), true);
  4683. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_GRID), true);
  4684. view_menu->get_popup()->add_separator();
  4685. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_cinematic_preview", TTR("Cinematic Preview")), VIEW_CINEMATIC_PREVIEW);
  4686. view_menu->get_popup()->add_separator();
  4687. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_origin"), VIEW_CENTER_TO_ORIGIN);
  4688. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_selection"), VIEW_CENTER_TO_SELECTION);
  4689. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_transform_with_view"), VIEW_ALIGN_TRANSFORM_WITH_VIEW);
  4690. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_rotation_with_view"), VIEW_ALIGN_ROTATION_WITH_VIEW);
  4691. view_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Node3DEditorViewport::_menu_option));
  4692. display_submenu->connect(SceneStringName(id_pressed), callable_mp(this, &Node3DEditorViewport::_menu_option));
  4693. view_menu->set_disable_shortcuts(true);
  4694. // TODO: Re-evaluate with new OpenGL3 renderer, and implement.
  4695. //if (OS::get_singleton()->get_current_video_driver() == OS::RENDERING_DRIVER_OPENGL3) {
  4696. if (false) {
  4697. // Alternate display modes only work when using the Vulkan renderer; make this explicit.
  4698. const int normal_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL);
  4699. const int wireframe_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME);
  4700. const int overdraw_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW);
  4701. const int shadeless_idx = view_menu->get_popup()->get_item_index(VIEW_DISPLAY_UNSHADED);
  4702. const String unsupported_tooltip = TTR("Not available when using the OpenGL renderer.");
  4703. view_menu->get_popup()->set_item_disabled(normal_idx, true);
  4704. view_menu->get_popup()->set_item_tooltip(normal_idx, unsupported_tooltip);
  4705. view_menu->get_popup()->set_item_disabled(wireframe_idx, true);
  4706. view_menu->get_popup()->set_item_tooltip(wireframe_idx, unsupported_tooltip);
  4707. view_menu->get_popup()->set_item_disabled(overdraw_idx, true);
  4708. view_menu->get_popup()->set_item_tooltip(overdraw_idx, unsupported_tooltip);
  4709. view_menu->get_popup()->set_item_disabled(shadeless_idx, true);
  4710. view_menu->get_popup()->set_item_tooltip(shadeless_idx, unsupported_tooltip);
  4711. }
  4712. // Registering with Key::NONE intentionally creates an empty Array.
  4713. register_shortcut_action("spatial_editor/viewport_orbit_modifier_1", TTR("Viewport Orbit Modifier 1"), Key::NONE);
  4714. register_shortcut_action("spatial_editor/viewport_orbit_modifier_2", TTR("Viewport Orbit Modifier 2"), Key::NONE);
  4715. register_shortcut_action("spatial_editor/viewport_pan_modifier_1", TTR("Viewport Pan Modifier 1"), Key::SHIFT);
  4716. register_shortcut_action("spatial_editor/viewport_pan_modifier_2", TTR("Viewport Pan Modifier 2"), Key::NONE);
  4717. register_shortcut_action("spatial_editor/viewport_zoom_modifier_1", TTR("Viewport Zoom Modifier 1"), Key::SHIFT);
  4718. register_shortcut_action("spatial_editor/viewport_zoom_modifier_2", TTR("Viewport Zoom Modifier 2"), Key::CTRL);
  4719. register_shortcut_action("spatial_editor/freelook_left", TTR("Freelook Left"), Key::A, true);
  4720. register_shortcut_action("spatial_editor/freelook_right", TTR("Freelook Right"), Key::D, true);
  4721. register_shortcut_action("spatial_editor/freelook_forward", TTR("Freelook Forward"), Key::W, true);
  4722. register_shortcut_action("spatial_editor/freelook_backwards", TTR("Freelook Backwards"), Key::S, true);
  4723. register_shortcut_action("spatial_editor/freelook_up", TTR("Freelook Up"), Key::E, true);
  4724. register_shortcut_action("spatial_editor/freelook_down", TTR("Freelook Down"), Key::Q, true);
  4725. register_shortcut_action("spatial_editor/freelook_speed_modifier", TTR("Freelook Speed Modifier"), Key::SHIFT);
  4726. register_shortcut_action("spatial_editor/freelook_slow_modifier", TTR("Freelook Slow Modifier"), Key::ALT);
  4727. ED_SHORTCUT("spatial_editor/lock_transform_x", TTR("Lock Transformation to X axis"), Key::X);
  4728. ED_SHORTCUT("spatial_editor/lock_transform_y", TTR("Lock Transformation to Y axis"), Key::Y);
  4729. ED_SHORTCUT("spatial_editor/lock_transform_z", TTR("Lock Transformation to Z axis"), Key::Z);
  4730. ED_SHORTCUT("spatial_editor/lock_transform_yz", TTR("Lock Transformation to YZ plane"), KeyModifierMask::SHIFT | Key::X);
  4731. ED_SHORTCUT("spatial_editor/lock_transform_xz", TTR("Lock Transformation to XZ plane"), KeyModifierMask::SHIFT | Key::Y);
  4732. ED_SHORTCUT("spatial_editor/lock_transform_xy", TTR("Lock Transformation to XY plane"), KeyModifierMask::SHIFT | Key::Z);
  4733. ED_SHORTCUT("spatial_editor/cancel_transform", TTR("Cancel Transformation"), Key::ESCAPE);
  4734. ED_SHORTCUT("spatial_editor/instant_translate", TTR("Begin Translate Transformation"));
  4735. ED_SHORTCUT("spatial_editor/instant_rotate", TTR("Begin Rotate Transformation"));
  4736. ED_SHORTCUT("spatial_editor/instant_scale", TTR("Begin Scale Transformation"));
  4737. ED_SHORTCUT("spatial_editor/collision_reposition", TTR("Reposition Using Collisions"), KeyModifierMask::SHIFT | Key::G);
  4738. preview_camera = memnew(CheckBox);
  4739. preview_camera->set_text(TTR("Preview"));
  4740. // Using Control even on macOS to avoid conflict with Quick Open shortcut.
  4741. preview_camera->set_shortcut(ED_SHORTCUT("spatial_editor/toggle_camera_preview", TTR("Toggle Camera Preview"), KeyModifierMask::CTRL | Key::P));
  4742. vbox->add_child(preview_camera);
  4743. preview_camera->set_h_size_flags(0);
  4744. preview_camera->hide();
  4745. preview_camera->connect(SceneStringName(toggled), callable_mp(this, &Node3DEditorViewport::_toggle_camera_preview));
  4746. previewing = nullptr;
  4747. gizmo_scale = 1.0;
  4748. preview_node = nullptr;
  4749. bottom_center_vbox = memnew(VBoxContainer);
  4750. bottom_center_vbox->set_anchors_preset(LayoutPreset::PRESET_CENTER);
  4751. bottom_center_vbox->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -20 * EDSCALE);
  4752. bottom_center_vbox->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  4753. bottom_center_vbox->set_h_grow_direction(GROW_DIRECTION_BOTH);
  4754. bottom_center_vbox->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  4755. surface->add_child(bottom_center_vbox);
  4756. info_panel = memnew(PanelContainer);
  4757. info_panel->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -90 * EDSCALE);
  4758. info_panel->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -90 * EDSCALE);
  4759. info_panel->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, -10 * EDSCALE);
  4760. info_panel->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  4761. info_panel->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  4762. info_panel->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  4763. info_panel->set_mouse_filter(MOUSE_FILTER_IGNORE);
  4764. surface->add_child(info_panel);
  4765. info_panel->hide();
  4766. info_label = memnew(Label);
  4767. info_panel->add_child(info_label);
  4768. cinema_label = memnew(Label);
  4769. cinema_label->set_anchor_and_offset(SIDE_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  4770. cinema_label->set_h_grow_direction(GROW_DIRECTION_END);
  4771. cinema_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  4772. cinema_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  4773. surface->add_child(cinema_label);
  4774. cinema_label->set_text(TTR("Cinematic Preview"));
  4775. cinema_label->hide();
  4776. previewing_cinema = false;
  4777. locked_label = memnew(Label);
  4778. locked_label->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  4779. locked_label->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  4780. locked_label->set_h_size_flags(SIZE_SHRINK_CENTER);
  4781. bottom_center_vbox->add_child(locked_label);
  4782. locked_label->set_text(TTR("View Rotation Locked"));
  4783. locked_label->hide();
  4784. zoom_limit_label = memnew(Label);
  4785. zoom_limit_label->set_text(TTR("To zoom further, change the camera's clipping planes (View -> Settings...)"));
  4786. zoom_limit_label->set_name("ZoomLimitMessageLabel");
  4787. zoom_limit_label->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1, 1));
  4788. zoom_limit_label->hide();
  4789. bottom_center_vbox->add_child(zoom_limit_label);
  4790. preview_material_label = memnew(Label);
  4791. preview_material_label->set_anchors_and_offsets_preset(LayoutPreset::PRESET_BOTTOM_LEFT);
  4792. preview_material_label->set_offset(Side::SIDE_TOP, -70 * EDSCALE);
  4793. preview_material_label->set_text(TTR("Overriding material..."));
  4794. preview_material_label->add_theme_color_override(SceneStringName(font_color), Color(1, 1, 1, 1));
  4795. preview_material_label->hide();
  4796. surface->add_child(preview_material_label);
  4797. preview_material_label_desc = memnew(Label);
  4798. preview_material_label_desc->set_anchors_and_offsets_preset(LayoutPreset::PRESET_BOTTOM_LEFT);
  4799. preview_material_label_desc->set_offset(Side::SIDE_TOP, -50 * EDSCALE);
  4800. Key key = (OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios")) ? Key::META : Key::CTRL;
  4801. preview_material_label_desc->set_text(vformat(TTR("Drag and drop to override the material of any geometry node.\nHold %s when dropping to override a specific surface."), find_keycode_name(key)));
  4802. preview_material_label_desc->add_theme_color_override(SceneStringName(font_color), Color(0.8, 0.8, 0.8, 1));
  4803. preview_material_label_desc->add_theme_constant_override("line_spacing", 0);
  4804. preview_material_label_desc->hide();
  4805. surface->add_child(preview_material_label_desc);
  4806. frame_time_gradient = memnew(Gradient);
  4807. // The color is set when the theme changes.
  4808. frame_time_gradient->add_point(0.5, Color());
  4809. top_right_vbox = memnew(VBoxContainer);
  4810. top_right_vbox->add_theme_constant_override("separation", 10.0 * EDSCALE);
  4811. top_right_vbox->set_anchors_and_offsets_preset(PRESET_TOP_RIGHT, PRESET_MODE_MINSIZE, 10.0 * EDSCALE);
  4812. top_right_vbox->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  4813. const int navigation_control_size = 150;
  4814. position_control = memnew(ViewportNavigationControl);
  4815. position_control->set_navigation_mode(Node3DEditorViewport::NAVIGATION_MOVE);
  4816. position_control->set_custom_minimum_size(Size2(navigation_control_size, navigation_control_size) * EDSCALE);
  4817. position_control->set_h_size_flags(SIZE_SHRINK_END);
  4818. position_control->set_anchor_and_offset(SIDE_LEFT, ANCHOR_BEGIN, 0);
  4819. position_control->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -navigation_control_size * EDSCALE);
  4820. position_control->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_BEGIN, navigation_control_size * EDSCALE);
  4821. position_control->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  4822. position_control->set_viewport(this);
  4823. surface->add_child(position_control);
  4824. look_control = memnew(ViewportNavigationControl);
  4825. look_control->set_navigation_mode(Node3DEditorViewport::NAVIGATION_LOOK);
  4826. look_control->set_custom_minimum_size(Size2(navigation_control_size, navigation_control_size) * EDSCALE);
  4827. look_control->set_h_size_flags(SIZE_SHRINK_END);
  4828. look_control->set_anchor_and_offset(SIDE_LEFT, ANCHOR_END, -navigation_control_size * EDSCALE);
  4829. look_control->set_anchor_and_offset(SIDE_TOP, ANCHOR_END, -navigation_control_size * EDSCALE);
  4830. look_control->set_anchor_and_offset(SIDE_RIGHT, ANCHOR_END, 0);
  4831. look_control->set_anchor_and_offset(SIDE_BOTTOM, ANCHOR_END, 0);
  4832. look_control->set_viewport(this);
  4833. surface->add_child(look_control);
  4834. rotation_control = memnew(ViewportRotationControl);
  4835. rotation_control->set_custom_minimum_size(Size2(80, 80) * EDSCALE);
  4836. rotation_control->set_h_size_flags(SIZE_SHRINK_END);
  4837. rotation_control->set_viewport(this);
  4838. top_right_vbox->add_child(rotation_control);
  4839. frame_time_panel = memnew(PanelContainer);
  4840. frame_time_panel->set_mouse_filter(MOUSE_FILTER_IGNORE);
  4841. top_right_vbox->add_child(frame_time_panel);
  4842. frame_time_panel->hide();
  4843. frame_time_vbox = memnew(VBoxContainer);
  4844. frame_time_panel->add_child(frame_time_vbox);
  4845. // Individual Labels are used to allow coloring each label with its own color.
  4846. cpu_time_label = memnew(Label);
  4847. frame_time_vbox->add_child(cpu_time_label);
  4848. gpu_time_label = memnew(Label);
  4849. frame_time_vbox->add_child(gpu_time_label);
  4850. fps_label = memnew(Label);
  4851. frame_time_vbox->add_child(fps_label);
  4852. surface->add_child(top_right_vbox);
  4853. accept = nullptr;
  4854. freelook_active = false;
  4855. freelook_speed = EDITOR_GET("editors/3d/freelook/freelook_base_speed");
  4856. selection_menu = memnew(PopupMenu);
  4857. add_child(selection_menu);
  4858. selection_menu->set_min_size(Size2(100, 0) * EDSCALE);
  4859. selection_menu->connect(SceneStringName(id_pressed), callable_mp(this, &Node3DEditorViewport::_selection_result_pressed));
  4860. selection_menu->connect("popup_hide", callable_mp(this, &Node3DEditorViewport::_selection_menu_hide));
  4861. if (p_index == 0) {
  4862. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER), true);
  4863. viewport->set_as_audio_listener_3d(true);
  4864. }
  4865. view_type = VIEW_TYPE_USER;
  4866. _update_name();
  4867. EditorSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Node3DEditorViewport::update_transform_gizmo_view));
  4868. }
  4869. Node3DEditorViewport::~Node3DEditorViewport() {
  4870. memdelete(frame_time_gradient);
  4871. }
  4872. //////////////////////////////////////////////////////////////
  4873. void Node3DEditorViewportContainer::gui_input(const Ref<InputEvent> &p_event) {
  4874. ERR_FAIL_COND(p_event.is_null());
  4875. Ref<InputEventMouseButton> mb = p_event;
  4876. if (mb.is_valid() && mb->get_button_index() == MouseButton::LEFT) {
  4877. if (mb->is_pressed()) {
  4878. Vector2 size = get_size();
  4879. int h_sep = get_theme_constant(SNAME("separation"), SNAME("HSplitContainer"));
  4880. int v_sep = get_theme_constant(SNAME("separation"), SNAME("VSplitContainer"));
  4881. int mid_w = size.width * ratio_h;
  4882. int mid_h = size.height * ratio_v;
  4883. dragging_h = mb->get_position().x > (mid_w - h_sep / 2) && mb->get_position().x < (mid_w + h_sep / 2);
  4884. dragging_v = mb->get_position().y > (mid_h - v_sep / 2) && mb->get_position().y < (mid_h + v_sep / 2);
  4885. drag_begin_pos = mb->get_position();
  4886. drag_begin_ratio.x = ratio_h;
  4887. drag_begin_ratio.y = ratio_v;
  4888. switch (view) {
  4889. case VIEW_USE_1_VIEWPORT: {
  4890. dragging_h = false;
  4891. dragging_v = false;
  4892. } break;
  4893. case VIEW_USE_2_VIEWPORTS: {
  4894. dragging_h = false;
  4895. } break;
  4896. case VIEW_USE_2_VIEWPORTS_ALT: {
  4897. dragging_v = false;
  4898. } break;
  4899. case VIEW_USE_3_VIEWPORTS:
  4900. case VIEW_USE_3_VIEWPORTS_ALT:
  4901. case VIEW_USE_4_VIEWPORTS: {
  4902. // Do nothing.
  4903. } break;
  4904. }
  4905. } else {
  4906. dragging_h = false;
  4907. dragging_v = false;
  4908. }
  4909. }
  4910. Ref<InputEventMouseMotion> mm = p_event;
  4911. if (mm.is_valid()) {
  4912. if (view == VIEW_USE_3_VIEWPORTS || view == VIEW_USE_3_VIEWPORTS_ALT || view == VIEW_USE_4_VIEWPORTS) {
  4913. Vector2 size = get_size();
  4914. int h_sep = get_theme_constant(SNAME("separation"), SNAME("HSplitContainer"));
  4915. int v_sep = get_theme_constant(SNAME("separation"), SNAME("VSplitContainer"));
  4916. int mid_w = size.width * ratio_h;
  4917. int mid_h = size.height * ratio_v;
  4918. bool was_hovering_h = hovering_h;
  4919. bool was_hovering_v = hovering_v;
  4920. hovering_h = mm->get_position().x > (mid_w - h_sep / 2) && mm->get_position().x < (mid_w + h_sep / 2);
  4921. hovering_v = mm->get_position().y > (mid_h - v_sep / 2) && mm->get_position().y < (mid_h + v_sep / 2);
  4922. if (was_hovering_h != hovering_h || was_hovering_v != hovering_v) {
  4923. queue_redraw();
  4924. }
  4925. }
  4926. if (dragging_h) {
  4927. real_t new_ratio = drag_begin_ratio.x + (mm->get_position().x - drag_begin_pos.x) / get_size().width;
  4928. new_ratio = CLAMP(new_ratio, 40 / get_size().width, (get_size().width - 40) / get_size().width);
  4929. ratio_h = new_ratio;
  4930. queue_sort();
  4931. queue_redraw();
  4932. }
  4933. if (dragging_v) {
  4934. real_t new_ratio = drag_begin_ratio.y + (mm->get_position().y - drag_begin_pos.y) / get_size().height;
  4935. new_ratio = CLAMP(new_ratio, 40 / get_size().height, (get_size().height - 40) / get_size().height);
  4936. ratio_v = new_ratio;
  4937. queue_sort();
  4938. queue_redraw();
  4939. }
  4940. }
  4941. }
  4942. void Node3DEditorViewportContainer::_notification(int p_what) {
  4943. switch (p_what) {
  4944. case NOTIFICATION_MOUSE_ENTER:
  4945. case NOTIFICATION_MOUSE_EXIT: {
  4946. mouseover = (p_what == NOTIFICATION_MOUSE_ENTER);
  4947. queue_redraw();
  4948. } break;
  4949. case NOTIFICATION_DRAW: {
  4950. if (mouseover) {
  4951. Ref<Texture2D> h_grabber = get_theme_icon(SNAME("grabber"), SNAME("HSplitContainer"));
  4952. Ref<Texture2D> v_grabber = get_theme_icon(SNAME("grabber"), SNAME("VSplitContainer"));
  4953. Ref<Texture2D> hdiag_grabber = get_editor_theme_icon(SNAME("GuiViewportHdiagsplitter"));
  4954. Ref<Texture2D> vdiag_grabber = get_editor_theme_icon(SNAME("GuiViewportVdiagsplitter"));
  4955. Ref<Texture2D> vh_grabber = get_editor_theme_icon(SNAME("GuiViewportVhsplitter"));
  4956. Vector2 size = get_size();
  4957. int h_sep = get_theme_constant(SNAME("separation"), SNAME("HSplitContainer"));
  4958. int v_sep = get_theme_constant(SNAME("separation"), SNAME("VSplitContainer"));
  4959. int mid_w = size.width * ratio_h;
  4960. int mid_h = size.height * ratio_v;
  4961. int size_left = mid_w - h_sep / 2;
  4962. int size_bottom = size.height - mid_h - v_sep / 2;
  4963. switch (view) {
  4964. case VIEW_USE_1_VIEWPORT: {
  4965. // Nothing to show.
  4966. } break;
  4967. case VIEW_USE_2_VIEWPORTS: {
  4968. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  4969. set_default_cursor_shape(CURSOR_VSPLIT);
  4970. } break;
  4971. case VIEW_USE_2_VIEWPORTS_ALT: {
  4972. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  4973. set_default_cursor_shape(CURSOR_HSPLIT);
  4974. } break;
  4975. case VIEW_USE_3_VIEWPORTS: {
  4976. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  4977. draw_texture(hdiag_grabber, Vector2(mid_w - hdiag_grabber->get_width() / 2, mid_h - v_grabber->get_height() / 4));
  4978. set_default_cursor_shape(CURSOR_DRAG);
  4979. } else if ((hovering_v && !dragging_h) || dragging_v) {
  4980. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  4981. set_default_cursor_shape(CURSOR_VSPLIT);
  4982. } else if (hovering_h || dragging_h) {
  4983. 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));
  4984. set_default_cursor_shape(CURSOR_HSPLIT);
  4985. }
  4986. } break;
  4987. case VIEW_USE_3_VIEWPORTS_ALT: {
  4988. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  4989. draw_texture(vdiag_grabber, Vector2(mid_w - vdiag_grabber->get_width() + v_grabber->get_height() / 4, mid_h - vdiag_grabber->get_height() / 2));
  4990. set_default_cursor_shape(CURSOR_DRAG);
  4991. } else if ((hovering_v && !dragging_h) || dragging_v) {
  4992. draw_texture(v_grabber, Vector2((size_left - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  4993. set_default_cursor_shape(CURSOR_VSPLIT);
  4994. } else if (hovering_h || dragging_h) {
  4995. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  4996. set_default_cursor_shape(CURSOR_HSPLIT);
  4997. }
  4998. } break;
  4999. case VIEW_USE_4_VIEWPORTS: {
  5000. Vector2 half(mid_w, mid_h);
  5001. if ((hovering_v && hovering_h && !dragging_v && !dragging_h) || (dragging_v && dragging_h)) {
  5002. draw_texture(vh_grabber, half - vh_grabber->get_size() / 2.0);
  5003. set_default_cursor_shape(CURSOR_DRAG);
  5004. } else if ((hovering_v && !dragging_h) || dragging_v) {
  5005. draw_texture(v_grabber, half - v_grabber->get_size() / 2.0);
  5006. set_default_cursor_shape(CURSOR_VSPLIT);
  5007. } else if (hovering_h || dragging_h) {
  5008. draw_texture(h_grabber, half - h_grabber->get_size() / 2.0);
  5009. set_default_cursor_shape(CURSOR_HSPLIT);
  5010. }
  5011. } break;
  5012. }
  5013. }
  5014. } break;
  5015. case NOTIFICATION_SORT_CHILDREN: {
  5016. Node3DEditorViewport *viewports[4];
  5017. int vc = 0;
  5018. for (int i = 0; i < get_child_count(); i++) {
  5019. viewports[vc] = Object::cast_to<Node3DEditorViewport>(get_child(i));
  5020. if (viewports[vc]) {
  5021. vc++;
  5022. }
  5023. }
  5024. ERR_FAIL_COND(vc != 4);
  5025. Size2 size = get_size();
  5026. if (size.x < 10 || size.y < 10) {
  5027. for (int i = 0; i < 4; i++) {
  5028. viewports[i]->hide();
  5029. }
  5030. return;
  5031. }
  5032. int h_sep = get_theme_constant(SNAME("separation"), SNAME("HSplitContainer"));
  5033. int v_sep = get_theme_constant(SNAME("separation"), SNAME("VSplitContainer"));
  5034. int mid_w = size.width * ratio_h;
  5035. int mid_h = size.height * ratio_v;
  5036. int size_left = mid_w - h_sep / 2;
  5037. int size_right = size.width - mid_w - h_sep / 2;
  5038. int size_top = mid_h - v_sep / 2;
  5039. int size_bottom = size.height - mid_h - v_sep / 2;
  5040. switch (view) {
  5041. case VIEW_USE_1_VIEWPORT: {
  5042. viewports[0]->show();
  5043. for (int i = 1; i < 4; i++) {
  5044. viewports[i]->hide();
  5045. }
  5046. fit_child_in_rect(viewports[0], Rect2(Vector2(), size));
  5047. } break;
  5048. case VIEW_USE_2_VIEWPORTS: {
  5049. for (int i = 0; i < 4; i++) {
  5050. if (i == 1 || i == 3) {
  5051. viewports[i]->hide();
  5052. } else {
  5053. viewports[i]->show();
  5054. }
  5055. }
  5056. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  5057. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size.width, size_bottom)));
  5058. } break;
  5059. case VIEW_USE_2_VIEWPORTS_ALT: {
  5060. for (int i = 0; i < 4; i++) {
  5061. if (i == 1 || i == 3) {
  5062. viewports[i]->hide();
  5063. } else {
  5064. viewports[i]->show();
  5065. }
  5066. }
  5067. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size.height)));
  5068. fit_child_in_rect(viewports[2], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  5069. } break;
  5070. case VIEW_USE_3_VIEWPORTS: {
  5071. for (int i = 0; i < 4; i++) {
  5072. if (i == 1) {
  5073. viewports[i]->hide();
  5074. } else {
  5075. viewports[i]->show();
  5076. }
  5077. }
  5078. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  5079. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  5080. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  5081. } break;
  5082. case VIEW_USE_3_VIEWPORTS_ALT: {
  5083. for (int i = 0; i < 4; i++) {
  5084. if (i == 1) {
  5085. viewports[i]->hide();
  5086. } else {
  5087. viewports[i]->show();
  5088. }
  5089. }
  5090. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  5091. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  5092. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  5093. } break;
  5094. case VIEW_USE_4_VIEWPORTS: {
  5095. for (int i = 0; i < 4; i++) {
  5096. viewports[i]->show();
  5097. }
  5098. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  5099. fit_child_in_rect(viewports[1], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size_top)));
  5100. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  5101. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  5102. } break;
  5103. }
  5104. } break;
  5105. }
  5106. }
  5107. void Node3DEditorViewportContainer::set_view(View p_view) {
  5108. view = p_view;
  5109. queue_sort();
  5110. }
  5111. Node3DEditorViewportContainer::View Node3DEditorViewportContainer::get_view() {
  5112. return view;
  5113. }
  5114. Node3DEditorViewportContainer::Node3DEditorViewportContainer() {
  5115. set_clip_contents(true);
  5116. view = VIEW_USE_1_VIEWPORT;
  5117. mouseover = false;
  5118. ratio_h = 0.5;
  5119. ratio_v = 0.5;
  5120. hovering_v = false;
  5121. hovering_h = false;
  5122. dragging_v = false;
  5123. dragging_h = false;
  5124. }
  5125. ///////////////////////////////////////////////////////////////////
  5126. Node3DEditor *Node3DEditor::singleton = nullptr;
  5127. Node3DEditorSelectedItem::~Node3DEditorSelectedItem() {
  5128. ERR_FAIL_NULL(RenderingServer::get_singleton());
  5129. if (sbox_instance.is_valid()) {
  5130. RenderingServer::get_singleton()->free(sbox_instance);
  5131. }
  5132. if (sbox_instance_offset.is_valid()) {
  5133. RenderingServer::get_singleton()->free(sbox_instance_offset);
  5134. }
  5135. if (sbox_instance_xray.is_valid()) {
  5136. RenderingServer::get_singleton()->free(sbox_instance_xray);
  5137. }
  5138. if (sbox_instance_xray_offset.is_valid()) {
  5139. RenderingServer::get_singleton()->free(sbox_instance_xray_offset);
  5140. }
  5141. }
  5142. void Node3DEditor::select_gizmo_highlight_axis(int p_axis) {
  5143. for (int i = 0; i < 3; i++) {
  5144. move_gizmo[i]->surface_set_material(0, i == p_axis ? gizmo_color_hl[i] : gizmo_color[i]);
  5145. move_plane_gizmo[i]->surface_set_material(0, (i + 6) == p_axis ? plane_gizmo_color_hl[i] : plane_gizmo_color[i]);
  5146. rotate_gizmo[i]->surface_set_material(0, (i + 3) == p_axis ? rotate_gizmo_color_hl[i] : rotate_gizmo_color[i]);
  5147. scale_gizmo[i]->surface_set_material(0, (i + 9) == p_axis ? gizmo_color_hl[i] : gizmo_color[i]);
  5148. scale_plane_gizmo[i]->surface_set_material(0, (i + 12) == p_axis ? plane_gizmo_color_hl[i] : plane_gizmo_color[i]);
  5149. }
  5150. }
  5151. void Node3DEditor::update_transform_gizmo() {
  5152. int count = 0;
  5153. bool local_gizmo_coords = are_local_coords_enabled();
  5154. Vector3 gizmo_center;
  5155. Basis gizmo_basis;
  5156. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  5157. if (se && se->gizmo.is_valid()) {
  5158. for (const KeyValue<int, Transform3D> &E : se->subgizmos) {
  5159. Transform3D xf = se->sp->get_global_transform() * se->gizmo->get_subgizmo_transform(E.key);
  5160. gizmo_center += xf.origin;
  5161. if (count == 0 && local_gizmo_coords) {
  5162. gizmo_basis = xf.basis;
  5163. }
  5164. count++;
  5165. }
  5166. } else {
  5167. List<Node *> &selection = editor_selection->get_selected_node_list();
  5168. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  5169. Node3D *sp = Object::cast_to<Node3D>(E->get());
  5170. if (!sp) {
  5171. continue;
  5172. }
  5173. if (sp->has_meta("_edit_lock_")) {
  5174. continue;
  5175. }
  5176. Node3DEditorSelectedItem *sel_item = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  5177. if (!sel_item) {
  5178. continue;
  5179. }
  5180. Transform3D xf = sel_item->sp->get_global_transform();
  5181. gizmo_center += xf.origin;
  5182. if (count == 0 && local_gizmo_coords) {
  5183. gizmo_basis = xf.basis;
  5184. }
  5185. count++;
  5186. }
  5187. }
  5188. gizmo.visible = count > 0;
  5189. gizmo.transform.origin = (count > 0) ? gizmo_center / count : Vector3();
  5190. gizmo.transform.basis = (count == 1) ? gizmo_basis : Basis();
  5191. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  5192. viewports[i]->update_transform_gizmo_view();
  5193. }
  5194. }
  5195. void _update_all_gizmos(Node *p_node) {
  5196. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  5197. Node3D *spatial_node = Object::cast_to<Node3D>(p_node->get_child(i));
  5198. if (spatial_node) {
  5199. spatial_node->update_gizmos();
  5200. }
  5201. _update_all_gizmos(p_node->get_child(i));
  5202. }
  5203. }
  5204. void Node3DEditor::update_all_gizmos(Node *p_node) {
  5205. if (!p_node && is_inside_tree()) {
  5206. p_node = get_tree()->get_edited_scene_root();
  5207. }
  5208. if (!p_node) {
  5209. // No edited scene, so nothing to update.
  5210. return;
  5211. }
  5212. _update_all_gizmos(p_node);
  5213. }
  5214. Object *Node3DEditor::_get_editor_data(Object *p_what) {
  5215. Node3D *sp = Object::cast_to<Node3D>(p_what);
  5216. if (!sp) {
  5217. return nullptr;
  5218. }
  5219. Node3DEditorSelectedItem *si = memnew(Node3DEditorSelectedItem);
  5220. si->sp = sp;
  5221. si->sbox_instance = RenderingServer::get_singleton()->instance_create2(
  5222. selection_box->get_rid(),
  5223. sp->get_world_3d()->get_scenario());
  5224. si->sbox_instance_offset = RenderingServer::get_singleton()->instance_create2(
  5225. selection_box->get_rid(),
  5226. sp->get_world_3d()->get_scenario());
  5227. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(
  5228. si->sbox_instance,
  5229. RS::SHADOW_CASTING_SETTING_OFF);
  5230. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(
  5231. si->sbox_instance_offset,
  5232. RS::SHADOW_CASTING_SETTING_OFF);
  5233. // Use the Edit layer to hide the selection box when View Gizmos is disabled, since it is a bit distracting.
  5234. // It's still possible to approximately guess what is selected by looking at the manipulation gizmo position.
  5235. RS::get_singleton()->instance_set_layer_mask(si->sbox_instance, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER);
  5236. RS::get_singleton()->instance_set_layer_mask(si->sbox_instance_offset, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER);
  5237. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  5238. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  5239. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_offset, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  5240. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_offset, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  5241. si->sbox_instance_xray = RenderingServer::get_singleton()->instance_create2(
  5242. selection_box_xray->get_rid(),
  5243. sp->get_world_3d()->get_scenario());
  5244. si->sbox_instance_xray_offset = RenderingServer::get_singleton()->instance_create2(
  5245. selection_box_xray->get_rid(),
  5246. sp->get_world_3d()->get_scenario());
  5247. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(
  5248. si->sbox_instance_xray,
  5249. RS::SHADOW_CASTING_SETTING_OFF);
  5250. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(
  5251. si->sbox_instance_xray_offset,
  5252. RS::SHADOW_CASTING_SETTING_OFF);
  5253. // Use the Edit layer to hide the selection box when View Gizmos is disabled, since it is a bit distracting.
  5254. // It's still possible to approximately guess what is selected by looking at the manipulation gizmo position.
  5255. RS::get_singleton()->instance_set_layer_mask(si->sbox_instance_xray, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER);
  5256. RS::get_singleton()->instance_set_layer_mask(si->sbox_instance_xray_offset, 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER);
  5257. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_xray, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  5258. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_xray, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  5259. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_xray_offset, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  5260. RS::get_singleton()->instance_geometry_set_flag(si->sbox_instance_xray_offset, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  5261. return si;
  5262. }
  5263. void Node3DEditor::_generate_selection_boxes() {
  5264. // Use two AABBs to create the illusion of a slightly thicker line.
  5265. AABB aabb(Vector3(), Vector3(1, 1, 1));
  5266. // Create a x-ray (visible through solid surfaces) and standard version of the selection box.
  5267. // Both will be drawn at the same position, but with different opacity.
  5268. // This lets the user see where the selection is while still having a sense of depth.
  5269. Ref<SurfaceTool> st = memnew(SurfaceTool);
  5270. Ref<SurfaceTool> st_xray = memnew(SurfaceTool);
  5271. st->begin(Mesh::PRIMITIVE_LINES);
  5272. st_xray->begin(Mesh::PRIMITIVE_LINES);
  5273. for (int i = 0; i < 12; i++) {
  5274. Vector3 a, b;
  5275. aabb.get_edge(i, a, b);
  5276. st->add_vertex(a);
  5277. st->add_vertex(b);
  5278. st_xray->add_vertex(a);
  5279. st_xray->add_vertex(b);
  5280. }
  5281. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  5282. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  5283. mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  5284. const Color selection_box_color = EDITOR_GET("editors/3d/selection_box_color");
  5285. mat->set_albedo(selection_box_color);
  5286. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  5287. st->set_material(mat);
  5288. selection_box = st->commit();
  5289. Ref<StandardMaterial3D> mat_xray = memnew(StandardMaterial3D);
  5290. mat_xray->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  5291. mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  5292. mat_xray->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  5293. mat_xray->set_albedo(selection_box_color * Color(1, 1, 1, 0.15));
  5294. mat_xray->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  5295. st_xray->set_material(mat_xray);
  5296. selection_box_xray = st_xray->commit();
  5297. }
  5298. Dictionary Node3DEditor::get_state() const {
  5299. Dictionary d;
  5300. d["snap_enabled"] = snap_enabled;
  5301. d["translate_snap"] = snap_translate_value;
  5302. d["rotate_snap"] = snap_rotate_value;
  5303. d["scale_snap"] = snap_scale_value;
  5304. d["local_coords"] = tool_option_button[TOOL_OPT_LOCAL_COORDS]->is_pressed();
  5305. int vc = 0;
  5306. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT))) {
  5307. vc = 1;
  5308. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS))) {
  5309. vc = 2;
  5310. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS))) {
  5311. vc = 3;
  5312. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS))) {
  5313. vc = 4;
  5314. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT))) {
  5315. vc = 5;
  5316. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT))) {
  5317. vc = 6;
  5318. }
  5319. d["viewport_mode"] = vc;
  5320. Array vpdata;
  5321. for (int i = 0; i < 4; i++) {
  5322. vpdata.push_back(viewports[i]->get_state());
  5323. }
  5324. d["viewports"] = vpdata;
  5325. d["show_grid"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID));
  5326. d["show_origin"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN));
  5327. d["fov"] = get_fov();
  5328. d["znear"] = get_znear();
  5329. d["zfar"] = get_zfar();
  5330. Dictionary gizmos_status;
  5331. for (int i = 0; i < gizmo_plugins_by_name.size(); i++) {
  5332. if (!gizmo_plugins_by_name[i]->can_be_hidden()) {
  5333. continue;
  5334. }
  5335. int state = gizmos_menu->get_item_state(gizmos_menu->get_item_index(i));
  5336. String name = gizmo_plugins_by_name[i]->get_gizmo_name();
  5337. gizmos_status[name] = state;
  5338. }
  5339. d["gizmos_status"] = gizmos_status;
  5340. {
  5341. Dictionary pd;
  5342. pd["sun_rotation"] = sun_rotation;
  5343. pd["environ_sky_color"] = environ_sky_color->get_pick_color();
  5344. pd["environ_ground_color"] = environ_ground_color->get_pick_color();
  5345. pd["environ_energy"] = environ_energy->get_value();
  5346. pd["environ_glow_enabled"] = environ_glow_button->is_pressed();
  5347. pd["environ_tonemap_enabled"] = environ_tonemap_button->is_pressed();
  5348. pd["environ_ao_enabled"] = environ_ao_button->is_pressed();
  5349. pd["environ_gi_enabled"] = environ_gi_button->is_pressed();
  5350. pd["sun_max_distance"] = sun_max_distance->get_value();
  5351. pd["sun_color"] = sun_color->get_pick_color();
  5352. pd["sun_energy"] = sun_energy->get_value();
  5353. pd["sun_enabled"] = sun_button->is_pressed();
  5354. pd["environ_enabled"] = environ_button->is_pressed();
  5355. d["preview_sun_env"] = pd;
  5356. }
  5357. return d;
  5358. }
  5359. void Node3DEditor::set_state(const Dictionary &p_state) {
  5360. Dictionary d = p_state;
  5361. if (d.has("snap_enabled")) {
  5362. snap_enabled = d["snap_enabled"];
  5363. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(d["snap_enabled"]);
  5364. }
  5365. if (d.has("translate_snap")) {
  5366. snap_translate_value = d["translate_snap"];
  5367. }
  5368. if (d.has("rotate_snap")) {
  5369. snap_rotate_value = d["rotate_snap"];
  5370. }
  5371. if (d.has("scale_snap")) {
  5372. snap_scale_value = d["scale_snap"];
  5373. }
  5374. _snap_update();
  5375. if (d.has("local_coords")) {
  5376. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(d["local_coords"]);
  5377. update_transform_gizmo();
  5378. }
  5379. if (d.has("viewport_mode")) {
  5380. int vc = d["viewport_mode"];
  5381. if (vc == 1) {
  5382. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  5383. } else if (vc == 2) {
  5384. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  5385. } else if (vc == 3) {
  5386. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  5387. } else if (vc == 4) {
  5388. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  5389. } else if (vc == 5) {
  5390. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  5391. } else if (vc == 6) {
  5392. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  5393. }
  5394. }
  5395. if (d.has("viewports")) {
  5396. Array vp = d["viewports"];
  5397. uint32_t vp_size = static_cast<uint32_t>(vp.size());
  5398. if (vp_size > VIEWPORTS_COUNT) {
  5399. WARN_PRINT("Ignoring superfluous viewport settings from spatial editor state.");
  5400. vp_size = VIEWPORTS_COUNT;
  5401. }
  5402. for (uint32_t i = 0; i < vp_size; i++) {
  5403. viewports[i]->set_state(vp[i]);
  5404. }
  5405. }
  5406. if (d.has("zfar")) {
  5407. settings_zfar->set_value(double(d["zfar"]));
  5408. }
  5409. if (d.has("znear")) {
  5410. settings_znear->set_value(double(d["znear"]));
  5411. }
  5412. if (d.has("fov")) {
  5413. settings_fov->set_value(double(d["fov"]));
  5414. }
  5415. if (d.has("show_grid")) {
  5416. bool use = d["show_grid"];
  5417. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID))) {
  5418. _menu_item_pressed(MENU_VIEW_GRID);
  5419. }
  5420. }
  5421. if (d.has("show_origin")) {
  5422. bool use = d["show_origin"];
  5423. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN))) {
  5424. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), use);
  5425. RenderingServer::get_singleton()->instance_set_visible(origin_instance, use);
  5426. }
  5427. }
  5428. if (d.has("gizmos_status")) {
  5429. Dictionary gizmos_status = d["gizmos_status"];
  5430. List<Variant> keys;
  5431. gizmos_status.get_key_list(&keys);
  5432. for (int j = 0; j < gizmo_plugins_by_name.size(); ++j) {
  5433. if (!gizmo_plugins_by_name[j]->can_be_hidden()) {
  5434. continue;
  5435. }
  5436. int state = EditorNode3DGizmoPlugin::VISIBLE;
  5437. for (const Variant &key : keys) {
  5438. if (gizmo_plugins_by_name.write[j]->get_gizmo_name() == String(key)) {
  5439. state = gizmos_status[key];
  5440. break;
  5441. }
  5442. }
  5443. gizmo_plugins_by_name.write[j]->set_state(state);
  5444. }
  5445. _update_gizmos_menu();
  5446. }
  5447. if (d.has("preview_sun_env")) {
  5448. sun_environ_updating = true;
  5449. Dictionary pd = d["preview_sun_env"];
  5450. sun_rotation = pd["sun_rotation"];
  5451. environ_sky_color->set_pick_color(pd["environ_sky_color"]);
  5452. environ_ground_color->set_pick_color(pd["environ_ground_color"]);
  5453. environ_energy->set_value(pd["environ_energy"]);
  5454. environ_glow_button->set_pressed(pd["environ_glow_enabled"]);
  5455. environ_tonemap_button->set_pressed(pd["environ_tonemap_enabled"]);
  5456. environ_ao_button->set_pressed(pd["environ_ao_enabled"]);
  5457. environ_gi_button->set_pressed(pd["environ_gi_enabled"]);
  5458. sun_max_distance->set_value(pd["sun_max_distance"]);
  5459. sun_color->set_pick_color(pd["sun_color"]);
  5460. sun_energy->set_value(pd["sun_energy"]);
  5461. sun_button->set_pressed(pd["sun_enabled"]);
  5462. environ_button->set_pressed(pd["environ_enabled"]);
  5463. sun_environ_updating = false;
  5464. _preview_settings_changed();
  5465. _update_preview_environment();
  5466. } else {
  5467. _load_default_preview_settings();
  5468. sun_button->set_pressed(true);
  5469. environ_button->set_pressed(true);
  5470. _preview_settings_changed();
  5471. _update_preview_environment();
  5472. }
  5473. }
  5474. void Node3DEditor::edit(Node3D *p_spatial) {
  5475. if (p_spatial != selected) {
  5476. if (selected) {
  5477. Vector<Ref<Node3DGizmo>> gizmos = selected->get_gizmos();
  5478. for (int i = 0; i < gizmos.size(); i++) {
  5479. Ref<EditorNode3DGizmo> seg = gizmos[i];
  5480. if (!seg.is_valid()) {
  5481. continue;
  5482. }
  5483. seg->set_selected(false);
  5484. }
  5485. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected);
  5486. if (se) {
  5487. se->gizmo.unref();
  5488. se->subgizmos.clear();
  5489. }
  5490. selected->update_gizmos();
  5491. }
  5492. selected = p_spatial;
  5493. current_hover_gizmo = Ref<EditorNode3DGizmo>();
  5494. current_hover_gizmo_handle = -1;
  5495. current_hover_gizmo_handle_secondary = false;
  5496. if (selected) {
  5497. Vector<Ref<Node3DGizmo>> gizmos = selected->get_gizmos();
  5498. for (int i = 0; i < gizmos.size(); i++) {
  5499. Ref<EditorNode3DGizmo> seg = gizmos[i];
  5500. if (!seg.is_valid()) {
  5501. continue;
  5502. }
  5503. seg->set_selected(true);
  5504. }
  5505. selected->update_gizmos();
  5506. }
  5507. }
  5508. }
  5509. void Node3DEditor::_snap_changed() {
  5510. snap_translate_value = snap_translate->get_text().to_float();
  5511. snap_rotate_value = snap_rotate->get_text().to_float();
  5512. snap_scale_value = snap_scale->get_text().to_float();
  5513. EditorSettings::get_singleton()->set_project_metadata("3d_editor", "snap_translate_value", snap_translate_value);
  5514. EditorSettings::get_singleton()->set_project_metadata("3d_editor", "snap_rotate_value", snap_rotate_value);
  5515. EditorSettings::get_singleton()->set_project_metadata("3d_editor", "snap_scale_value", snap_scale_value);
  5516. }
  5517. void Node3DEditor::_snap_update() {
  5518. snap_translate->set_text(String::num(snap_translate_value));
  5519. snap_rotate->set_text(String::num(snap_rotate_value));
  5520. snap_scale->set_text(String::num(snap_scale_value));
  5521. }
  5522. void Node3DEditor::_xform_dialog_action() {
  5523. Transform3D t;
  5524. //translation
  5525. Vector3 scale;
  5526. Vector3 rotate;
  5527. Vector3 translate;
  5528. for (int i = 0; i < 3; i++) {
  5529. translate[i] = xform_translate[i]->get_text().to_float();
  5530. rotate[i] = Math::deg_to_rad(xform_rotate[i]->get_text().to_float());
  5531. scale[i] = xform_scale[i]->get_text().to_float();
  5532. }
  5533. t.basis.scale(scale);
  5534. t.basis.rotate(rotate);
  5535. t.origin = translate;
  5536. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  5537. undo_redo->create_action(TTR("XForm Dialog"));
  5538. const List<Node *> &selection = editor_selection->get_selected_node_list();
  5539. for (Node *E : selection) {
  5540. Node3D *sp = Object::cast_to<Node3D>(E);
  5541. if (!sp) {
  5542. continue;
  5543. }
  5544. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  5545. if (!se) {
  5546. continue;
  5547. }
  5548. bool post = xform_type->get_selected() > 0;
  5549. Transform3D tr = sp->get_global_gizmo_transform();
  5550. if (post) {
  5551. tr = tr * t;
  5552. } else {
  5553. tr.basis = t.basis * tr.basis;
  5554. tr.origin += t.origin;
  5555. }
  5556. Node3D *parent = sp->get_parent_node_3d();
  5557. Transform3D local_tr = parent ? parent->get_global_transform().affine_inverse() * tr : tr;
  5558. undo_redo->add_do_method(sp, "set_transform", local_tr);
  5559. undo_redo->add_undo_method(sp, "set_transform", sp->get_transform());
  5560. }
  5561. undo_redo->commit_action();
  5562. }
  5563. void Node3DEditor::_menu_item_toggled(bool pressed, int p_option) {
  5564. switch (p_option) {
  5565. case MENU_TOOL_LOCAL_COORDS: {
  5566. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(pressed);
  5567. update_transform_gizmo();
  5568. } break;
  5569. case MENU_TOOL_USE_SNAP: {
  5570. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(pressed);
  5571. snap_enabled = pressed;
  5572. } break;
  5573. }
  5574. }
  5575. void Node3DEditor::_menu_gizmo_toggled(int p_option) {
  5576. const int idx = gizmos_menu->get_item_index(p_option);
  5577. gizmos_menu->toggle_item_multistate(idx);
  5578. // Change icon
  5579. const int state = gizmos_menu->get_item_state(idx);
  5580. switch (state) {
  5581. case EditorNode3DGizmoPlugin::VISIBLE:
  5582. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_theme_icon(SNAME("visibility_visible")));
  5583. break;
  5584. case EditorNode3DGizmoPlugin::ON_TOP:
  5585. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_theme_icon(SNAME("visibility_xray")));
  5586. break;
  5587. case EditorNode3DGizmoPlugin::HIDDEN:
  5588. gizmos_menu->set_item_icon(idx, view_menu->get_popup()->get_theme_icon(SNAME("visibility_hidden")));
  5589. break;
  5590. }
  5591. gizmo_plugins_by_name.write[p_option]->set_state(state);
  5592. update_all_gizmos();
  5593. }
  5594. void Node3DEditor::_menu_item_pressed(int p_option) {
  5595. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  5596. switch (p_option) {
  5597. case MENU_TOOL_SELECT:
  5598. case MENU_TOOL_MOVE:
  5599. case MENU_TOOL_ROTATE:
  5600. case MENU_TOOL_SCALE:
  5601. case MENU_TOOL_LIST_SELECT: {
  5602. for (int i = 0; i < TOOL_MAX; i++) {
  5603. tool_button[i]->set_pressed(i == p_option);
  5604. }
  5605. tool_mode = (ToolMode)p_option;
  5606. update_transform_gizmo();
  5607. } break;
  5608. case MENU_TRANSFORM_CONFIGURE_SNAP: {
  5609. snap_dialog->popup_centered(Size2(200, 180));
  5610. } break;
  5611. case MENU_TRANSFORM_DIALOG: {
  5612. for (int i = 0; i < 3; i++) {
  5613. xform_translate[i]->set_text("0");
  5614. xform_rotate[i]->set_text("0");
  5615. xform_scale[i]->set_text("1");
  5616. }
  5617. xform_dialog->popup_centered(Size2(320, 240) * EDSCALE);
  5618. } break;
  5619. case MENU_VIEW_USE_1_VIEWPORT: {
  5620. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_1_VIEWPORT);
  5621. if (last_used_viewport > 0) {
  5622. last_used_viewport = 0;
  5623. }
  5624. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true);
  5625. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  5626. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  5627. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  5628. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  5629. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  5630. } break;
  5631. case MENU_VIEW_USE_2_VIEWPORTS: {
  5632. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_2_VIEWPORTS);
  5633. if (last_used_viewport > 1) {
  5634. last_used_viewport = 0;
  5635. }
  5636. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  5637. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true);
  5638. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  5639. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  5640. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  5641. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  5642. } break;
  5643. case MENU_VIEW_USE_2_VIEWPORTS_ALT: {
  5644. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_2_VIEWPORTS_ALT);
  5645. if (last_used_viewport > 1) {
  5646. last_used_viewport = 0;
  5647. }
  5648. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  5649. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  5650. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  5651. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  5652. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), true);
  5653. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  5654. } break;
  5655. case MENU_VIEW_USE_3_VIEWPORTS: {
  5656. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_3_VIEWPORTS);
  5657. if (last_used_viewport > 2) {
  5658. last_used_viewport = 0;
  5659. }
  5660. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  5661. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  5662. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), true);
  5663. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  5664. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  5665. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  5666. } break;
  5667. case MENU_VIEW_USE_3_VIEWPORTS_ALT: {
  5668. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_3_VIEWPORTS_ALT);
  5669. if (last_used_viewport > 2) {
  5670. last_used_viewport = 0;
  5671. }
  5672. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  5673. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  5674. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  5675. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  5676. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  5677. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), true);
  5678. } break;
  5679. case MENU_VIEW_USE_4_VIEWPORTS: {
  5680. viewport_base->set_view(Node3DEditorViewportContainer::VIEW_USE_4_VIEWPORTS);
  5681. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  5682. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  5683. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  5684. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), true);
  5685. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  5686. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  5687. } break;
  5688. case MENU_VIEW_ORIGIN: {
  5689. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  5690. origin_enabled = !is_checked;
  5691. RenderingServer::get_singleton()->instance_set_visible(origin_instance, origin_enabled);
  5692. // Update the grid since its appearance depends on whether the origin is enabled
  5693. _finish_grid();
  5694. _init_grid();
  5695. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), origin_enabled);
  5696. } break;
  5697. case MENU_VIEW_GRID: {
  5698. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  5699. grid_enabled = !is_checked;
  5700. for (int i = 0; i < 3; ++i) {
  5701. if (grid_enable[i]) {
  5702. grid_visible[i] = grid_enabled;
  5703. }
  5704. }
  5705. _finish_grid();
  5706. _init_grid();
  5707. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), grid_enabled);
  5708. } break;
  5709. case MENU_VIEW_CAMERA_SETTINGS: {
  5710. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50));
  5711. } break;
  5712. case MENU_SNAP_TO_FLOOR: {
  5713. snap_selected_nodes_to_floor();
  5714. } break;
  5715. case MENU_LOCK_SELECTED: {
  5716. undo_redo->create_action(TTR("Lock Selected"));
  5717. List<Node *> &selection = editor_selection->get_selected_node_list();
  5718. for (Node *E : selection) {
  5719. Node3D *spatial = Object::cast_to<Node3D>(E);
  5720. if (!spatial || !spatial->is_inside_tree()) {
  5721. continue;
  5722. }
  5723. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  5724. continue;
  5725. }
  5726. undo_redo->add_do_method(spatial, "set_meta", "_edit_lock_", true);
  5727. undo_redo->add_undo_method(spatial, "remove_meta", "_edit_lock_");
  5728. undo_redo->add_do_method(this, "emit_signal", "item_lock_status_changed");
  5729. undo_redo->add_undo_method(this, "emit_signal", "item_lock_status_changed");
  5730. }
  5731. undo_redo->add_do_method(this, "_refresh_menu_icons");
  5732. undo_redo->add_undo_method(this, "_refresh_menu_icons");
  5733. undo_redo->commit_action();
  5734. } break;
  5735. case MENU_UNLOCK_SELECTED: {
  5736. undo_redo->create_action(TTR("Unlock Selected"));
  5737. List<Node *> &selection = editor_selection->get_selected_node_list();
  5738. for (Node *E : selection) {
  5739. Node3D *spatial = Object::cast_to<Node3D>(E);
  5740. if (!spatial || !spatial->is_inside_tree()) {
  5741. continue;
  5742. }
  5743. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  5744. continue;
  5745. }
  5746. undo_redo->add_do_method(spatial, "remove_meta", "_edit_lock_");
  5747. undo_redo->add_undo_method(spatial, "set_meta", "_edit_lock_", true);
  5748. undo_redo->add_do_method(this, "emit_signal", "item_lock_status_changed");
  5749. undo_redo->add_undo_method(this, "emit_signal", "item_lock_status_changed");
  5750. }
  5751. undo_redo->add_do_method(this, "_refresh_menu_icons");
  5752. undo_redo->add_undo_method(this, "_refresh_menu_icons");
  5753. undo_redo->commit_action();
  5754. } break;
  5755. case MENU_GROUP_SELECTED: {
  5756. undo_redo->create_action(TTR("Group Selected"));
  5757. List<Node *> &selection = editor_selection->get_selected_node_list();
  5758. for (Node *E : selection) {
  5759. Node3D *spatial = Object::cast_to<Node3D>(E);
  5760. if (!spatial || !spatial->is_inside_tree()) {
  5761. continue;
  5762. }
  5763. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  5764. continue;
  5765. }
  5766. undo_redo->add_do_method(spatial, "set_meta", "_edit_group_", true);
  5767. undo_redo->add_undo_method(spatial, "remove_meta", "_edit_group_");
  5768. undo_redo->add_do_method(this, "emit_signal", "item_group_status_changed");
  5769. undo_redo->add_undo_method(this, "emit_signal", "item_group_status_changed");
  5770. }
  5771. undo_redo->add_do_method(this, "_refresh_menu_icons");
  5772. undo_redo->add_undo_method(this, "_refresh_menu_icons");
  5773. undo_redo->commit_action();
  5774. } break;
  5775. case MENU_UNGROUP_SELECTED: {
  5776. undo_redo->create_action(TTR("Ungroup Selected"));
  5777. List<Node *> &selection = editor_selection->get_selected_node_list();
  5778. for (Node *E : selection) {
  5779. Node3D *spatial = Object::cast_to<Node3D>(E);
  5780. if (!spatial || !spatial->is_inside_tree()) {
  5781. continue;
  5782. }
  5783. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root()) {
  5784. continue;
  5785. }
  5786. undo_redo->add_do_method(spatial, "remove_meta", "_edit_group_");
  5787. undo_redo->add_undo_method(spatial, "set_meta", "_edit_group_", true);
  5788. undo_redo->add_do_method(this, "emit_signal", "item_group_status_changed");
  5789. undo_redo->add_undo_method(this, "emit_signal", "item_group_status_changed");
  5790. }
  5791. undo_redo->add_do_method(this, "_refresh_menu_icons");
  5792. undo_redo->add_undo_method(this, "_refresh_menu_icons");
  5793. undo_redo->commit_action();
  5794. } break;
  5795. }
  5796. }
  5797. void Node3DEditor::_init_indicators() {
  5798. {
  5799. origin_enabled = true;
  5800. grid_enabled = true;
  5801. Ref<Shader> origin_shader = memnew(Shader);
  5802. origin_shader->set_code(R"(
  5803. // 3D editor origin line shader.
  5804. shader_type spatial;
  5805. render_mode blend_mix, cull_disabled, unshaded, fog_disabled;
  5806. void vertex() {
  5807. vec3 point_a = MODEL_MATRIX[3].xyz;
  5808. // Encoded in scale.
  5809. vec3 point_b = vec3(MODEL_MATRIX[0].x, MODEL_MATRIX[1].y, MODEL_MATRIX[2].z);
  5810. // Points are already in world space, so no need for MODEL_MATRIX anymore.
  5811. vec4 clip_a = PROJECTION_MATRIX * (VIEW_MATRIX * vec4(point_a, 1.0));
  5812. vec4 clip_b = PROJECTION_MATRIX * (VIEW_MATRIX * vec4(point_b, 1.0));
  5813. vec2 screen_a = VIEWPORT_SIZE * (0.5 * clip_a.xy / clip_a.w + 0.5);
  5814. vec2 screen_b = VIEWPORT_SIZE * (0.5 * clip_b.xy / clip_b.w + 0.5);
  5815. vec2 x_basis = normalize(screen_b - screen_a);
  5816. vec2 y_basis = vec2(-x_basis.y, x_basis.x);
  5817. float width = 3.0;
  5818. vec2 screen_point_a = screen_a + width * (VERTEX.x * x_basis + VERTEX.y * y_basis);
  5819. vec2 screen_point_b = screen_b + width * (VERTEX.x * x_basis + VERTEX.y * y_basis);
  5820. vec2 screen_point_final = mix(screen_point_a, screen_point_b, VERTEX.z);
  5821. vec4 clip_final = mix(clip_a, clip_b, VERTEX.z);
  5822. POSITION = vec4(clip_final.w * ((2.0 * screen_point_final) / VIEWPORT_SIZE - 1.0), clip_final.z, clip_final.w);
  5823. UV = VERTEX.yz * clip_final.w;
  5824. if (!OUTPUT_IS_SRGB) {
  5825. COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb, vec3(0.04045)));
  5826. }
  5827. }
  5828. void fragment() {
  5829. // Multiply by 0.5 since UV is actually UV is [-1, 1].
  5830. float line_width = fwidth(UV.x * 0.5);
  5831. float line_uv = abs(UV.x * 0.5);
  5832. float line = smoothstep(line_width * 1.0, line_width * 0.25, line_uv);
  5833. ALBEDO = COLOR.rgb;
  5834. ALPHA *= COLOR.a * line;
  5835. }
  5836. )");
  5837. origin_mat.instantiate();
  5838. origin_mat->set_shader(origin_shader);
  5839. Vector<Vector3> origin_points;
  5840. origin_points.resize(6);
  5841. origin_points.set(0, Vector3(0.0, -0.5, 0.0));
  5842. origin_points.set(1, Vector3(0.0, -0.5, 1.0));
  5843. origin_points.set(2, Vector3(0.0, 0.5, 1.0));
  5844. origin_points.set(3, Vector3(0.0, -0.5, 0.0));
  5845. origin_points.set(4, Vector3(0.0, 0.5, 1.0));
  5846. origin_points.set(5, Vector3(0.0, 0.5, 0.0));
  5847. Array d;
  5848. d.resize(RS::ARRAY_MAX);
  5849. d[RenderingServer::ARRAY_VERTEX] = origin_points;
  5850. origin_mesh = RenderingServer::get_singleton()->mesh_create();
  5851. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(origin_mesh, RenderingServer::PRIMITIVE_TRIANGLES, d);
  5852. RenderingServer::get_singleton()->mesh_surface_set_material(origin_mesh, 0, origin_mat->get_rid());
  5853. origin_multimesh = RenderingServer::get_singleton()->multimesh_create();
  5854. RenderingServer::get_singleton()->multimesh_set_mesh(origin_multimesh, origin_mesh);
  5855. RenderingServer::get_singleton()->multimesh_allocate_data(origin_multimesh, 12, RS::MultimeshTransformFormat::MULTIMESH_TRANSFORM_3D, true, false);
  5856. RenderingServer::get_singleton()->multimesh_set_visible_instances(origin_multimesh, -1);
  5857. LocalVector<float> distances;
  5858. distances.resize(5);
  5859. distances[0] = -1000000.0;
  5860. distances[1] = -1000.0;
  5861. distances[2] = 0.0;
  5862. distances[3] = 1000.0;
  5863. distances[4] = 1000000.0;
  5864. for (int i = 0; i < 3; i++) {
  5865. Color origin_color;
  5866. switch (i) {
  5867. case 0:
  5868. origin_color = get_theme_color(SNAME("axis_x_color"), EditorStringName(Editor));
  5869. break;
  5870. case 1:
  5871. origin_color = get_theme_color(SNAME("axis_y_color"), EditorStringName(Editor));
  5872. break;
  5873. case 2:
  5874. origin_color = get_theme_color(SNAME("axis_z_color"), EditorStringName(Editor));
  5875. break;
  5876. default:
  5877. origin_color = Color();
  5878. break;
  5879. }
  5880. Vector3 axis;
  5881. axis[i] = 1;
  5882. for (int j = 0; j < 4; j++) {
  5883. Transform3D t = Transform3D();
  5884. if (distances[j] > 0.0) {
  5885. t = t.scaled(axis * distances[j + 1]);
  5886. t = t.translated(axis * distances[j]);
  5887. } else {
  5888. t = t.scaled(axis * distances[j]);
  5889. t = t.translated(axis * distances[j + 1]);
  5890. }
  5891. RenderingServer::get_singleton()->multimesh_instance_set_transform(origin_multimesh, i * 4 + j, t);
  5892. RenderingServer::get_singleton()->multimesh_instance_set_color(origin_multimesh, i * 4 + j, origin_color);
  5893. }
  5894. }
  5895. origin_instance = RenderingServer::get_singleton()->instance_create2(origin_multimesh, get_tree()->get_root()->get_world_3d()->get_scenario());
  5896. RS::get_singleton()->instance_set_layer_mask(origin_instance, 1 << Node3DEditorViewport::GIZMO_GRID_LAYER);
  5897. RS::get_singleton()->instance_geometry_set_flag(origin_instance, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  5898. RS::get_singleton()->instance_geometry_set_flag(origin_instance, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  5899. RenderingServer::get_singleton()->instance_geometry_set_cast_shadows_setting(origin_instance, RS::SHADOW_CASTING_SETTING_OFF);
  5900. Ref<Shader> grid_shader = memnew(Shader);
  5901. grid_shader->set_code(R"(
  5902. // 3D editor grid shader.
  5903. shader_type spatial;
  5904. render_mode unshaded, fog_disabled;
  5905. uniform bool orthogonal;
  5906. uniform float grid_size;
  5907. void vertex() {
  5908. // From FLAG_SRGB_VERTEX_COLOR.
  5909. if (!OUTPUT_IS_SRGB) {
  5910. COLOR.rgb = mix(pow((COLOR.rgb + vec3(0.055)) * (1.0 / (1.0 + 0.055)), vec3(2.4)), COLOR.rgb * (1.0 / 12.92), lessThan(COLOR.rgb, vec3(0.04045)));
  5911. }
  5912. }
  5913. void fragment() {
  5914. ALBEDO = COLOR.rgb;
  5915. vec3 dir = orthogonal ? -vec3(0, 0, 1) : VIEW;
  5916. float angle_fade = abs(dot(dir, NORMAL));
  5917. angle_fade = smoothstep(0.05, 0.2, angle_fade);
  5918. vec3 world_pos = (INV_VIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
  5919. vec3 world_normal = (INV_VIEW_MATRIX * vec4(NORMAL, 0.0)).xyz;
  5920. vec3 camera_world_pos = INV_VIEW_MATRIX[3].xyz;
  5921. vec3 camera_world_pos_on_plane = camera_world_pos * (1.0 - world_normal);
  5922. float dist_fade = 1.0 - (distance(world_pos, camera_world_pos_on_plane) / grid_size);
  5923. dist_fade = smoothstep(0.02, 0.3, dist_fade);
  5924. ALPHA = COLOR.a * dist_fade * angle_fade;
  5925. }
  5926. )");
  5927. for (int i = 0; i < 3; i++) {
  5928. grid_mat[i].instantiate();
  5929. grid_mat[i]->set_shader(grid_shader);
  5930. }
  5931. grid_enable[0] = EDITOR_GET("editors/3d/grid_xy_plane");
  5932. grid_enable[1] = EDITOR_GET("editors/3d/grid_yz_plane");
  5933. grid_enable[2] = EDITOR_GET("editors/3d/grid_xz_plane");
  5934. grid_visible[0] = grid_enable[0];
  5935. grid_visible[1] = grid_enable[1];
  5936. grid_visible[2] = grid_enable[2];
  5937. _init_grid();
  5938. }
  5939. {
  5940. //move gizmo
  5941. // Inverted zxy.
  5942. Vector3 ivec = Vector3(0, 0, -1);
  5943. Vector3 nivec = Vector3(-1, -1, 0);
  5944. Vector3 ivec2 = Vector3(-1, 0, 0);
  5945. Vector3 ivec3 = Vector3(0, -1, 0);
  5946. for (int i = 0; i < 3; i++) {
  5947. Color col;
  5948. switch (i) {
  5949. case 0:
  5950. col = get_theme_color(SNAME("axis_x_color"), EditorStringName(Editor));
  5951. break;
  5952. case 1:
  5953. col = get_theme_color(SNAME("axis_y_color"), EditorStringName(Editor));
  5954. break;
  5955. case 2:
  5956. col = get_theme_color(SNAME("axis_z_color"), EditorStringName(Editor));
  5957. break;
  5958. default:
  5959. col = Color();
  5960. break;
  5961. }
  5962. col.a = EDITOR_GET("editors/3d/manipulator_gizmo_opacity");
  5963. move_gizmo[i].instantiate();
  5964. move_plane_gizmo[i].instantiate();
  5965. rotate_gizmo[i].instantiate();
  5966. scale_gizmo[i].instantiate();
  5967. scale_plane_gizmo[i].instantiate();
  5968. axis_gizmo[i].instantiate();
  5969. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  5970. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  5971. mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  5972. mat->set_on_top_of_alpha();
  5973. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  5974. mat->set_albedo(col);
  5975. gizmo_color[i] = mat;
  5976. Ref<StandardMaterial3D> mat_hl = mat->duplicate();
  5977. const Color albedo = col.from_hsv(col.get_h(), 0.25, 1.0, 1);
  5978. mat_hl->set_albedo(albedo);
  5979. gizmo_color_hl[i] = mat_hl;
  5980. //translate
  5981. {
  5982. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  5983. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  5984. // Arrow profile
  5985. const int arrow_points = 5;
  5986. Vector3 arrow[5] = {
  5987. nivec * 0.0 + ivec * 0.0,
  5988. nivec * 0.01 + ivec * 0.0,
  5989. nivec * 0.01 + ivec * GIZMO_ARROW_OFFSET,
  5990. nivec * 0.065 + ivec * GIZMO_ARROW_OFFSET,
  5991. nivec * 0.0 + ivec * (GIZMO_ARROW_OFFSET + GIZMO_ARROW_SIZE),
  5992. };
  5993. int arrow_sides = 16;
  5994. const real_t arrow_sides_step = Math_TAU / arrow_sides;
  5995. for (int k = 0; k < arrow_sides; k++) {
  5996. Basis ma(ivec, k * arrow_sides_step);
  5997. Basis mb(ivec, (k + 1) * arrow_sides_step);
  5998. for (int j = 0; j < arrow_points - 1; j++) {
  5999. Vector3 points[4] = {
  6000. ma.xform(arrow[j]),
  6001. mb.xform(arrow[j]),
  6002. mb.xform(arrow[j + 1]),
  6003. ma.xform(arrow[j + 1]),
  6004. };
  6005. surftool->add_vertex(points[0]);
  6006. surftool->add_vertex(points[1]);
  6007. surftool->add_vertex(points[2]);
  6008. surftool->add_vertex(points[0]);
  6009. surftool->add_vertex(points[2]);
  6010. surftool->add_vertex(points[3]);
  6011. }
  6012. }
  6013. surftool->set_material(mat);
  6014. surftool->commit(move_gizmo[i]);
  6015. }
  6016. // Plane Translation
  6017. {
  6018. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  6019. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  6020. Vector3 vec = ivec2 - ivec3;
  6021. Vector3 plane[4] = {
  6022. vec * GIZMO_PLANE_DST,
  6023. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  6024. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  6025. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  6026. };
  6027. Basis ma(ivec, Math_PI / 2);
  6028. Vector3 points[4] = {
  6029. ma.xform(plane[0]),
  6030. ma.xform(plane[1]),
  6031. ma.xform(plane[2]),
  6032. ma.xform(plane[3]),
  6033. };
  6034. surftool->add_vertex(points[0]);
  6035. surftool->add_vertex(points[1]);
  6036. surftool->add_vertex(points[2]);
  6037. surftool->add_vertex(points[0]);
  6038. surftool->add_vertex(points[2]);
  6039. surftool->add_vertex(points[3]);
  6040. Ref<StandardMaterial3D> plane_mat = memnew(StandardMaterial3D);
  6041. plane_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  6042. plane_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  6043. plane_mat->set_on_top_of_alpha();
  6044. plane_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  6045. plane_mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
  6046. plane_mat->set_albedo(col);
  6047. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  6048. surftool->set_material(plane_mat);
  6049. surftool->commit(move_plane_gizmo[i]);
  6050. Ref<StandardMaterial3D> plane_mat_hl = plane_mat->duplicate();
  6051. plane_mat_hl->set_albedo(albedo);
  6052. plane_gizmo_color_hl[i] = plane_mat_hl; // needed, so we can draw planes from both sides
  6053. }
  6054. // Rotate
  6055. {
  6056. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  6057. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  6058. int n = 128; // number of circle segments
  6059. int m = 3; // number of thickness segments
  6060. real_t step = Math_TAU / n;
  6061. for (int j = 0; j < n; ++j) {
  6062. Basis basis = Basis(ivec, j * step);
  6063. Vector3 vertex = basis.xform(ivec2 * GIZMO_CIRCLE_SIZE);
  6064. for (int k = 0; k < m; ++k) {
  6065. Vector2 ofs = Vector2(Math::cos((Math_TAU * k) / m), Math::sin((Math_TAU * k) / m));
  6066. Vector3 normal = ivec * ofs.x + ivec2 * ofs.y;
  6067. surftool->set_normal(basis.xform(normal));
  6068. surftool->add_vertex(vertex);
  6069. }
  6070. }
  6071. for (int j = 0; j < n; ++j) {
  6072. for (int k = 0; k < m; ++k) {
  6073. int current_ring = j * m;
  6074. int next_ring = ((j + 1) % n) * m;
  6075. int current_segment = k;
  6076. int next_segment = (k + 1) % m;
  6077. surftool->add_index(current_ring + next_segment);
  6078. surftool->add_index(current_ring + current_segment);
  6079. surftool->add_index(next_ring + current_segment);
  6080. surftool->add_index(next_ring + current_segment);
  6081. surftool->add_index(next_ring + next_segment);
  6082. surftool->add_index(current_ring + next_segment);
  6083. }
  6084. }
  6085. Ref<Shader> rotate_shader = memnew(Shader);
  6086. rotate_shader->set_code(R"(
  6087. // 3D editor rotation manipulator gizmo shader.
  6088. shader_type spatial;
  6089. render_mode unshaded, depth_test_disabled, fog_disabled;
  6090. uniform vec4 albedo;
  6091. mat3 orthonormalize(mat3 m) {
  6092. vec3 x = normalize(m[0]);
  6093. vec3 y = normalize(m[1] - x * dot(x, m[1]));
  6094. vec3 z = m[2] - x * dot(x, m[2]);
  6095. z = normalize(z - y * (dot(y, m[2])));
  6096. return mat3(x,y,z);
  6097. }
  6098. void vertex() {
  6099. mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX));
  6100. vec3 n = mv * VERTEX;
  6101. float orientation = dot(vec3(0.0, 0.0, -1.0), n);
  6102. if (orientation <= 0.005) {
  6103. VERTEX += NORMAL * 0.02;
  6104. }
  6105. }
  6106. void fragment() {
  6107. ALBEDO = albedo.rgb;
  6108. ALPHA = albedo.a;
  6109. }
  6110. )");
  6111. Ref<ShaderMaterial> rotate_mat = memnew(ShaderMaterial);
  6112. rotate_mat->set_render_priority(Material::RENDER_PRIORITY_MAX);
  6113. rotate_mat->set_shader(rotate_shader);
  6114. rotate_mat->set_shader_parameter("albedo", col);
  6115. rotate_gizmo_color[i] = rotate_mat;
  6116. Array arrays = surftool->commit_to_arrays();
  6117. rotate_gizmo[i]->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
  6118. rotate_gizmo[i]->surface_set_material(0, rotate_mat);
  6119. Ref<ShaderMaterial> rotate_mat_hl = rotate_mat->duplicate();
  6120. rotate_mat_hl->set_shader_parameter("albedo", albedo);
  6121. rotate_gizmo_color_hl[i] = rotate_mat_hl;
  6122. if (i == 2) { // Rotation white outline
  6123. Ref<ShaderMaterial> border_mat = rotate_mat->duplicate();
  6124. Ref<Shader> border_shader = memnew(Shader);
  6125. border_shader->set_code(R"(
  6126. // 3D editor rotation manipulator gizmo shader (white outline).
  6127. shader_type spatial;
  6128. render_mode unshaded, depth_test_disabled, fog_disabled;
  6129. uniform vec4 albedo;
  6130. mat3 orthonormalize(mat3 m) {
  6131. vec3 x = normalize(m[0]);
  6132. vec3 y = normalize(m[1] - x * dot(x, m[1]));
  6133. vec3 z = m[2] - x * dot(x, m[2]);
  6134. z = normalize(z - y * (dot(y, m[2])));
  6135. return mat3(x, y, z);
  6136. }
  6137. void vertex() {
  6138. mat3 mv = orthonormalize(mat3(MODELVIEW_MATRIX));
  6139. mv = inverse(mv);
  6140. VERTEX += NORMAL * 0.008;
  6141. vec3 camera_dir_local = mv * vec3(0.0, 0.0, 1.0);
  6142. vec3 camera_up_local = mv * vec3(0.0, 1.0, 0.0);
  6143. mat3 rotation_matrix = mat3(cross(camera_dir_local, camera_up_local), camera_up_local, camera_dir_local);
  6144. VERTEX = rotation_matrix * VERTEX;
  6145. }
  6146. void fragment() {
  6147. ALBEDO = albedo.rgb;
  6148. ALPHA = albedo.a;
  6149. }
  6150. )");
  6151. border_mat->set_shader(border_shader);
  6152. border_mat->set_shader_parameter("albedo", Color(0.75, 0.75, 0.75, col.a / 3.0));
  6153. rotate_gizmo[3].instantiate();
  6154. rotate_gizmo[3]->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, arrays);
  6155. rotate_gizmo[3]->surface_set_material(0, border_mat);
  6156. }
  6157. }
  6158. // Scale
  6159. {
  6160. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  6161. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  6162. // Cube arrow profile
  6163. const int arrow_points = 6;
  6164. Vector3 arrow[6] = {
  6165. nivec * 0.0 + ivec * 0.0,
  6166. nivec * 0.01 + ivec * 0.0,
  6167. nivec * 0.01 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  6168. nivec * 0.07 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  6169. nivec * 0.07 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  6170. nivec * 0.0 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  6171. };
  6172. int arrow_sides = 4;
  6173. const real_t arrow_sides_step = Math_TAU / arrow_sides;
  6174. for (int k = 0; k < 4; k++) {
  6175. Basis ma(ivec, k * arrow_sides_step);
  6176. Basis mb(ivec, (k + 1) * arrow_sides_step);
  6177. for (int j = 0; j < arrow_points - 1; j++) {
  6178. Vector3 points[4] = {
  6179. ma.xform(arrow[j]),
  6180. mb.xform(arrow[j]),
  6181. mb.xform(arrow[j + 1]),
  6182. ma.xform(arrow[j + 1]),
  6183. };
  6184. surftool->add_vertex(points[0]);
  6185. surftool->add_vertex(points[1]);
  6186. surftool->add_vertex(points[2]);
  6187. surftool->add_vertex(points[0]);
  6188. surftool->add_vertex(points[2]);
  6189. surftool->add_vertex(points[3]);
  6190. }
  6191. }
  6192. surftool->set_material(mat);
  6193. surftool->commit(scale_gizmo[i]);
  6194. }
  6195. // Plane Scale
  6196. {
  6197. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  6198. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  6199. Vector3 vec = ivec2 - ivec3;
  6200. Vector3 plane[4] = {
  6201. vec * GIZMO_PLANE_DST,
  6202. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  6203. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  6204. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  6205. };
  6206. Basis ma(ivec, Math_PI / 2);
  6207. Vector3 points[4] = {
  6208. ma.xform(plane[0]),
  6209. ma.xform(plane[1]),
  6210. ma.xform(plane[2]),
  6211. ma.xform(plane[3]),
  6212. };
  6213. surftool->add_vertex(points[0]);
  6214. surftool->add_vertex(points[1]);
  6215. surftool->add_vertex(points[2]);
  6216. surftool->add_vertex(points[0]);
  6217. surftool->add_vertex(points[2]);
  6218. surftool->add_vertex(points[3]);
  6219. Ref<StandardMaterial3D> plane_mat = memnew(StandardMaterial3D);
  6220. plane_mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  6221. plane_mat->set_flag(StandardMaterial3D::FLAG_DISABLE_FOG, true);
  6222. plane_mat->set_on_top_of_alpha();
  6223. plane_mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  6224. plane_mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
  6225. plane_mat->set_albedo(col);
  6226. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  6227. surftool->set_material(plane_mat);
  6228. surftool->commit(scale_plane_gizmo[i]);
  6229. Ref<StandardMaterial3D> plane_mat_hl = plane_mat->duplicate();
  6230. plane_mat_hl->set_albedo(col.from_hsv(col.get_h(), 0.25, 1.0, 1));
  6231. plane_gizmo_color_hl[i] = plane_mat_hl; // needed, so we can draw planes from both sides
  6232. }
  6233. // Lines to visualize transforms locked to an axis/plane
  6234. {
  6235. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  6236. surftool->begin(Mesh::PRIMITIVE_LINE_STRIP);
  6237. Vector3 vec;
  6238. vec[i] = 1;
  6239. // line extending through infinity(ish)
  6240. surftool->add_vertex(vec * -1048576);
  6241. surftool->add_vertex(Vector3());
  6242. surftool->add_vertex(vec * 1048576);
  6243. surftool->set_material(mat_hl);
  6244. surftool->commit(axis_gizmo[i]);
  6245. }
  6246. }
  6247. }
  6248. _generate_selection_boxes();
  6249. }
  6250. void Node3DEditor::_update_gizmos_menu() {
  6251. gizmos_menu->clear();
  6252. for (int i = 0; i < gizmo_plugins_by_name.size(); ++i) {
  6253. if (!gizmo_plugins_by_name[i]->can_be_hidden()) {
  6254. continue;
  6255. }
  6256. String plugin_name = gizmo_plugins_by_name[i]->get_gizmo_name();
  6257. const int plugin_state = gizmo_plugins_by_name[i]->get_state();
  6258. gizmos_menu->add_multistate_item(plugin_name, 3, plugin_state, i);
  6259. const int idx = gizmos_menu->get_item_index(i);
  6260. gizmos_menu->set_item_tooltip(
  6261. idx,
  6262. TTR("Click to toggle between visibility states.\n\nOpen eye: Gizmo is visible.\nClosed eye: Gizmo is hidden.\nHalf-open eye: Gizmo is also visible through opaque surfaces (\"x-ray\")."));
  6263. switch (plugin_state) {
  6264. case EditorNode3DGizmoPlugin::VISIBLE:
  6265. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_visible")));
  6266. break;
  6267. case EditorNode3DGizmoPlugin::ON_TOP:
  6268. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_xray")));
  6269. break;
  6270. case EditorNode3DGizmoPlugin::HIDDEN:
  6271. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_hidden")));
  6272. break;
  6273. }
  6274. }
  6275. }
  6276. void Node3DEditor::_update_gizmos_menu_theme() {
  6277. for (int i = 0; i < gizmo_plugins_by_name.size(); ++i) {
  6278. if (!gizmo_plugins_by_name[i]->can_be_hidden()) {
  6279. continue;
  6280. }
  6281. const int plugin_state = gizmo_plugins_by_name[i]->get_state();
  6282. const int idx = gizmos_menu->get_item_index(i);
  6283. switch (plugin_state) {
  6284. case EditorNode3DGizmoPlugin::VISIBLE:
  6285. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_visible")));
  6286. break;
  6287. case EditorNode3DGizmoPlugin::ON_TOP:
  6288. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_xray")));
  6289. break;
  6290. case EditorNode3DGizmoPlugin::HIDDEN:
  6291. gizmos_menu->set_item_icon(idx, gizmos_menu->get_theme_icon(SNAME("visibility_hidden")));
  6292. break;
  6293. }
  6294. }
  6295. }
  6296. void Node3DEditor::_init_grid() {
  6297. if (!grid_enabled) {
  6298. return;
  6299. }
  6300. Camera3D *camera = get_editor_viewport(0)->camera;
  6301. Vector3 camera_position = camera->get_position();
  6302. if (camera_position == Vector3()) {
  6303. return; // Camera3D is invalid, don't draw the grid.
  6304. }
  6305. bool orthogonal = camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL;
  6306. static LocalVector<Color> grid_colors[3];
  6307. static LocalVector<Vector3> grid_points[3];
  6308. static LocalVector<Vector3> grid_normals[3];
  6309. for (uint32_t n = 0; n < 3; n++) {
  6310. grid_colors[n].clear();
  6311. grid_points[n].clear();
  6312. grid_normals[n].clear();
  6313. }
  6314. Color primary_grid_color = EDITOR_GET("editors/3d/primary_grid_color");
  6315. Color secondary_grid_color = EDITOR_GET("editors/3d/secondary_grid_color");
  6316. int grid_size = EDITOR_GET("editors/3d/grid_size");
  6317. int primary_grid_steps = EDITOR_GET("editors/3d/primary_grid_steps");
  6318. // Which grid planes are enabled? Which should we generate?
  6319. grid_enable[0] = grid_visible[0] = orthogonal || EDITOR_GET("editors/3d/grid_xy_plane");
  6320. grid_enable[1] = grid_visible[1] = orthogonal || EDITOR_GET("editors/3d/grid_yz_plane");
  6321. grid_enable[2] = grid_visible[2] = orthogonal || EDITOR_GET("editors/3d/grid_xz_plane");
  6322. // Offsets division_level for bigger or smaller grids.
  6323. // Default value is -0.2. -1.0 gives Blender-like behavior, 0.5 gives huge grids.
  6324. real_t division_level_bias = EDITOR_GET("editors/3d/grid_division_level_bias");
  6325. // Default largest grid size is 8^2 (default value is 2) when primary_grid_steps is 8 (64m apart, so primary grid lines are 512m apart).
  6326. int division_level_max = EDITOR_GET("editors/3d/grid_division_level_max");
  6327. // Default smallest grid size is 8^0 (default value is 0) when primary_grid_steps is 8.
  6328. int division_level_min = EDITOR_GET("editors/3d/grid_division_level_min");
  6329. ERR_FAIL_COND_MSG(division_level_max < division_level_min, "The 3D grid's maximum division level cannot be lower than its minimum division level.");
  6330. if (primary_grid_steps != 10) { // Log10 of 10 is 1.
  6331. // Change of base rule, divide by ln(10).
  6332. real_t div = Math::log((real_t)primary_grid_steps) / (real_t)2.302585092994045901094;
  6333. // Truncation (towards zero) is intentional.
  6334. division_level_max = (int)(division_level_max / div);
  6335. division_level_min = (int)(division_level_min / div);
  6336. }
  6337. for (int a = 0; a < 3; a++) {
  6338. if (!grid_enable[a]) {
  6339. continue; // If this grid plane is disabled, skip generation.
  6340. }
  6341. int b = (a + 1) % 3;
  6342. int c = (a + 2) % 3;
  6343. Vector3 normal;
  6344. normal[c] = 1.0;
  6345. real_t camera_distance = Math::abs(camera_position[c]);
  6346. if (orthogonal) {
  6347. camera_distance = camera->get_size() / 2.0;
  6348. Vector3 camera_direction = -camera->get_global_transform().get_basis().get_column(2);
  6349. Plane grid_plane = Plane(normal);
  6350. Vector3 intersection;
  6351. if (grid_plane.intersects_ray(camera_position, camera_direction, &intersection)) {
  6352. camera_position = intersection;
  6353. }
  6354. }
  6355. real_t division_level = Math::log(Math::abs(camera_distance)) / Math::log((double)primary_grid_steps) + division_level_bias;
  6356. real_t clamped_division_level = CLAMP(division_level, division_level_min, division_level_max);
  6357. real_t division_level_floored = Math::floor(clamped_division_level);
  6358. real_t division_level_decimals = clamped_division_level - division_level_floored;
  6359. real_t small_step_size = Math::pow(primary_grid_steps, division_level_floored);
  6360. real_t large_step_size = small_step_size * primary_grid_steps;
  6361. real_t center_a = large_step_size * (int)(camera_position[a] / large_step_size);
  6362. real_t center_b = large_step_size * (int)(camera_position[b] / large_step_size);
  6363. real_t bgn_a = center_a - grid_size * small_step_size;
  6364. real_t end_a = center_a + grid_size * small_step_size;
  6365. real_t bgn_b = center_b - grid_size * small_step_size;
  6366. real_t end_b = center_b + grid_size * small_step_size;
  6367. real_t fade_size = Math::pow(primary_grid_steps, division_level - 1.0);
  6368. real_t min_fade_size = Math::pow(primary_grid_steps, float(division_level_min));
  6369. real_t max_fade_size = Math::pow(primary_grid_steps, float(division_level_max));
  6370. fade_size = CLAMP(fade_size, min_fade_size, max_fade_size);
  6371. real_t grid_fade_size = (grid_size - primary_grid_steps) * fade_size;
  6372. grid_mat[c]->set_shader_parameter("grid_size", grid_fade_size);
  6373. grid_mat[c]->set_shader_parameter("orthogonal", orthogonal);
  6374. LocalVector<Vector3> &ref_grid = grid_points[c];
  6375. LocalVector<Vector3> &ref_grid_normals = grid_normals[c];
  6376. LocalVector<Color> &ref_grid_colors = grid_colors[c];
  6377. // Count our elements same as code below it.
  6378. int expected_size = 0;
  6379. for (int i = -grid_size; i <= grid_size; i++) {
  6380. const real_t position_a = center_a + i * small_step_size;
  6381. const real_t position_b = center_b + i * small_step_size;
  6382. // Don't draw lines over the origin if it's enabled.
  6383. if (!(origin_enabled && Math::is_zero_approx(position_a))) {
  6384. expected_size += 2;
  6385. }
  6386. if (!(origin_enabled && Math::is_zero_approx(position_b))) {
  6387. expected_size += 2;
  6388. }
  6389. }
  6390. int idx = 0;
  6391. ref_grid.resize(expected_size);
  6392. ref_grid_normals.resize(expected_size);
  6393. ref_grid_colors.resize(expected_size);
  6394. // In each iteration of this loop, draw one line in each direction (so two lines per loop, in each if statement).
  6395. for (int i = -grid_size; i <= grid_size; i++) {
  6396. Color line_color;
  6397. // Is this a primary line? Set the appropriate color.
  6398. if (i % primary_grid_steps == 0) {
  6399. line_color = primary_grid_color.lerp(secondary_grid_color, division_level_decimals);
  6400. } else {
  6401. line_color = secondary_grid_color;
  6402. line_color.a = line_color.a * (1 - division_level_decimals);
  6403. }
  6404. real_t position_a = center_a + i * small_step_size;
  6405. real_t position_b = center_b + i * small_step_size;
  6406. // Don't draw lines over the origin if it's enabled.
  6407. if (!(origin_enabled && Math::is_zero_approx(position_a))) {
  6408. Vector3 line_bgn;
  6409. Vector3 line_end;
  6410. line_bgn[a] = position_a;
  6411. line_end[a] = position_a;
  6412. line_bgn[b] = bgn_b;
  6413. line_end[b] = end_b;
  6414. ref_grid[idx] = line_bgn;
  6415. ref_grid[idx + 1] = line_end;
  6416. ref_grid_colors[idx] = line_color;
  6417. ref_grid_colors[idx + 1] = line_color;
  6418. ref_grid_normals[idx] = normal;
  6419. ref_grid_normals[idx + 1] = normal;
  6420. idx += 2;
  6421. }
  6422. if (!(origin_enabled && Math::is_zero_approx(position_b))) {
  6423. Vector3 line_bgn;
  6424. Vector3 line_end;
  6425. line_bgn[b] = position_b;
  6426. line_end[b] = position_b;
  6427. line_bgn[a] = bgn_a;
  6428. line_end[a] = end_a;
  6429. ref_grid[idx] = line_bgn;
  6430. ref_grid[idx + 1] = line_end;
  6431. ref_grid_colors[idx] = line_color;
  6432. ref_grid_colors[idx + 1] = line_color;
  6433. ref_grid_normals[idx] = normal;
  6434. ref_grid_normals[idx + 1] = normal;
  6435. idx += 2;
  6436. }
  6437. }
  6438. // Create a mesh from the pushed vector points and colors.
  6439. grid[c] = RenderingServer::get_singleton()->mesh_create();
  6440. Array d;
  6441. d.resize(RS::ARRAY_MAX);
  6442. d[RenderingServer::ARRAY_VERTEX] = (Vector<Vector3>)grid_points[c];
  6443. d[RenderingServer::ARRAY_COLOR] = (Vector<Color>)grid_colors[c];
  6444. d[RenderingServer::ARRAY_NORMAL] = (Vector<Vector3>)grid_normals[c];
  6445. RenderingServer::get_singleton()->mesh_add_surface_from_arrays(grid[c], RenderingServer::PRIMITIVE_LINES, d);
  6446. RenderingServer::get_singleton()->mesh_surface_set_material(grid[c], 0, grid_mat[c]->get_rid());
  6447. grid_instance[c] = RenderingServer::get_singleton()->instance_create2(grid[c], get_tree()->get_root()->get_world_3d()->get_scenario());
  6448. // Yes, the end of this line is supposed to be a.
  6449. RenderingServer::get_singleton()->instance_set_visible(grid_instance[c], grid_visible[a]);
  6450. RenderingServer::get_singleton()->instance_geometry_set_cast_shadows_setting(grid_instance[c], RS::SHADOW_CASTING_SETTING_OFF);
  6451. RS::get_singleton()->instance_set_layer_mask(grid_instance[c], 1 << Node3DEditorViewport::GIZMO_GRID_LAYER);
  6452. RS::get_singleton()->instance_geometry_set_flag(grid_instance[c], RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  6453. RS::get_singleton()->instance_geometry_set_flag(grid_instance[c], RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  6454. }
  6455. }
  6456. void Node3DEditor::_finish_indicators() {
  6457. RenderingServer::get_singleton()->free(origin_instance);
  6458. RenderingServer::get_singleton()->free(origin_multimesh);
  6459. RenderingServer::get_singleton()->free(origin_mesh);
  6460. _finish_grid();
  6461. }
  6462. void Node3DEditor::_finish_grid() {
  6463. for (int i = 0; i < 3; i++) {
  6464. RenderingServer::get_singleton()->free(grid_instance[i]);
  6465. RenderingServer::get_singleton()->free(grid[i]);
  6466. }
  6467. }
  6468. void Node3DEditor::update_grid() {
  6469. const Camera3D::ProjectionType current_projection = viewports[0]->camera->get_projection();
  6470. if (current_projection != grid_camera_last_update_perspective) {
  6471. grid_init_draw = false; // redraw
  6472. grid_camera_last_update_perspective = current_projection;
  6473. }
  6474. // Gets a orthogonal or perspective position correctly (for the grid comparison)
  6475. const Vector3 camera_position = get_editor_viewport(0)->camera->get_position();
  6476. if (!grid_init_draw || grid_camera_last_update_position.distance_squared_to(camera_position) >= 100.0f) {
  6477. _finish_grid();
  6478. _init_grid();
  6479. grid_init_draw = true;
  6480. grid_camera_last_update_position = camera_position;
  6481. }
  6482. }
  6483. void Node3DEditor::_selection_changed() {
  6484. _refresh_menu_icons();
  6485. if (selected && editor_selection->get_selected_node_list().size() != 1) {
  6486. Vector<Ref<Node3DGizmo>> gizmos = selected->get_gizmos();
  6487. for (int i = 0; i < gizmos.size(); i++) {
  6488. Ref<EditorNode3DGizmo> seg = gizmos[i];
  6489. if (!seg.is_valid()) {
  6490. continue;
  6491. }
  6492. seg->set_selected(false);
  6493. }
  6494. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected);
  6495. if (se) {
  6496. se->gizmo.unref();
  6497. se->subgizmos.clear();
  6498. }
  6499. selected->update_gizmos();
  6500. selected = nullptr;
  6501. }
  6502. update_transform_gizmo();
  6503. }
  6504. void Node3DEditor::_refresh_menu_icons() {
  6505. bool all_locked = true;
  6506. bool all_grouped = true;
  6507. bool has_node3d_item = false;
  6508. List<Node *> &selection = editor_selection->get_selected_node_list();
  6509. if (selection.is_empty()) {
  6510. all_locked = false;
  6511. all_grouped = false;
  6512. } else {
  6513. for (Node *E : selection) {
  6514. Node3D *node = Object::cast_to<Node3D>(E);
  6515. if (node) {
  6516. if (all_locked && !node->has_meta("_edit_lock_")) {
  6517. all_locked = false;
  6518. }
  6519. if (all_grouped && !node->has_meta("_edit_group_")) {
  6520. all_grouped = false;
  6521. }
  6522. has_node3d_item = true;
  6523. }
  6524. if (!all_locked && !all_grouped) {
  6525. break;
  6526. }
  6527. }
  6528. }
  6529. all_locked = all_locked && has_node3d_item;
  6530. all_grouped = all_grouped && has_node3d_item;
  6531. tool_button[TOOL_LOCK_SELECTED]->set_visible(!all_locked);
  6532. tool_button[TOOL_LOCK_SELECTED]->set_disabled(!has_node3d_item);
  6533. tool_button[TOOL_UNLOCK_SELECTED]->set_visible(all_locked);
  6534. tool_button[TOOL_UNLOCK_SELECTED]->set_disabled(!has_node3d_item);
  6535. tool_button[TOOL_GROUP_SELECTED]->set_visible(!all_grouped);
  6536. tool_button[TOOL_GROUP_SELECTED]->set_disabled(!has_node3d_item);
  6537. tool_button[TOOL_UNGROUP_SELECTED]->set_visible(all_grouped);
  6538. tool_button[TOOL_UNGROUP_SELECTED]->set_disabled(!has_node3d_item);
  6539. }
  6540. template <typename T>
  6541. HashSet<T *> _get_child_nodes(Node *parent_node) {
  6542. HashSet<T *> nodes = HashSet<T *>();
  6543. T *node = Node::cast_to<T>(parent_node);
  6544. if (node) {
  6545. nodes.insert(node);
  6546. }
  6547. for (int i = 0; i < parent_node->get_child_count(); i++) {
  6548. Node *child_node = parent_node->get_child(i);
  6549. HashSet<T *> child_nodes = _get_child_nodes<T>(child_node);
  6550. for (T *I : child_nodes) {
  6551. nodes.insert(I);
  6552. }
  6553. }
  6554. return nodes;
  6555. }
  6556. HashSet<RID> _get_physics_bodies_rid(Node *node) {
  6557. HashSet<RID> rids = HashSet<RID>();
  6558. PhysicsBody3D *pb = Node::cast_to<PhysicsBody3D>(node);
  6559. if (pb) {
  6560. rids.insert(pb->get_rid());
  6561. }
  6562. HashSet<PhysicsBody3D *> child_nodes = _get_child_nodes<PhysicsBody3D>(node);
  6563. for (const PhysicsBody3D *I : child_nodes) {
  6564. rids.insert(I->get_rid());
  6565. }
  6566. return rids;
  6567. }
  6568. void Node3DEditor::snap_selected_nodes_to_floor() {
  6569. do_snap_selected_nodes_to_floor = true;
  6570. }
  6571. void Node3DEditor::_snap_selected_nodes_to_floor() {
  6572. const List<Node *> &selection = editor_selection->get_selected_node_list();
  6573. Dictionary snap_data;
  6574. for (Node *E : selection) {
  6575. Node3D *sp = Object::cast_to<Node3D>(E);
  6576. if (sp) {
  6577. Vector3 from;
  6578. Vector3 position_offset;
  6579. // Priorities for snapping to floor are CollisionShapes, VisualInstances and then origin
  6580. HashSet<VisualInstance3D *> vi = _get_child_nodes<VisualInstance3D>(sp);
  6581. HashSet<CollisionShape3D *> cs = _get_child_nodes<CollisionShape3D>(sp);
  6582. bool found_valid_shape = false;
  6583. if (cs.size()) {
  6584. AABB aabb;
  6585. HashSet<CollisionShape3D *>::Iterator I = cs.begin();
  6586. if ((*I)->get_shape().is_valid()) {
  6587. CollisionShape3D *collision_shape = *cs.begin();
  6588. aabb = collision_shape->get_global_transform().xform(collision_shape->get_shape()->get_debug_mesh()->get_aabb());
  6589. found_valid_shape = true;
  6590. }
  6591. for (++I; I; ++I) {
  6592. CollisionShape3D *col_shape = *I;
  6593. if (col_shape->get_shape().is_valid()) {
  6594. aabb.merge_with(col_shape->get_global_transform().xform(col_shape->get_shape()->get_debug_mesh()->get_aabb()));
  6595. found_valid_shape = true;
  6596. }
  6597. }
  6598. if (found_valid_shape) {
  6599. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  6600. from = aabb.position + size;
  6601. position_offset.y = from.y - sp->get_global_transform().origin.y;
  6602. }
  6603. }
  6604. if (!found_valid_shape && vi.size()) {
  6605. VisualInstance3D *begin = *vi.begin();
  6606. AABB aabb = begin->get_global_transform().xform(begin->get_aabb());
  6607. for (const VisualInstance3D *I : vi) {
  6608. aabb.merge_with(I->get_global_transform().xform(I->get_aabb()));
  6609. }
  6610. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  6611. from = aabb.position + size;
  6612. position_offset.y = from.y - sp->get_global_transform().origin.y;
  6613. } else if (!found_valid_shape) {
  6614. from = sp->get_global_transform().origin;
  6615. }
  6616. // We add a bit of margin to the from position to avoid it from snapping
  6617. // when the spatial is already on a floor and there's another floor under
  6618. // it
  6619. from = from + Vector3(0.0, 1, 0.0);
  6620. Dictionary d;
  6621. d["from"] = from;
  6622. d["position_offset"] = position_offset;
  6623. snap_data[sp] = d;
  6624. }
  6625. }
  6626. PhysicsDirectSpaceState3D *ss = get_tree()->get_root()->get_world_3d()->get_direct_space_state();
  6627. PhysicsDirectSpaceState3D::RayResult result;
  6628. Array keys = snap_data.keys();
  6629. // The maximum height an object can travel to be snapped
  6630. const float max_snap_height = 500.0;
  6631. // Will be set to `true` if at least one node from the selection was successfully snapped
  6632. bool snapped_to_floor = false;
  6633. if (keys.size()) {
  6634. // For snapping to be performed, there must be solid geometry under at least one of the selected nodes.
  6635. // We need to check this before snapping to register the undo/redo action only if needed.
  6636. for (int i = 0; i < keys.size(); i++) {
  6637. Node *node = Object::cast_to<Node>(keys[i]);
  6638. Node3D *sp = Object::cast_to<Node3D>(node);
  6639. Dictionary d = snap_data[node];
  6640. Vector3 from = d["from"];
  6641. Vector3 to = from - Vector3(0.0, max_snap_height, 0.0);
  6642. HashSet<RID> excluded = _get_physics_bodies_rid(sp);
  6643. PhysicsDirectSpaceState3D::RayParameters ray_params;
  6644. ray_params.from = from;
  6645. ray_params.to = to;
  6646. ray_params.exclude = excluded;
  6647. if (ss->intersect_ray(ray_params, result)) {
  6648. snapped_to_floor = true;
  6649. }
  6650. }
  6651. if (snapped_to_floor) {
  6652. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  6653. undo_redo->create_action(TTR("Snap Nodes to Floor"));
  6654. // Perform snapping if at least one node can be snapped
  6655. for (int i = 0; i < keys.size(); i++) {
  6656. Node *node = Object::cast_to<Node>(keys[i]);
  6657. Node3D *sp = Object::cast_to<Node3D>(node);
  6658. Dictionary d = snap_data[node];
  6659. Vector3 from = d["from"];
  6660. Vector3 to = from - Vector3(0.0, max_snap_height, 0.0);
  6661. HashSet<RID> excluded = _get_physics_bodies_rid(sp);
  6662. PhysicsDirectSpaceState3D::RayParameters ray_params;
  6663. ray_params.from = from;
  6664. ray_params.to = to;
  6665. ray_params.exclude = excluded;
  6666. if (ss->intersect_ray(ray_params, result)) {
  6667. Vector3 position_offset = d["position_offset"];
  6668. Transform3D new_transform = sp->get_global_transform();
  6669. new_transform.origin.y = result.position.y;
  6670. new_transform.origin = new_transform.origin - position_offset;
  6671. Node3D *parent = sp->get_parent_node_3d();
  6672. Transform3D new_local_xform = parent ? parent->get_global_transform().affine_inverse() * new_transform : new_transform;
  6673. undo_redo->add_do_method(sp, "set_transform", new_local_xform);
  6674. undo_redo->add_undo_method(sp, "set_transform", sp->get_transform());
  6675. }
  6676. }
  6677. undo_redo->commit_action();
  6678. } else {
  6679. EditorNode::get_singleton()->show_warning(TTR("Couldn't find a solid floor to snap the selection to."));
  6680. }
  6681. }
  6682. }
  6683. void Node3DEditor::shortcut_input(const Ref<InputEvent> &p_event) {
  6684. ERR_FAIL_COND(p_event.is_null());
  6685. if (!is_visible_in_tree()) {
  6686. return;
  6687. }
  6688. snap_key_enabled = Input::get_singleton()->is_key_pressed(Key::CTRL);
  6689. }
  6690. void Node3DEditor::_sun_environ_settings_pressed() {
  6691. Vector2 pos = sun_environ_settings->get_screen_position() + sun_environ_settings->get_size();
  6692. sun_environ_popup->set_position(pos - Vector2(sun_environ_popup->get_contents_minimum_size().width / 2, 0));
  6693. sun_environ_popup->reset_size();
  6694. sun_environ_popup->popup();
  6695. // Grabbing the focus is required for Shift modifier checking to be functional
  6696. // (when the Add sun/environment buttons are pressed).
  6697. sun_environ_popup->grab_focus();
  6698. }
  6699. void Node3DEditor::_add_sun_to_scene(bool p_already_added_environment) {
  6700. sun_environ_popup->hide();
  6701. if (!p_already_added_environment && world_env_count == 0 && Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  6702. // Prevent infinite feedback loop between the sun and environment methods.
  6703. _add_environment_to_scene(true);
  6704. }
  6705. Node *base = get_tree()->get_edited_scene_root();
  6706. if (!base) {
  6707. // Create a root node so we can add child nodes to it.
  6708. SceneTreeDock::get_singleton()->add_root_node(memnew(Node3D));
  6709. base = get_tree()->get_edited_scene_root();
  6710. }
  6711. ERR_FAIL_NULL(base);
  6712. Node *new_sun = preview_sun->duplicate();
  6713. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  6714. undo_redo->create_action(TTR("Add Preview Sun to Scene"));
  6715. undo_redo->add_do_method(base, "add_child", new_sun, true);
  6716. // Move to the beginning of the scene tree since more "global" nodes
  6717. // generally look better when placed at the top.
  6718. undo_redo->add_do_method(base, "move_child", new_sun, 0);
  6719. undo_redo->add_do_method(new_sun, "set_owner", base);
  6720. undo_redo->add_undo_method(base, "remove_child", new_sun);
  6721. undo_redo->add_do_reference(new_sun);
  6722. undo_redo->commit_action();
  6723. }
  6724. void Node3DEditor::_add_environment_to_scene(bool p_already_added_sun) {
  6725. sun_environ_popup->hide();
  6726. if (!p_already_added_sun && directional_light_count == 0 && Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  6727. // Prevent infinite feedback loop between the sun and environment methods.
  6728. _add_sun_to_scene(true);
  6729. }
  6730. Node *base = get_tree()->get_edited_scene_root();
  6731. if (!base) {
  6732. // Create a root node so we can add child nodes to it.
  6733. SceneTreeDock::get_singleton()->add_root_node(memnew(Node3D));
  6734. base = get_tree()->get_edited_scene_root();
  6735. }
  6736. ERR_FAIL_NULL(base);
  6737. WorldEnvironment *new_env = memnew(WorldEnvironment);
  6738. new_env->set_environment(preview_environment->get_environment()->duplicate(true));
  6739. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  6740. new_env->set_camera_attributes(preview_environment->get_camera_attributes()->duplicate(true));
  6741. }
  6742. EditorUndoRedoManager *undo_redo = EditorUndoRedoManager::get_singleton();
  6743. undo_redo->create_action(TTR("Add Preview Environment to Scene"));
  6744. undo_redo->add_do_method(base, "add_child", new_env, true);
  6745. // Move to the beginning of the scene tree since more "global" nodes
  6746. // generally look better when placed at the top.
  6747. undo_redo->add_do_method(base, "move_child", new_env, 0);
  6748. undo_redo->add_do_method(new_env, "set_owner", base);
  6749. undo_redo->add_undo_method(base, "remove_child", new_env);
  6750. undo_redo->add_do_reference(new_env);
  6751. undo_redo->commit_action();
  6752. }
  6753. void Node3DEditor::_update_theme() {
  6754. tool_button[TOOL_MODE_SELECT]->set_button_icon(get_editor_theme_icon(SNAME("ToolSelect")));
  6755. tool_button[TOOL_MODE_MOVE]->set_button_icon(get_editor_theme_icon(SNAME("ToolMove")));
  6756. tool_button[TOOL_MODE_ROTATE]->set_button_icon(get_editor_theme_icon(SNAME("ToolRotate")));
  6757. tool_button[TOOL_MODE_SCALE]->set_button_icon(get_editor_theme_icon(SNAME("ToolScale")));
  6758. tool_button[TOOL_MODE_LIST_SELECT]->set_button_icon(get_editor_theme_icon(SNAME("ListSelect")));
  6759. tool_button[TOOL_LOCK_SELECTED]->set_button_icon(get_editor_theme_icon(SNAME("Lock")));
  6760. tool_button[TOOL_UNLOCK_SELECTED]->set_button_icon(get_editor_theme_icon(SNAME("Unlock")));
  6761. tool_button[TOOL_GROUP_SELECTED]->set_button_icon(get_editor_theme_icon(SNAME("Group")));
  6762. tool_button[TOOL_UNGROUP_SELECTED]->set_button_icon(get_editor_theme_icon(SNAME("Ungroup")));
  6763. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_button_icon(get_editor_theme_icon(SNAME("Object")));
  6764. tool_option_button[TOOL_OPT_USE_SNAP]->set_button_icon(get_editor_theme_icon(SNAME("Snap")));
  6765. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_editor_theme_icon(SNAME("Panels1")));
  6766. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_editor_theme_icon(SNAME("Panels2")));
  6767. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_editor_theme_icon(SNAME("Panels2Alt")));
  6768. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_editor_theme_icon(SNAME("Panels3")));
  6769. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_editor_theme_icon(SNAME("Panels3Alt")));
  6770. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_editor_theme_icon(SNAME("Panels4")));
  6771. sun_button->set_button_icon(get_editor_theme_icon(SNAME("PreviewSun")));
  6772. environ_button->set_button_icon(get_editor_theme_icon(SNAME("PreviewEnvironment")));
  6773. sun_environ_settings->set_button_icon(get_editor_theme_icon(SNAME("GuiTabMenuHl")));
  6774. sun_title->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("title_font"), SNAME("Window")));
  6775. environ_title->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("title_font"), SNAME("Window")));
  6776. sun_color->set_custom_minimum_size(Size2(0, get_theme_constant(SNAME("color_picker_button_height"), EditorStringName(Editor))));
  6777. environ_sky_color->set_custom_minimum_size(Size2(0, get_theme_constant(SNAME("color_picker_button_height"), EditorStringName(Editor))));
  6778. environ_ground_color->set_custom_minimum_size(Size2(0, get_theme_constant(SNAME("color_picker_button_height"), EditorStringName(Editor))));
  6779. context_toolbar_panel->add_theme_style_override(SceneStringName(panel), get_theme_stylebox(SNAME("ContextualToolbar"), EditorStringName(EditorStyles)));
  6780. }
  6781. void Node3DEditor::_notification(int p_what) {
  6782. switch (p_what) {
  6783. case NOTIFICATION_READY: {
  6784. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  6785. _refresh_menu_icons();
  6786. get_tree()->connect("node_removed", callable_mp(this, &Node3DEditor::_node_removed));
  6787. get_tree()->connect("node_added", callable_mp(this, &Node3DEditor::_node_added));
  6788. SceneTreeDock::get_singleton()->get_tree_editor()->connect("node_changed", callable_mp(this, &Node3DEditor::_refresh_menu_icons));
  6789. editor_selection->connect("selection_changed", callable_mp(this, &Node3DEditor::_selection_changed));
  6790. _update_preview_environment();
  6791. sun_state->set_custom_minimum_size(sun_vb->get_combined_minimum_size());
  6792. environ_state->set_custom_minimum_size(environ_vb->get_combined_minimum_size());
  6793. ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &Node3DEditor::update_all_gizmos).bind(Variant()));
  6794. } break;
  6795. case NOTIFICATION_ENTER_TREE: {
  6796. _update_theme();
  6797. _register_all_gizmos();
  6798. _update_gizmos_menu();
  6799. _init_indicators();
  6800. update_all_gizmos();
  6801. } break;
  6802. case NOTIFICATION_EXIT_TREE: {
  6803. _finish_indicators();
  6804. } break;
  6805. case NOTIFICATION_THEME_CHANGED: {
  6806. _update_theme();
  6807. _update_gizmos_menu_theme();
  6808. sun_title->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("title_font"), SNAME("Window")));
  6809. environ_title->add_theme_font_override(SceneStringName(font), get_theme_font(SNAME("title_font"), SNAME("Window")));
  6810. } break;
  6811. case EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED: {
  6812. // Update grid color by rebuilding grid.
  6813. if (EditorSettings::get_singleton()->check_changed_settings_in_group("editors/3d")) {
  6814. _finish_grid();
  6815. _init_grid();
  6816. }
  6817. } break;
  6818. case NOTIFICATION_PHYSICS_PROCESS: {
  6819. if (do_snap_selected_nodes_to_floor) {
  6820. _snap_selected_nodes_to_floor();
  6821. do_snap_selected_nodes_to_floor = false;
  6822. }
  6823. }
  6824. }
  6825. }
  6826. bool Node3DEditor::is_subgizmo_selected(int p_id) {
  6827. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  6828. if (se) {
  6829. return se->subgizmos.has(p_id);
  6830. }
  6831. return false;
  6832. }
  6833. bool Node3DEditor::is_current_selected_gizmo(const EditorNode3DGizmo *p_gizmo) {
  6834. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  6835. if (se) {
  6836. return se->gizmo == p_gizmo;
  6837. }
  6838. return false;
  6839. }
  6840. Vector<int> Node3DEditor::get_subgizmo_selection() {
  6841. Node3DEditorSelectedItem *se = selected ? editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected) : nullptr;
  6842. Vector<int> ret;
  6843. if (se) {
  6844. for (const KeyValue<int, Transform3D> &E : se->subgizmos) {
  6845. ret.push_back(E.key);
  6846. }
  6847. }
  6848. return ret;
  6849. }
  6850. void Node3DEditor::clear_subgizmo_selection(Object *p_obj) {
  6851. _clear_subgizmo_selection(p_obj);
  6852. }
  6853. void Node3DEditor::add_control_to_menu_panel(Control *p_control) {
  6854. ERR_FAIL_NULL(p_control);
  6855. ERR_FAIL_COND(p_control->get_parent());
  6856. VSeparator *sep = memnew(VSeparator);
  6857. context_toolbar_hbox->add_child(sep);
  6858. context_toolbar_hbox->add_child(p_control);
  6859. context_toolbar_separators[p_control] = sep;
  6860. p_control->connect(SceneStringName(visibility_changed), callable_mp(this, &Node3DEditor::_update_context_toolbar));
  6861. _update_context_toolbar();
  6862. }
  6863. void Node3DEditor::remove_control_from_menu_panel(Control *p_control) {
  6864. ERR_FAIL_NULL(p_control);
  6865. ERR_FAIL_COND(p_control->get_parent() != context_toolbar_hbox);
  6866. p_control->disconnect(SceneStringName(visibility_changed), callable_mp(this, &Node3DEditor::_update_context_toolbar));
  6867. VSeparator *sep = context_toolbar_separators[p_control];
  6868. context_toolbar_hbox->remove_child(sep);
  6869. context_toolbar_hbox->remove_child(p_control);
  6870. context_toolbar_separators.erase(p_control);
  6871. memdelete(sep);
  6872. _update_context_toolbar();
  6873. }
  6874. void Node3DEditor::_update_context_toolbar() {
  6875. bool has_visible = false;
  6876. bool first_visible = false;
  6877. for (int i = 0; i < context_toolbar_hbox->get_child_count(); i++) {
  6878. Control *child = Object::cast_to<Control>(context_toolbar_hbox->get_child(i));
  6879. if (!child || !context_toolbar_separators.has(child)) {
  6880. continue;
  6881. }
  6882. if (child->is_visible()) {
  6883. first_visible = !has_visible;
  6884. has_visible = true;
  6885. }
  6886. VSeparator *sep = context_toolbar_separators[child];
  6887. sep->set_visible(!first_visible && child->is_visible());
  6888. }
  6889. context_toolbar_panel->set_visible(has_visible);
  6890. }
  6891. void Node3DEditor::set_can_preview(Camera3D *p_preview) {
  6892. for (int i = 0; i < 4; i++) {
  6893. viewports[i]->set_can_preview(p_preview);
  6894. }
  6895. }
  6896. VSplitContainer *Node3DEditor::get_shader_split() {
  6897. return shader_split;
  6898. }
  6899. Node3DEditorViewport *Node3DEditor::get_last_used_viewport() {
  6900. return viewports[last_used_viewport];
  6901. }
  6902. void Node3DEditor::add_control_to_left_panel(Control *p_control) {
  6903. left_panel_split->add_child(p_control);
  6904. left_panel_split->move_child(p_control, 0);
  6905. }
  6906. void Node3DEditor::add_control_to_right_panel(Control *p_control) {
  6907. right_panel_split->add_child(p_control);
  6908. right_panel_split->move_child(p_control, 1);
  6909. }
  6910. void Node3DEditor::remove_control_from_left_panel(Control *p_control) {
  6911. left_panel_split->remove_child(p_control);
  6912. }
  6913. void Node3DEditor::remove_control_from_right_panel(Control *p_control) {
  6914. right_panel_split->remove_child(p_control);
  6915. }
  6916. void Node3DEditor::move_control_to_left_panel(Control *p_control) {
  6917. ERR_FAIL_NULL(p_control);
  6918. if (p_control->get_parent() == left_panel_split) {
  6919. return;
  6920. }
  6921. ERR_FAIL_COND(p_control->get_parent() != right_panel_split);
  6922. right_panel_split->remove_child(p_control);
  6923. add_control_to_left_panel(p_control);
  6924. }
  6925. void Node3DEditor::move_control_to_right_panel(Control *p_control) {
  6926. ERR_FAIL_NULL(p_control);
  6927. if (p_control->get_parent() == right_panel_split) {
  6928. return;
  6929. }
  6930. ERR_FAIL_COND(p_control->get_parent() != left_panel_split);
  6931. left_panel_split->remove_child(p_control);
  6932. add_control_to_right_panel(p_control);
  6933. }
  6934. void Node3DEditor::_request_gizmo(Object *p_obj) {
  6935. Node3D *sp = Object::cast_to<Node3D>(p_obj);
  6936. if (!sp) {
  6937. return;
  6938. }
  6939. bool is_selected = (sp == selected);
  6940. Node *edited_scene = EditorNode::get_singleton()->get_edited_scene();
  6941. if (edited_scene && (sp == edited_scene || (sp->get_owner() && edited_scene->is_ancestor_of(sp)))) {
  6942. for (int i = 0; i < gizmo_plugins_by_priority.size(); ++i) {
  6943. Ref<EditorNode3DGizmo> seg = gizmo_plugins_by_priority.write[i]->get_gizmo(sp);
  6944. if (seg.is_valid()) {
  6945. sp->add_gizmo(seg);
  6946. if (is_selected != seg->is_selected()) {
  6947. seg->set_selected(is_selected);
  6948. }
  6949. }
  6950. }
  6951. if (!sp->get_gizmos().is_empty()) {
  6952. sp->update_gizmos();
  6953. }
  6954. }
  6955. }
  6956. void Node3DEditor::_request_gizmo_for_id(ObjectID p_id) {
  6957. Node3D *node = Object::cast_to<Node3D>(ObjectDB::get_instance(p_id));
  6958. if (node) {
  6959. _request_gizmo(node);
  6960. }
  6961. }
  6962. void Node3DEditor::_set_subgizmo_selection(Object *p_obj, Ref<Node3DGizmo> p_gizmo, int p_id, Transform3D p_transform) {
  6963. if (p_id == -1) {
  6964. _clear_subgizmo_selection(p_obj);
  6965. return;
  6966. }
  6967. Node3D *sp = nullptr;
  6968. if (p_obj) {
  6969. sp = Object::cast_to<Node3D>(p_obj);
  6970. } else {
  6971. sp = selected;
  6972. }
  6973. if (!sp) {
  6974. return;
  6975. }
  6976. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  6977. if (se) {
  6978. se->subgizmos.clear();
  6979. se->subgizmos.insert(p_id, p_transform);
  6980. se->gizmo = p_gizmo;
  6981. sp->update_gizmos();
  6982. update_transform_gizmo();
  6983. }
  6984. }
  6985. void Node3DEditor::_clear_subgizmo_selection(Object *p_obj) {
  6986. Node3D *sp = nullptr;
  6987. if (p_obj) {
  6988. sp = Object::cast_to<Node3D>(p_obj);
  6989. } else {
  6990. sp = selected;
  6991. }
  6992. if (!sp) {
  6993. return;
  6994. }
  6995. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(sp);
  6996. if (se) {
  6997. se->subgizmos.clear();
  6998. se->gizmo.unref();
  6999. sp->update_gizmos();
  7000. update_transform_gizmo();
  7001. }
  7002. }
  7003. void Node3DEditor::_toggle_maximize_view(Object *p_viewport) {
  7004. if (!p_viewport) {
  7005. return;
  7006. }
  7007. Node3DEditorViewport *current_viewport = Object::cast_to<Node3DEditorViewport>(p_viewport);
  7008. if (!current_viewport) {
  7009. return;
  7010. }
  7011. int index = -1;
  7012. bool maximized = false;
  7013. for (int i = 0; i < 4; i++) {
  7014. if (viewports[i] == current_viewport) {
  7015. index = i;
  7016. if (current_viewport->get_global_rect() == viewport_base->get_global_rect()) {
  7017. maximized = true;
  7018. }
  7019. break;
  7020. }
  7021. }
  7022. if (index == -1) {
  7023. return;
  7024. }
  7025. if (!maximized) {
  7026. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  7027. if (i == (uint32_t)index) {
  7028. viewports[i]->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
  7029. } else {
  7030. viewports[i]->hide();
  7031. }
  7032. }
  7033. } else {
  7034. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  7035. viewports[i]->show();
  7036. }
  7037. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT))) {
  7038. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  7039. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS))) {
  7040. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  7041. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT))) {
  7042. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  7043. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS))) {
  7044. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  7045. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT))) {
  7046. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  7047. } else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS))) {
  7048. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  7049. }
  7050. }
  7051. }
  7052. void Node3DEditor::_viewport_clicked(int p_viewport_idx) {
  7053. last_used_viewport = p_viewport_idx;
  7054. }
  7055. void Node3DEditor::_node_added(Node *p_node) {
  7056. if (EditorNode::get_singleton()->get_scene_root()->is_ancestor_of(p_node)) {
  7057. if (Object::cast_to<WorldEnvironment>(p_node)) {
  7058. world_env_count++;
  7059. if (world_env_count == 1) {
  7060. _update_preview_environment();
  7061. }
  7062. } else if (Object::cast_to<DirectionalLight3D>(p_node)) {
  7063. directional_light_count++;
  7064. if (directional_light_count == 1) {
  7065. _update_preview_environment();
  7066. }
  7067. }
  7068. }
  7069. }
  7070. void Node3DEditor::_node_removed(Node *p_node) {
  7071. if (EditorNode::get_singleton()->get_scene_root()->is_ancestor_of(p_node)) {
  7072. if (Object::cast_to<WorldEnvironment>(p_node)) {
  7073. world_env_count--;
  7074. if (world_env_count == 0) {
  7075. _update_preview_environment();
  7076. }
  7077. } else if (Object::cast_to<DirectionalLight3D>(p_node)) {
  7078. directional_light_count--;
  7079. if (directional_light_count == 0) {
  7080. _update_preview_environment();
  7081. }
  7082. }
  7083. }
  7084. if (p_node == selected) {
  7085. Node3DEditorSelectedItem *se = editor_selection->get_node_editor_data<Node3DEditorSelectedItem>(selected);
  7086. if (se) {
  7087. se->gizmo.unref();
  7088. se->subgizmos.clear();
  7089. }
  7090. selected = nullptr;
  7091. update_transform_gizmo();
  7092. }
  7093. }
  7094. void Node3DEditor::_register_all_gizmos() {
  7095. add_gizmo_plugin(Ref<Camera3DGizmoPlugin>(memnew(Camera3DGizmoPlugin)));
  7096. add_gizmo_plugin(Ref<Light3DGizmoPlugin>(memnew(Light3DGizmoPlugin)));
  7097. add_gizmo_plugin(Ref<AudioStreamPlayer3DGizmoPlugin>(memnew(AudioStreamPlayer3DGizmoPlugin)));
  7098. add_gizmo_plugin(Ref<AudioListener3DGizmoPlugin>(memnew(AudioListener3DGizmoPlugin)));
  7099. add_gizmo_plugin(Ref<MeshInstance3DGizmoPlugin>(memnew(MeshInstance3DGizmoPlugin)));
  7100. add_gizmo_plugin(Ref<OccluderInstance3DGizmoPlugin>(memnew(OccluderInstance3DGizmoPlugin)));
  7101. add_gizmo_plugin(Ref<SoftBody3DGizmoPlugin>(memnew(SoftBody3DGizmoPlugin)));
  7102. add_gizmo_plugin(Ref<SpriteBase3DGizmoPlugin>(memnew(SpriteBase3DGizmoPlugin)));
  7103. add_gizmo_plugin(Ref<Label3DGizmoPlugin>(memnew(Label3DGizmoPlugin)));
  7104. add_gizmo_plugin(Ref<GeometryInstance3DGizmoPlugin>(memnew(GeometryInstance3DGizmoPlugin)));
  7105. add_gizmo_plugin(Ref<Marker3DGizmoPlugin>(memnew(Marker3DGizmoPlugin)));
  7106. add_gizmo_plugin(Ref<RayCast3DGizmoPlugin>(memnew(RayCast3DGizmoPlugin)));
  7107. add_gizmo_plugin(Ref<ShapeCast3DGizmoPlugin>(memnew(ShapeCast3DGizmoPlugin)));
  7108. add_gizmo_plugin(Ref<SpringArm3DGizmoPlugin>(memnew(SpringArm3DGizmoPlugin)));
  7109. add_gizmo_plugin(Ref<VehicleWheel3DGizmoPlugin>(memnew(VehicleWheel3DGizmoPlugin)));
  7110. add_gizmo_plugin(Ref<VisibleOnScreenNotifier3DGizmoPlugin>(memnew(VisibleOnScreenNotifier3DGizmoPlugin)));
  7111. add_gizmo_plugin(Ref<GPUParticles3DGizmoPlugin>(memnew(GPUParticles3DGizmoPlugin)));
  7112. add_gizmo_plugin(Ref<GPUParticlesCollision3DGizmoPlugin>(memnew(GPUParticlesCollision3DGizmoPlugin)));
  7113. add_gizmo_plugin(Ref<CPUParticles3DGizmoPlugin>(memnew(CPUParticles3DGizmoPlugin)));
  7114. add_gizmo_plugin(Ref<ReflectionProbeGizmoPlugin>(memnew(ReflectionProbeGizmoPlugin)));
  7115. add_gizmo_plugin(Ref<DecalGizmoPlugin>(memnew(DecalGizmoPlugin)));
  7116. add_gizmo_plugin(Ref<VoxelGIGizmoPlugin>(memnew(VoxelGIGizmoPlugin)));
  7117. add_gizmo_plugin(Ref<LightmapGIGizmoPlugin>(memnew(LightmapGIGizmoPlugin)));
  7118. add_gizmo_plugin(Ref<LightmapProbeGizmoPlugin>(memnew(LightmapProbeGizmoPlugin)));
  7119. add_gizmo_plugin(Ref<CollisionObject3DGizmoPlugin>(memnew(CollisionObject3DGizmoPlugin)));
  7120. add_gizmo_plugin(Ref<CollisionShape3DGizmoPlugin>(memnew(CollisionShape3DGizmoPlugin)));
  7121. add_gizmo_plugin(Ref<CollisionPolygon3DGizmoPlugin>(memnew(CollisionPolygon3DGizmoPlugin)));
  7122. add_gizmo_plugin(Ref<NavigationLink3DGizmoPlugin>(memnew(NavigationLink3DGizmoPlugin)));
  7123. add_gizmo_plugin(Ref<NavigationRegion3DGizmoPlugin>(memnew(NavigationRegion3DGizmoPlugin)));
  7124. add_gizmo_plugin(Ref<Joint3DGizmoPlugin>(memnew(Joint3DGizmoPlugin)));
  7125. add_gizmo_plugin(Ref<PhysicalBone3DGizmoPlugin>(memnew(PhysicalBone3DGizmoPlugin)));
  7126. add_gizmo_plugin(Ref<FogVolumeGizmoPlugin>(memnew(FogVolumeGizmoPlugin)));
  7127. }
  7128. void Node3DEditor::_bind_methods() {
  7129. ClassDB::bind_method("_get_editor_data", &Node3DEditor::_get_editor_data);
  7130. ClassDB::bind_method("_request_gizmo", &Node3DEditor::_request_gizmo);
  7131. ClassDB::bind_method("_request_gizmo_for_id", &Node3DEditor::_request_gizmo_for_id);
  7132. ClassDB::bind_method("_set_subgizmo_selection", &Node3DEditor::_set_subgizmo_selection);
  7133. ClassDB::bind_method("_clear_subgizmo_selection", &Node3DEditor::_clear_subgizmo_selection);
  7134. ClassDB::bind_method("_refresh_menu_icons", &Node3DEditor::_refresh_menu_icons);
  7135. ClassDB::bind_method("update_all_gizmos", &Node3DEditor::update_all_gizmos);
  7136. ClassDB::bind_method("update_transform_gizmo", &Node3DEditor::update_transform_gizmo);
  7137. ADD_SIGNAL(MethodInfo("transform_key_request"));
  7138. ADD_SIGNAL(MethodInfo("item_lock_status_changed"));
  7139. ADD_SIGNAL(MethodInfo("item_group_status_changed"));
  7140. }
  7141. void Node3DEditor::clear() {
  7142. settings_fov->set_value(EDITOR_GET("editors/3d/default_fov"));
  7143. settings_znear->set_value(EDITOR_GET("editors/3d/default_z_near"));
  7144. settings_zfar->set_value(EDITOR_GET("editors/3d/default_z_far"));
  7145. snap_translate_value = EditorSettings::get_singleton()->get_project_metadata("3d_editor", "snap_translate_value", 1);
  7146. snap_rotate_value = EditorSettings::get_singleton()->get_project_metadata("3d_editor", "snap_rotate_value", 15);
  7147. snap_scale_value = EditorSettings::get_singleton()->get_project_metadata("3d_editor", "snap_scale_value", 10);
  7148. _snap_update();
  7149. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  7150. viewports[i]->reset();
  7151. }
  7152. if (origin_instance.is_valid()) {
  7153. RenderingServer::get_singleton()->instance_set_visible(origin_instance, true);
  7154. }
  7155. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), true);
  7156. for (int i = 0; i < 3; ++i) {
  7157. if (grid_enable[i]) {
  7158. grid_visible[i] = true;
  7159. }
  7160. }
  7161. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  7162. viewports[i]->view_menu->get_popup()->set_item_checked(viewports[i]->view_menu->get_popup()->get_item_index(Node3DEditorViewport::VIEW_AUDIO_LISTENER), i == 0);
  7163. viewports[i]->viewport->set_as_audio_listener_3d(i == 0);
  7164. }
  7165. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID), true);
  7166. grid_enabled = true;
  7167. grid_init_draw = false;
  7168. }
  7169. void Node3DEditor::_sun_direction_draw() {
  7170. sun_direction->draw_rect(Rect2(Vector2(), sun_direction->get_size()), Color(1, 1, 1, 1));
  7171. Vector3 z_axis = preview_sun->get_transform().basis.get_column(Vector3::AXIS_Z);
  7172. z_axis = get_editor_viewport(0)->camera->get_camera_transform().basis.xform_inv(z_axis);
  7173. sun_direction_material->set_shader_parameter("sun_direction", Vector3(z_axis.x, -z_axis.y, z_axis.z));
  7174. Color color = sun_color->get_pick_color() * sun_energy->get_value();
  7175. sun_direction_material->set_shader_parameter("sun_color", Vector3(color.r, color.g, color.b));
  7176. }
  7177. void Node3DEditor::_preview_settings_changed() {
  7178. if (sun_environ_updating) {
  7179. return;
  7180. }
  7181. { // preview sun
  7182. Transform3D t;
  7183. t.basis = Basis::from_euler(Vector3(sun_rotation.x, sun_rotation.y, 0));
  7184. preview_sun->set_transform(t);
  7185. sun_direction->queue_redraw();
  7186. preview_sun->set_param(Light3D::PARAM_ENERGY, sun_energy->get_value());
  7187. preview_sun->set_param(Light3D::PARAM_SHADOW_MAX_DISTANCE, sun_max_distance->get_value());
  7188. preview_sun->set_color(sun_color->get_pick_color());
  7189. }
  7190. { //preview env
  7191. sky_material->set_sky_energy_multiplier(environ_energy->get_value());
  7192. Color hz_color = environ_sky_color->get_pick_color().lerp(environ_ground_color->get_pick_color(), 0.5).lerp(Color(1, 1, 1), 0.5);
  7193. sky_material->set_sky_top_color(environ_sky_color->get_pick_color());
  7194. sky_material->set_sky_horizon_color(hz_color);
  7195. sky_material->set_ground_bottom_color(environ_ground_color->get_pick_color());
  7196. sky_material->set_ground_horizon_color(hz_color);
  7197. environment->set_ssao_enabled(environ_ao_button->is_pressed());
  7198. environment->set_glow_enabled(environ_glow_button->is_pressed());
  7199. environment->set_sdfgi_enabled(environ_gi_button->is_pressed());
  7200. environment->set_tonemapper(environ_tonemap_button->is_pressed() ? Environment::TONE_MAPPER_FILMIC : Environment::TONE_MAPPER_LINEAR);
  7201. }
  7202. }
  7203. void Node3DEditor::_load_default_preview_settings() {
  7204. sun_environ_updating = true;
  7205. // These default rotations place the preview sun at an angular altitude
  7206. // of 60 degrees (must be negative) and an azimuth of 30 degrees clockwise
  7207. // from north (or 150 CCW from south), from north east, facing south west.
  7208. // On any not-tidally-locked planet, a sun would have an angular altitude
  7209. // of 60 degrees as the average of all points on the sphere at noon.
  7210. // The azimuth choice is arbitrary, but ideally shouldn't be on an axis.
  7211. sun_rotation = Vector2(-Math::deg_to_rad(60.0), Math::deg_to_rad(150.0));
  7212. sun_angle_altitude->set_value(-Math::rad_to_deg(sun_rotation.x));
  7213. sun_angle_azimuth->set_value(180.0 - Math::rad_to_deg(sun_rotation.y));
  7214. sun_direction->queue_redraw();
  7215. environ_sky_color->set_pick_color(Color(0.385, 0.454, 0.55));
  7216. environ_ground_color->set_pick_color(Color(0.2, 0.169, 0.133));
  7217. environ_energy->set_value(1.0);
  7218. if (OS::get_singleton()->get_current_rendering_method() != "gl_compatibility") {
  7219. environ_glow_button->set_pressed(true);
  7220. }
  7221. environ_tonemap_button->set_pressed(true);
  7222. environ_ao_button->set_pressed(false);
  7223. environ_gi_button->set_pressed(false);
  7224. sun_max_distance->set_value(100);
  7225. sun_color->set_pick_color(Color(1, 1, 1));
  7226. sun_energy->set_value(1.0);
  7227. sun_environ_updating = false;
  7228. }
  7229. void Node3DEditor::_update_preview_environment() {
  7230. bool disable_light = directional_light_count > 0 || !sun_button->is_pressed();
  7231. sun_button->set_disabled(directional_light_count > 0);
  7232. if (disable_light) {
  7233. if (preview_sun->get_parent()) {
  7234. preview_sun->get_parent()->remove_child(preview_sun);
  7235. sun_state->show();
  7236. sun_vb->hide();
  7237. preview_sun_dangling = true;
  7238. }
  7239. if (directional_light_count > 0) {
  7240. sun_state->set_text(TTR("Scene contains\nDirectionalLight3D.\nPreview disabled."));
  7241. } else {
  7242. sun_state->set_text(TTR("Preview disabled."));
  7243. }
  7244. } else {
  7245. if (!preview_sun->get_parent()) {
  7246. add_child(preview_sun, true);
  7247. sun_state->hide();
  7248. sun_vb->show();
  7249. preview_sun_dangling = false;
  7250. }
  7251. }
  7252. sun_angle_altitude->set_value(-Math::rad_to_deg(sun_rotation.x));
  7253. sun_angle_azimuth->set_value(180.0 - Math::rad_to_deg(sun_rotation.y));
  7254. bool disable_env = world_env_count > 0 || !environ_button->is_pressed();
  7255. environ_button->set_disabled(world_env_count > 0);
  7256. if (disable_env) {
  7257. if (preview_environment->get_parent()) {
  7258. preview_environment->get_parent()->remove_child(preview_environment);
  7259. environ_state->show();
  7260. environ_vb->hide();
  7261. preview_env_dangling = true;
  7262. }
  7263. if (world_env_count > 0) {
  7264. environ_state->set_text(TTR("Scene contains\nWorldEnvironment.\nPreview disabled."));
  7265. } else {
  7266. environ_state->set_text(TTR("Preview disabled."));
  7267. }
  7268. } else {
  7269. if (!preview_environment->get_parent()) {
  7270. add_child(preview_environment);
  7271. environ_state->hide();
  7272. environ_vb->show();
  7273. preview_env_dangling = false;
  7274. }
  7275. }
  7276. }
  7277. void Node3DEditor::_sun_direction_input(const Ref<InputEvent> &p_event) {
  7278. Ref<InputEventMouseMotion> mm = p_event;
  7279. if (mm.is_valid() && (mm->get_button_mask().has_flag(MouseButtonMask::LEFT))) {
  7280. sun_rotation.x += mm->get_relative().y * (0.02 * EDSCALE);
  7281. sun_rotation.y -= mm->get_relative().x * (0.02 * EDSCALE);
  7282. sun_rotation.x = CLAMP(sun_rotation.x, -Math_TAU / 4, Math_TAU / 4);
  7283. sun_angle_altitude->set_value(-Math::rad_to_deg(sun_rotation.x));
  7284. sun_angle_azimuth->set_value(180.0 - Math::rad_to_deg(sun_rotation.y));
  7285. _preview_settings_changed();
  7286. }
  7287. }
  7288. void Node3DEditor::_sun_direction_angle_set() {
  7289. sun_rotation.x = Math::deg_to_rad(-sun_angle_altitude->get_value());
  7290. sun_rotation.y = Math::deg_to_rad(180.0 - sun_angle_azimuth->get_value());
  7291. _preview_settings_changed();
  7292. }
  7293. Node3DEditor::Node3DEditor() {
  7294. gizmo.visible = true;
  7295. gizmo.scale = 1.0;
  7296. viewport_environment.instantiate();
  7297. VBoxContainer *vbc = this;
  7298. custom_camera = nullptr;
  7299. ERR_FAIL_COND_MSG(singleton != nullptr, "A Node3DEditor singleton already exists.");
  7300. singleton = this;
  7301. editor_selection = EditorNode::get_singleton()->get_editor_selection();
  7302. editor_selection->add_editor_plugin(this);
  7303. snap_enabled = false;
  7304. snap_key_enabled = false;
  7305. tool_mode = TOOL_MODE_SELECT;
  7306. // Add some margin to the sides for better aesthetics.
  7307. // This prevents the first button's hover/pressed effect from "touching" the panel's border,
  7308. // which looks ugly.
  7309. MarginContainer *toolbar_margin = memnew(MarginContainer);
  7310. toolbar_margin->add_theme_constant_override("margin_left", 4 * EDSCALE);
  7311. toolbar_margin->add_theme_constant_override("margin_right", 4 * EDSCALE);
  7312. vbc->add_child(toolbar_margin);
  7313. // A fluid container for all toolbars.
  7314. HFlowContainer *main_flow = memnew(HFlowContainer);
  7315. toolbar_margin->add_child(main_flow);
  7316. // Main toolbars.
  7317. HBoxContainer *main_menu_hbox = memnew(HBoxContainer);
  7318. main_flow->add_child(main_menu_hbox);
  7319. String sct;
  7320. tool_button[TOOL_MODE_SELECT] = memnew(Button);
  7321. main_menu_hbox->add_child(tool_button[TOOL_MODE_SELECT]);
  7322. tool_button[TOOL_MODE_SELECT]->set_toggle_mode(true);
  7323. tool_button[TOOL_MODE_SELECT]->set_theme_type_variation("FlatButton");
  7324. tool_button[TOOL_MODE_SELECT]->set_pressed(true);
  7325. tool_button[TOOL_MODE_SELECT]->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_TOOL_SELECT));
  7326. tool_button[TOOL_MODE_SELECT]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_select", TTR("Select Mode"), Key::Q));
  7327. tool_button[TOOL_MODE_SELECT]->set_shortcut_context(this);
  7328. tool_button[TOOL_MODE_SELECT]->set_tooltip_text(keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Drag: Rotate selected node around pivot.") + "\n" + TTR("Alt+RMB: Show list of all nodes at position clicked, including locked."));
  7329. main_menu_hbox->add_child(memnew(VSeparator));
  7330. tool_button[TOOL_MODE_MOVE] = memnew(Button);
  7331. main_menu_hbox->add_child(tool_button[TOOL_MODE_MOVE]);
  7332. tool_button[TOOL_MODE_MOVE]->set_toggle_mode(true);
  7333. tool_button[TOOL_MODE_MOVE]->set_theme_type_variation("FlatButton");
  7334. tool_button[TOOL_MODE_MOVE]->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_TOOL_MOVE));
  7335. tool_button[TOOL_MODE_MOVE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_move", TTR("Move Mode"), Key::W));
  7336. tool_button[TOOL_MODE_MOVE]->set_shortcut_context(this);
  7337. tool_button[TOOL_MODE_MOVE]->set_tooltip_text(keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Drag: Use snap.") + "\n" + TTR("Alt+RMB: Show list of all nodes at position clicked, including locked."));
  7338. tool_button[TOOL_MODE_ROTATE] = memnew(Button);
  7339. main_menu_hbox->add_child(tool_button[TOOL_MODE_ROTATE]);
  7340. tool_button[TOOL_MODE_ROTATE]->set_toggle_mode(true);
  7341. tool_button[TOOL_MODE_ROTATE]->set_theme_type_variation("FlatButton");
  7342. tool_button[TOOL_MODE_ROTATE]->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_TOOL_ROTATE));
  7343. tool_button[TOOL_MODE_ROTATE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_rotate", TTR("Rotate Mode"), Key::E));
  7344. tool_button[TOOL_MODE_ROTATE]->set_shortcut_context(this);
  7345. tool_button[TOOL_MODE_ROTATE]->set_tooltip_text(keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Drag: Use snap.") + "\n" + TTR("Alt+RMB: Show list of all nodes at position clicked, including locked."));
  7346. tool_button[TOOL_MODE_SCALE] = memnew(Button);
  7347. main_menu_hbox->add_child(tool_button[TOOL_MODE_SCALE]);
  7348. tool_button[TOOL_MODE_SCALE]->set_toggle_mode(true);
  7349. tool_button[TOOL_MODE_SCALE]->set_theme_type_variation("FlatButton");
  7350. tool_button[TOOL_MODE_SCALE]->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_TOOL_SCALE));
  7351. tool_button[TOOL_MODE_SCALE]->set_shortcut(ED_SHORTCUT("spatial_editor/tool_scale", TTR("Scale Mode"), Key::R));
  7352. tool_button[TOOL_MODE_SCALE]->set_shortcut_context(this);
  7353. tool_button[TOOL_MODE_SCALE]->set_tooltip_text(keycode_get_string((Key)KeyModifierMask::CMD_OR_CTRL) + TTR("Drag: Use snap.") + "\n" + TTR("Alt+RMB: Show list of all nodes at position clicked, including locked."));
  7354. main_menu_hbox->add_child(memnew(VSeparator));
  7355. tool_button[TOOL_MODE_LIST_SELECT] = memnew(Button);
  7356. main_menu_hbox->add_child(tool_button[TOOL_MODE_LIST_SELECT]);
  7357. tool_button[TOOL_MODE_LIST_SELECT]->set_toggle_mode(true);
  7358. tool_button[TOOL_MODE_LIST_SELECT]->set_theme_type_variation("FlatButton");
  7359. tool_button[TOOL_MODE_LIST_SELECT]->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_TOOL_LIST_SELECT));
  7360. tool_button[TOOL_MODE_LIST_SELECT]->set_tooltip_text(TTR("Show list of selectable nodes at position clicked."));
  7361. tool_button[TOOL_LOCK_SELECTED] = memnew(Button);
  7362. main_menu_hbox->add_child(tool_button[TOOL_LOCK_SELECTED]);
  7363. tool_button[TOOL_LOCK_SELECTED]->set_theme_type_variation("FlatButton");
  7364. tool_button[TOOL_LOCK_SELECTED]->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_LOCK_SELECTED));
  7365. tool_button[TOOL_LOCK_SELECTED]->set_tooltip_text(TTR("Lock selected node, preventing selection and movement."));
  7366. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  7367. tool_button[TOOL_LOCK_SELECTED]->set_shortcut(ED_GET_SHORTCUT("editor/lock_selected_nodes"));
  7368. tool_button[TOOL_UNLOCK_SELECTED] = memnew(Button);
  7369. main_menu_hbox->add_child(tool_button[TOOL_UNLOCK_SELECTED]);
  7370. tool_button[TOOL_UNLOCK_SELECTED]->set_theme_type_variation("FlatButton");
  7371. tool_button[TOOL_UNLOCK_SELECTED]->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_UNLOCK_SELECTED));
  7372. tool_button[TOOL_UNLOCK_SELECTED]->set_tooltip_text(TTR("Unlock selected node, allowing selection and movement."));
  7373. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  7374. tool_button[TOOL_UNLOCK_SELECTED]->set_shortcut(ED_GET_SHORTCUT("editor/unlock_selected_nodes"));
  7375. tool_button[TOOL_GROUP_SELECTED] = memnew(Button);
  7376. main_menu_hbox->add_child(tool_button[TOOL_GROUP_SELECTED]);
  7377. tool_button[TOOL_GROUP_SELECTED]->set_theme_type_variation("FlatButton");
  7378. tool_button[TOOL_GROUP_SELECTED]->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_GROUP_SELECTED));
  7379. tool_button[TOOL_GROUP_SELECTED]->set_tooltip_text(TTR("Groups the selected node with its children. This selects the parent when any child node is clicked in 2D and 3D view."));
  7380. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  7381. tool_button[TOOL_GROUP_SELECTED]->set_shortcut(ED_GET_SHORTCUT("editor/group_selected_nodes"));
  7382. tool_button[TOOL_UNGROUP_SELECTED] = memnew(Button);
  7383. main_menu_hbox->add_child(tool_button[TOOL_UNGROUP_SELECTED]);
  7384. tool_button[TOOL_UNGROUP_SELECTED]->set_theme_type_variation("FlatButton");
  7385. tool_button[TOOL_UNGROUP_SELECTED]->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed).bind(MENU_UNGROUP_SELECTED));
  7386. tool_button[TOOL_UNGROUP_SELECTED]->set_tooltip_text(TTR("Ungroups the selected node from its children. Child nodes will be individual items in 2D and 3D view."));
  7387. // Define the shortcut globally (without a context) so that it works if the Scene tree dock is currently focused.
  7388. tool_button[TOOL_UNGROUP_SELECTED]->set_shortcut(ED_GET_SHORTCUT("editor/ungroup_selected_nodes"));
  7389. main_menu_hbox->add_child(memnew(VSeparator));
  7390. tool_option_button[TOOL_OPT_LOCAL_COORDS] = memnew(Button);
  7391. main_menu_hbox->add_child(tool_option_button[TOOL_OPT_LOCAL_COORDS]);
  7392. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_toggle_mode(true);
  7393. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_theme_type_variation("FlatButton");
  7394. tool_option_button[TOOL_OPT_LOCAL_COORDS]->connect(SceneStringName(toggled), callable_mp(this, &Node3DEditor::_menu_item_toggled).bind(MENU_TOOL_LOCAL_COORDS));
  7395. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_shortcut(ED_SHORTCUT("spatial_editor/local_coords", TTR("Use Local Space"), Key::T));
  7396. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_shortcut_context(this);
  7397. tool_option_button[TOOL_OPT_USE_SNAP] = memnew(Button);
  7398. main_menu_hbox->add_child(tool_option_button[TOOL_OPT_USE_SNAP]);
  7399. tool_option_button[TOOL_OPT_USE_SNAP]->set_toggle_mode(true);
  7400. tool_option_button[TOOL_OPT_USE_SNAP]->set_theme_type_variation("FlatButton");
  7401. tool_option_button[TOOL_OPT_USE_SNAP]->connect(SceneStringName(toggled), callable_mp(this, &Node3DEditor::_menu_item_toggled).bind(MENU_TOOL_USE_SNAP));
  7402. tool_option_button[TOOL_OPT_USE_SNAP]->set_shortcut(ED_SHORTCUT("spatial_editor/snap", TTR("Use Snap"), Key::Y));
  7403. tool_option_button[TOOL_OPT_USE_SNAP]->set_shortcut_context(this);
  7404. main_menu_hbox->add_child(memnew(VSeparator));
  7405. sun_button = memnew(Button);
  7406. sun_button->set_tooltip_text(TTR("Toggle preview sunlight.\nIf a DirectionalLight3D node is added to the scene, preview sunlight is disabled."));
  7407. sun_button->set_toggle_mode(true);
  7408. sun_button->set_theme_type_variation("FlatButton");
  7409. sun_button->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_update_preview_environment), CONNECT_DEFERRED);
  7410. // Preview is enabled by default - ensure this applies on editor startup when there is no state yet.
  7411. sun_button->set_pressed(true);
  7412. main_menu_hbox->add_child(sun_button);
  7413. environ_button = memnew(Button);
  7414. environ_button->set_tooltip_text(TTR("Toggle preview environment.\nIf a WorldEnvironment node is added to the scene, preview environment is disabled."));
  7415. environ_button->set_toggle_mode(true);
  7416. environ_button->set_theme_type_variation("FlatButton");
  7417. environ_button->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_update_preview_environment), CONNECT_DEFERRED);
  7418. // Preview is enabled by default - ensure this applies on editor startup when there is no state yet.
  7419. environ_button->set_pressed(true);
  7420. main_menu_hbox->add_child(environ_button);
  7421. sun_environ_settings = memnew(Button);
  7422. sun_environ_settings->set_tooltip_text(TTR("Edit Sun and Environment settings."));
  7423. sun_environ_settings->set_theme_type_variation("FlatButton");
  7424. sun_environ_settings->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_sun_environ_settings_pressed));
  7425. main_menu_hbox->add_child(sun_environ_settings);
  7426. main_menu_hbox->add_child(memnew(VSeparator));
  7427. // Drag and drop support;
  7428. preview_node = memnew(Node3D);
  7429. preview_bounds = AABB();
  7430. ED_SHORTCUT("spatial_editor/bottom_view", TTR("Bottom View"), KeyModifierMask::ALT + Key::KP_7);
  7431. ED_SHORTCUT("spatial_editor/top_view", TTR("Top View"), Key::KP_7);
  7432. ED_SHORTCUT("spatial_editor/rear_view", TTR("Rear View"), KeyModifierMask::ALT + Key::KP_1);
  7433. ED_SHORTCUT("spatial_editor/front_view", TTR("Front View"), Key::KP_1);
  7434. ED_SHORTCUT("spatial_editor/left_view", TTR("Left View"), KeyModifierMask::ALT + Key::KP_3);
  7435. ED_SHORTCUT("spatial_editor/right_view", TTR("Right View"), Key::KP_3);
  7436. ED_SHORTCUT("spatial_editor/orbit_view_down", TTR("Orbit View Down"), Key::KP_2);
  7437. ED_SHORTCUT("spatial_editor/orbit_view_left", TTR("Orbit View Left"), Key::KP_4);
  7438. ED_SHORTCUT("spatial_editor/orbit_view_right", TTR("Orbit View Right"), Key::KP_6);
  7439. ED_SHORTCUT("spatial_editor/orbit_view_up", TTR("Orbit View Up"), Key::KP_8);
  7440. ED_SHORTCUT("spatial_editor/orbit_view_180", TTR("Orbit View 180"), Key::KP_9);
  7441. ED_SHORTCUT("spatial_editor/switch_perspective_orthogonal", TTR("Switch Perspective/Orthogonal View"), Key::KP_5);
  7442. ED_SHORTCUT("spatial_editor/insert_anim_key", TTR("Insert Animation Key"), Key::K);
  7443. ED_SHORTCUT("spatial_editor/focus_origin", TTR("Focus Origin"), Key::O);
  7444. ED_SHORTCUT("spatial_editor/focus_selection", TTR("Focus Selection"), Key::F);
  7445. ED_SHORTCUT_ARRAY("spatial_editor/align_transform_with_view", TTR("Align Transform with View"),
  7446. { int32_t(KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::KP_0), int32_t(KeyModifierMask::ALT | KeyModifierMask::CMD_OR_CTRL | Key::M) });
  7447. ED_SHORTCUT("spatial_editor/align_rotation_with_view", TTR("Align Rotation with View"), KeyModifierMask::ALT + KeyModifierMask::CMD_OR_CTRL + Key::F);
  7448. ED_SHORTCUT("spatial_editor/freelook_toggle", TTR("Toggle Freelook"), KeyModifierMask::SHIFT + Key::F);
  7449. ED_SHORTCUT("spatial_editor/decrease_fov", TTR("Decrease Field of View"), KeyModifierMask::CMD_OR_CTRL + Key::EQUAL); // Usually direct access key for `KEY_PLUS`.
  7450. ED_SHORTCUT("spatial_editor/increase_fov", TTR("Increase Field of View"), KeyModifierMask::CMD_OR_CTRL + Key::MINUS);
  7451. ED_SHORTCUT("spatial_editor/reset_fov", TTR("Reset Field of View to Default"), KeyModifierMask::CMD_OR_CTRL + Key::KEY_0);
  7452. PopupMenu *p;
  7453. transform_menu = memnew(MenuButton);
  7454. transform_menu->set_flat(false);
  7455. transform_menu->set_theme_type_variation("FlatMenuButton");
  7456. transform_menu->set_text(TTR("Transform"));
  7457. transform_menu->set_switch_on_hover(true);
  7458. transform_menu->set_shortcut_context(this);
  7459. main_menu_hbox->add_child(transform_menu);
  7460. p = transform_menu->get_popup();
  7461. p->add_shortcut(ED_SHORTCUT("spatial_editor/snap_to_floor", TTR("Snap Object to Floor"), Key::PAGEDOWN), MENU_SNAP_TO_FLOOR);
  7462. p->add_shortcut(ED_SHORTCUT("spatial_editor/transform_dialog", TTR("Transform Dialog...")), MENU_TRANSFORM_DIALOG);
  7463. p->add_separator();
  7464. p->add_shortcut(ED_SHORTCUT("spatial_editor/configure_snap", TTR("Configure Snap...")), MENU_TRANSFORM_CONFIGURE_SNAP);
  7465. p->connect(SceneStringName(id_pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed));
  7466. view_menu = memnew(MenuButton);
  7467. view_menu->set_flat(false);
  7468. view_menu->set_theme_type_variation("FlatMenuButton");
  7469. // TRANSLATORS: Noun, name of the 2D/3D View menus.
  7470. view_menu->set_text(TTR("View"));
  7471. view_menu->set_switch_on_hover(true);
  7472. view_menu->set_shortcut_context(this);
  7473. main_menu_hbox->add_child(view_menu);
  7474. main_menu_hbox->add_child(memnew(VSeparator));
  7475. context_toolbar_panel = memnew(PanelContainer);
  7476. context_toolbar_hbox = memnew(HBoxContainer);
  7477. context_toolbar_panel->add_child(context_toolbar_hbox);
  7478. main_flow->add_child(context_toolbar_panel);
  7479. // Get the view menu popup and have it stay open when a checkable item is selected
  7480. p = view_menu->get_popup();
  7481. p->set_hide_on_checkable_item_selection(false);
  7482. accept = memnew(AcceptDialog);
  7483. EditorNode::get_singleton()->get_gui_base()->add_child(accept);
  7484. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/1_viewport", TTR("1 Viewport"), KeyModifierMask::CMD_OR_CTRL + Key::KEY_1), MENU_VIEW_USE_1_VIEWPORT);
  7485. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports", TTR("2 Viewports"), KeyModifierMask::CMD_OR_CTRL + Key::KEY_2), MENU_VIEW_USE_2_VIEWPORTS);
  7486. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports_alt", TTR("2 Viewports (Alt)"), KeyModifierMask::ALT + KeyModifierMask::CMD_OR_CTRL + Key::KEY_2), MENU_VIEW_USE_2_VIEWPORTS_ALT);
  7487. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports", TTR("3 Viewports"), KeyModifierMask::CMD_OR_CTRL + Key::KEY_3), MENU_VIEW_USE_3_VIEWPORTS);
  7488. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports_alt", TTR("3 Viewports (Alt)"), KeyModifierMask::ALT + KeyModifierMask::CMD_OR_CTRL + Key::KEY_3), MENU_VIEW_USE_3_VIEWPORTS_ALT);
  7489. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/4_viewports", TTR("4 Viewports"), KeyModifierMask::CMD_OR_CTRL + Key::KEY_4), MENU_VIEW_USE_4_VIEWPORTS);
  7490. p->add_separator();
  7491. gizmos_menu = memnew(PopupMenu);
  7492. gizmos_menu->set_hide_on_checkable_item_selection(false);
  7493. p->add_submenu_node_item(TTR("Gizmos"), gizmos_menu);
  7494. gizmos_menu->connect(SceneStringName(id_pressed), callable_mp(this, &Node3DEditor::_menu_gizmo_toggled));
  7495. p->add_separator();
  7496. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_origin", TTR("View Origin")), MENU_VIEW_ORIGIN);
  7497. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_grid", TTR("View Grid"), Key::NUMBERSIGN), MENU_VIEW_GRID);
  7498. p->add_separator();
  7499. p->add_shortcut(ED_SHORTCUT("spatial_editor/settings", TTR("Settings...")), MENU_VIEW_CAMERA_SETTINGS);
  7500. p->set_item_checked(p->get_item_index(MENU_VIEW_ORIGIN), true);
  7501. p->set_item_checked(p->get_item_index(MENU_VIEW_GRID), true);
  7502. p->connect(SceneStringName(id_pressed), callable_mp(this, &Node3DEditor::_menu_item_pressed));
  7503. /* REST OF MENU */
  7504. left_panel_split = memnew(HSplitContainer);
  7505. left_panel_split->set_v_size_flags(SIZE_EXPAND_FILL);
  7506. vbc->add_child(left_panel_split);
  7507. right_panel_split = memnew(HSplitContainer);
  7508. right_panel_split->set_v_size_flags(SIZE_EXPAND_FILL);
  7509. left_panel_split->add_child(right_panel_split);
  7510. shader_split = memnew(VSplitContainer);
  7511. shader_split->set_h_size_flags(SIZE_EXPAND_FILL);
  7512. right_panel_split->add_child(shader_split);
  7513. viewport_base = memnew(Node3DEditorViewportContainer);
  7514. shader_split->add_child(viewport_base);
  7515. viewport_base->set_v_size_flags(SIZE_EXPAND_FILL);
  7516. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  7517. viewports[i] = memnew(Node3DEditorViewport(this, i));
  7518. viewports[i]->connect("toggle_maximize_view", callable_mp(this, &Node3DEditor::_toggle_maximize_view));
  7519. viewports[i]->connect("clicked", callable_mp(this, &Node3DEditor::_viewport_clicked).bind(i));
  7520. viewports[i]->assign_pending_data_pointers(preview_node, &preview_bounds, accept);
  7521. viewport_base->add_child(viewports[i]);
  7522. }
  7523. /* SNAP DIALOG */
  7524. snap_dialog = memnew(ConfirmationDialog);
  7525. snap_dialog->set_title(TTR("Snap Settings"));
  7526. add_child(snap_dialog);
  7527. snap_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Node3DEditor::_snap_changed));
  7528. snap_dialog->get_cancel_button()->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_snap_update));
  7529. VBoxContainer *snap_dialog_vbc = memnew(VBoxContainer);
  7530. snap_dialog->add_child(snap_dialog_vbc);
  7531. snap_translate = memnew(LineEdit);
  7532. snap_translate->set_select_all_on_focus(true);
  7533. snap_dialog_vbc->add_margin_child(TTR("Translate Snap:"), snap_translate);
  7534. snap_rotate = memnew(LineEdit);
  7535. snap_rotate->set_select_all_on_focus(true);
  7536. snap_dialog_vbc->add_margin_child(TTR("Rotate Snap (deg.):"), snap_rotate);
  7537. snap_scale = memnew(LineEdit);
  7538. snap_scale->set_select_all_on_focus(true);
  7539. snap_dialog_vbc->add_margin_child(TTR("Scale Snap (%):"), snap_scale);
  7540. /* SETTINGS DIALOG */
  7541. settings_dialog = memnew(ConfirmationDialog);
  7542. settings_dialog->set_title(TTR("Viewport Settings"));
  7543. add_child(settings_dialog);
  7544. settings_vbc = memnew(VBoxContainer);
  7545. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  7546. settings_dialog->add_child(settings_vbc);
  7547. settings_fov = memnew(SpinBox);
  7548. settings_fov->set_max(MAX_FOV);
  7549. settings_fov->set_min(MIN_FOV);
  7550. settings_fov->set_step(0.1);
  7551. settings_fov->set_value(EDITOR_GET("editors/3d/default_fov"));
  7552. settings_fov->set_select_all_on_focus(true);
  7553. settings_fov->set_tooltip_text(TTR("FOV is defined as a vertical value, as the editor camera always uses the Keep Height aspect mode."));
  7554. settings_vbc->add_margin_child(TTR("Perspective VFOV (deg.):"), settings_fov);
  7555. settings_znear = memnew(SpinBox);
  7556. settings_znear->set_max(MAX_Z);
  7557. settings_znear->set_min(MIN_Z);
  7558. settings_znear->set_step(0.01);
  7559. settings_znear->set_value(EDITOR_GET("editors/3d/default_z_near"));
  7560. settings_znear->set_select_all_on_focus(true);
  7561. settings_vbc->add_margin_child(TTR("View Z-Near:"), settings_znear);
  7562. settings_zfar = memnew(SpinBox);
  7563. settings_zfar->set_max(MAX_Z);
  7564. settings_zfar->set_min(MIN_Z);
  7565. settings_zfar->set_step(0.1);
  7566. settings_zfar->set_value(EDITOR_GET("editors/3d/default_z_far"));
  7567. settings_zfar->set_select_all_on_focus(true);
  7568. settings_vbc->add_margin_child(TTR("View Z-Far:"), settings_zfar);
  7569. for (uint32_t i = 0; i < VIEWPORTS_COUNT; ++i) {
  7570. settings_dialog->connect(SceneStringName(confirmed), callable_mp(viewports[i], &Node3DEditorViewport::_view_settings_confirmed).bind(0.0));
  7571. }
  7572. /* XFORM DIALOG */
  7573. xform_dialog = memnew(ConfirmationDialog);
  7574. xform_dialog->set_title(TTR("Transform Change"));
  7575. add_child(xform_dialog);
  7576. VBoxContainer *xform_vbc = memnew(VBoxContainer);
  7577. xform_dialog->add_child(xform_vbc);
  7578. Label *l = memnew(Label);
  7579. l->set_text(TTR("Translate:"));
  7580. xform_vbc->add_child(l);
  7581. HBoxContainer *xform_hbc = memnew(HBoxContainer);
  7582. xform_vbc->add_child(xform_hbc);
  7583. for (int i = 0; i < 3; i++) {
  7584. xform_translate[i] = memnew(LineEdit);
  7585. xform_translate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  7586. xform_translate[i]->set_select_all_on_focus(true);
  7587. xform_hbc->add_child(xform_translate[i]);
  7588. }
  7589. l = memnew(Label);
  7590. l->set_text(TTR("Rotate (deg.):"));
  7591. xform_vbc->add_child(l);
  7592. xform_hbc = memnew(HBoxContainer);
  7593. xform_vbc->add_child(xform_hbc);
  7594. for (int i = 0; i < 3; i++) {
  7595. xform_rotate[i] = memnew(LineEdit);
  7596. xform_rotate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  7597. xform_rotate[i]->set_select_all_on_focus(true);
  7598. xform_hbc->add_child(xform_rotate[i]);
  7599. }
  7600. l = memnew(Label);
  7601. l->set_text(TTR("Scale (ratio):"));
  7602. xform_vbc->add_child(l);
  7603. xform_hbc = memnew(HBoxContainer);
  7604. xform_vbc->add_child(xform_hbc);
  7605. for (int i = 0; i < 3; i++) {
  7606. xform_scale[i] = memnew(LineEdit);
  7607. xform_scale[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  7608. xform_scale[i]->set_select_all_on_focus(true);
  7609. xform_hbc->add_child(xform_scale[i]);
  7610. }
  7611. l = memnew(Label);
  7612. l->set_text(TTR("Transform Type"));
  7613. xform_vbc->add_child(l);
  7614. xform_type = memnew(OptionButton);
  7615. xform_type->set_h_size_flags(SIZE_EXPAND_FILL);
  7616. xform_type->add_item(TTR("Pre"));
  7617. xform_type->add_item(TTR("Post"));
  7618. xform_vbc->add_child(xform_type);
  7619. xform_dialog->connect(SceneStringName(confirmed), callable_mp(this, &Node3DEditor::_xform_dialog_action));
  7620. selected = nullptr;
  7621. set_process_shortcut_input(true);
  7622. add_to_group(SceneStringName(_spatial_editor_group));
  7623. current_hover_gizmo_handle = -1;
  7624. current_hover_gizmo_handle_secondary = false;
  7625. {
  7626. // Sun/preview environment popup.
  7627. sun_environ_popup = memnew(PopupPanel);
  7628. add_child(sun_environ_popup);
  7629. HBoxContainer *sun_environ_hb = memnew(HBoxContainer);
  7630. sun_environ_popup->add_child(sun_environ_hb);
  7631. sun_vb = memnew(VBoxContainer);
  7632. sun_environ_hb->add_child(sun_vb);
  7633. sun_vb->set_custom_minimum_size(Size2(200 * EDSCALE, 0));
  7634. sun_vb->hide();
  7635. sun_title = memnew(Label);
  7636. sun_title->set_theme_type_variation("HeaderMedium");
  7637. sun_vb->add_child(sun_title);
  7638. sun_title->set_text(TTR("Preview Sun"));
  7639. sun_title->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  7640. CenterContainer *sun_direction_center = memnew(CenterContainer);
  7641. sun_direction = memnew(Control);
  7642. sun_direction->set_custom_minimum_size(Size2(128, 128) * EDSCALE);
  7643. sun_direction_center->add_child(sun_direction);
  7644. sun_vb->add_margin_child(TTR("Sun Direction"), sun_direction_center);
  7645. sun_direction->connect(SceneStringName(gui_input), callable_mp(this, &Node3DEditor::_sun_direction_input));
  7646. sun_direction->connect(SceneStringName(draw), callable_mp(this, &Node3DEditor::_sun_direction_draw));
  7647. sun_direction->set_default_cursor_shape(CURSOR_MOVE);
  7648. sun_direction_shader.instantiate();
  7649. sun_direction_shader->set_code(R"(
  7650. // 3D editor Preview Sun direction shader.
  7651. shader_type canvas_item;
  7652. uniform vec3 sun_direction;
  7653. uniform vec3 sun_color;
  7654. void fragment() {
  7655. vec3 n;
  7656. n.xy = UV * 2.0 - 1.0;
  7657. n.z = sqrt(max(0.0, 1.0 - dot(n.xy, n.xy)));
  7658. COLOR.rgb = dot(n, sun_direction) * sun_color;
  7659. COLOR.a = 1.0 - smoothstep(0.99, 1.0, length(n.xy));
  7660. }
  7661. )");
  7662. sun_direction_material.instantiate();
  7663. sun_direction_material->set_shader(sun_direction_shader);
  7664. sun_direction_material->set_shader_parameter("sun_direction", Vector3(0, 0, 1));
  7665. sun_direction_material->set_shader_parameter("sun_color", Vector3(1, 1, 1));
  7666. sun_direction->set_material(sun_direction_material);
  7667. HBoxContainer *sun_angle_hbox = memnew(HBoxContainer);
  7668. sun_angle_hbox->set_h_size_flags(SIZE_EXPAND_FILL);
  7669. VBoxContainer *sun_angle_altitude_vbox = memnew(VBoxContainer);
  7670. sun_angle_altitude_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  7671. Label *sun_angle_altitude_label = memnew(Label);
  7672. sun_angle_altitude_label->set_text(TTR("Angular Altitude"));
  7673. sun_angle_altitude_vbox->add_child(sun_angle_altitude_label);
  7674. sun_angle_altitude = memnew(EditorSpinSlider);
  7675. sun_angle_altitude->set_suffix(U"\u00B0");
  7676. sun_angle_altitude->set_max(90);
  7677. sun_angle_altitude->set_min(-90);
  7678. sun_angle_altitude->set_step(0.1);
  7679. sun_angle_altitude->connect(SceneStringName(value_changed), callable_mp(this, &Node3DEditor::_sun_direction_angle_set).unbind(1));
  7680. sun_angle_altitude_vbox->add_child(sun_angle_altitude);
  7681. sun_angle_hbox->add_child(sun_angle_altitude_vbox);
  7682. VBoxContainer *sun_angle_azimuth_vbox = memnew(VBoxContainer);
  7683. sun_angle_azimuth_vbox->set_h_size_flags(SIZE_EXPAND_FILL);
  7684. sun_angle_azimuth_vbox->set_custom_minimum_size(Vector2(100, 0));
  7685. Label *sun_angle_azimuth_label = memnew(Label);
  7686. sun_angle_azimuth_label->set_text(TTR("Azimuth"));
  7687. sun_angle_azimuth_vbox->add_child(sun_angle_azimuth_label);
  7688. sun_angle_azimuth = memnew(EditorSpinSlider);
  7689. sun_angle_azimuth->set_suffix(U"\u00B0");
  7690. sun_angle_azimuth->set_max(180);
  7691. sun_angle_azimuth->set_min(-180);
  7692. sun_angle_azimuth->set_step(0.1);
  7693. sun_angle_azimuth->set_allow_greater(true);
  7694. sun_angle_azimuth->set_allow_lesser(true);
  7695. sun_angle_azimuth->connect(SceneStringName(value_changed), callable_mp(this, &Node3DEditor::_sun_direction_angle_set).unbind(1));
  7696. sun_angle_azimuth_vbox->add_child(sun_angle_azimuth);
  7697. sun_angle_hbox->add_child(sun_angle_azimuth_vbox);
  7698. sun_angle_hbox->add_theme_constant_override("separation", 10);
  7699. sun_vb->add_child(sun_angle_hbox);
  7700. sun_color = memnew(ColorPickerButton);
  7701. sun_color->set_edit_alpha(false);
  7702. sun_vb->add_margin_child(TTR("Sun Color"), sun_color);
  7703. sun_color->connect("color_changed", callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  7704. sun_color->get_popup()->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(sun_color->get_picker()));
  7705. sun_energy = memnew(EditorSpinSlider);
  7706. sun_energy->set_max(64.0);
  7707. sun_energy->set_min(0);
  7708. sun_energy->set_step(0.05);
  7709. sun_vb->add_margin_child(TTR("Sun Energy"), sun_energy);
  7710. sun_energy->connect(SceneStringName(value_changed), callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  7711. sun_max_distance = memnew(EditorSpinSlider);
  7712. sun_vb->add_margin_child(TTR("Shadow Max Distance"), sun_max_distance);
  7713. sun_max_distance->connect(SceneStringName(value_changed), callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  7714. sun_max_distance->set_min(1);
  7715. sun_max_distance->set_max(4096);
  7716. sun_add_to_scene = memnew(Button);
  7717. sun_add_to_scene->set_text(TTR("Add Sun to Scene"));
  7718. sun_add_to_scene->set_tooltip_text(TTR("Adds a DirectionalLight3D node matching the preview sun settings to the current scene.\nHold Shift while clicking to also add the preview environment to the current scene."));
  7719. sun_add_to_scene->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_add_sun_to_scene).bind(false));
  7720. sun_vb->add_spacer();
  7721. sun_vb->add_child(sun_add_to_scene);
  7722. sun_state = memnew(Label);
  7723. sun_environ_hb->add_child(sun_state);
  7724. sun_state->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  7725. sun_state->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  7726. sun_state->set_h_size_flags(SIZE_EXPAND_FILL);
  7727. VSeparator *sc = memnew(VSeparator);
  7728. sc->set_custom_minimum_size(Size2(10 * EDSCALE, 0));
  7729. sc->set_v_size_flags(SIZE_EXPAND_FILL);
  7730. sun_environ_hb->add_child(sc);
  7731. environ_vb = memnew(VBoxContainer);
  7732. sun_environ_hb->add_child(environ_vb);
  7733. environ_vb->set_custom_minimum_size(Size2(200 * EDSCALE, 0));
  7734. environ_vb->hide();
  7735. environ_title = memnew(Label);
  7736. environ_title->set_theme_type_variation("HeaderMedium");
  7737. environ_vb->add_child(environ_title);
  7738. environ_title->set_text(TTR("Preview Environment"));
  7739. environ_title->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  7740. environ_sky_color = memnew(ColorPickerButton);
  7741. environ_sky_color->set_edit_alpha(false);
  7742. environ_sky_color->connect("color_changed", callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  7743. environ_sky_color->get_popup()->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(environ_sky_color->get_picker()));
  7744. environ_vb->add_margin_child(TTR("Sky Color"), environ_sky_color);
  7745. environ_ground_color = memnew(ColorPickerButton);
  7746. environ_ground_color->connect("color_changed", callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  7747. environ_ground_color->set_edit_alpha(false);
  7748. environ_ground_color->get_popup()->connect("about_to_popup", callable_mp(EditorNode::get_singleton(), &EditorNode::setup_color_picker).bind(environ_ground_color->get_picker()));
  7749. environ_vb->add_margin_child(TTR("Ground Color"), environ_ground_color);
  7750. environ_energy = memnew(EditorSpinSlider);
  7751. environ_energy->set_max(8.0);
  7752. environ_energy->set_min(0);
  7753. environ_energy->set_step(0.05);
  7754. environ_energy->connect(SceneStringName(value_changed), callable_mp(this, &Node3DEditor::_preview_settings_changed).unbind(1));
  7755. environ_vb->add_margin_child(TTR("Sky Energy"), environ_energy);
  7756. HBoxContainer *fx_vb = memnew(HBoxContainer);
  7757. fx_vb->set_h_size_flags(SIZE_EXPAND_FILL);
  7758. environ_ao_button = memnew(Button);
  7759. environ_ao_button->set_text(TTR("AO"));
  7760. environ_ao_button->set_h_size_flags(SIZE_EXPAND_FILL);
  7761. environ_ao_button->set_toggle_mode(true);
  7762. environ_ao_button->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_preview_settings_changed), CONNECT_DEFERRED);
  7763. fx_vb->add_child(environ_ao_button);
  7764. environ_glow_button = memnew(Button);
  7765. environ_glow_button->set_text(TTR("Glow"));
  7766. environ_glow_button->set_h_size_flags(SIZE_EXPAND_FILL);
  7767. environ_glow_button->set_toggle_mode(true);
  7768. environ_glow_button->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_preview_settings_changed), CONNECT_DEFERRED);
  7769. fx_vb->add_child(environ_glow_button);
  7770. environ_tonemap_button = memnew(Button);
  7771. environ_tonemap_button->set_text(TTR("Tonemap"));
  7772. environ_tonemap_button->set_h_size_flags(SIZE_EXPAND_FILL);
  7773. environ_tonemap_button->set_toggle_mode(true);
  7774. environ_tonemap_button->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_preview_settings_changed), CONNECT_DEFERRED);
  7775. fx_vb->add_child(environ_tonemap_button);
  7776. environ_gi_button = memnew(Button);
  7777. environ_gi_button->set_text(TTR("GI"));
  7778. environ_gi_button->set_h_size_flags(SIZE_EXPAND_FILL);
  7779. environ_gi_button->set_toggle_mode(true);
  7780. environ_gi_button->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_preview_settings_changed), CONNECT_DEFERRED);
  7781. fx_vb->add_child(environ_gi_button);
  7782. environ_vb->add_margin_child(TTR("Post Process"), fx_vb);
  7783. environ_add_to_scene = memnew(Button);
  7784. environ_add_to_scene->set_text(TTR("Add Environment to Scene"));
  7785. environ_add_to_scene->set_tooltip_text(TTR("Adds a WorldEnvironment node matching the preview environment settings to the current scene.\nHold Shift while clicking to also add the preview sun to the current scene."));
  7786. environ_add_to_scene->connect(SceneStringName(pressed), callable_mp(this, &Node3DEditor::_add_environment_to_scene).bind(false));
  7787. environ_vb->add_spacer();
  7788. environ_vb->add_child(environ_add_to_scene);
  7789. environ_state = memnew(Label);
  7790. sun_environ_hb->add_child(environ_state);
  7791. environ_state->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_CENTER);
  7792. environ_state->set_vertical_alignment(VERTICAL_ALIGNMENT_CENTER);
  7793. environ_state->set_h_size_flags(SIZE_EXPAND_FILL);
  7794. preview_sun = memnew(DirectionalLight3D);
  7795. preview_sun->set_shadow(true);
  7796. preview_sun->set_shadow_mode(DirectionalLight3D::SHADOW_PARALLEL_4_SPLITS);
  7797. preview_environment = memnew(WorldEnvironment);
  7798. environment.instantiate();
  7799. preview_environment->set_environment(environment);
  7800. if (GLOBAL_GET("rendering/lights_and_shadows/use_physical_light_units")) {
  7801. camera_attributes.instantiate();
  7802. preview_environment->set_camera_attributes(camera_attributes);
  7803. }
  7804. Ref<Sky> sky;
  7805. sky.instantiate();
  7806. sky_material.instantiate();
  7807. sky->set_material(sky_material);
  7808. environment->set_sky(sky);
  7809. environment->set_background(Environment::BG_SKY);
  7810. _load_default_preview_settings();
  7811. _preview_settings_changed();
  7812. }
  7813. clear(); // Make sure values are initialized. Will call _snap_update() for us.
  7814. }
  7815. Node3DEditor::~Node3DEditor() {
  7816. singleton = nullptr;
  7817. memdelete(preview_node);
  7818. if (preview_sun_dangling && preview_sun) {
  7819. memdelete(preview_sun);
  7820. }
  7821. if (preview_env_dangling && preview_environment) {
  7822. memdelete(preview_environment);
  7823. }
  7824. }
  7825. void Node3DEditorPlugin::make_visible(bool p_visible) {
  7826. if (p_visible) {
  7827. spatial_editor->show();
  7828. spatial_editor->set_process(true);
  7829. spatial_editor->set_physics_process(true);
  7830. } else {
  7831. spatial_editor->hide();
  7832. spatial_editor->set_process(false);
  7833. spatial_editor->set_physics_process(false);
  7834. }
  7835. }
  7836. void Node3DEditorPlugin::edit(Object *p_object) {
  7837. spatial_editor->edit(Object::cast_to<Node3D>(p_object));
  7838. }
  7839. bool Node3DEditorPlugin::handles(Object *p_object) const {
  7840. return p_object->is_class("Node3D");
  7841. }
  7842. Dictionary Node3DEditorPlugin::get_state() const {
  7843. return spatial_editor->get_state();
  7844. }
  7845. void Node3DEditorPlugin::set_state(const Dictionary &p_state) {
  7846. spatial_editor->set_state(p_state);
  7847. }
  7848. Vector3 Node3DEditor::snap_point(Vector3 p_target, Vector3 p_start) const {
  7849. if (is_snap_enabled()) {
  7850. real_t snap = get_translate_snap();
  7851. p_target.snapf(snap);
  7852. }
  7853. return p_target;
  7854. }
  7855. bool Node3DEditor::is_gizmo_visible() const {
  7856. if (selected) {
  7857. return gizmo.visible && selected->is_transform_gizmo_visible();
  7858. }
  7859. return gizmo.visible;
  7860. }
  7861. real_t Node3DEditor::get_translate_snap() const {
  7862. real_t snap_value = snap_translate_value;
  7863. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  7864. snap_value /= 10.0f;
  7865. }
  7866. return snap_value;
  7867. }
  7868. real_t Node3DEditor::get_rotate_snap() const {
  7869. real_t snap_value = snap_rotate_value;
  7870. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  7871. snap_value /= 3.0f;
  7872. }
  7873. return snap_value;
  7874. }
  7875. real_t Node3DEditor::get_scale_snap() const {
  7876. real_t snap_value = snap_scale_value;
  7877. if (Input::get_singleton()->is_key_pressed(Key::SHIFT)) {
  7878. snap_value /= 2.0f;
  7879. }
  7880. return snap_value;
  7881. }
  7882. struct _GizmoPluginPriorityComparator {
  7883. bool operator()(const Ref<EditorNode3DGizmoPlugin> &p_a, const Ref<EditorNode3DGizmoPlugin> &p_b) const {
  7884. if (p_a->get_priority() == p_b->get_priority()) {
  7885. return p_a->get_gizmo_name() < p_b->get_gizmo_name();
  7886. }
  7887. return p_a->get_priority() > p_b->get_priority();
  7888. }
  7889. };
  7890. struct _GizmoPluginNameComparator {
  7891. bool operator()(const Ref<EditorNode3DGizmoPlugin> &p_a, const Ref<EditorNode3DGizmoPlugin> &p_b) const {
  7892. return p_a->get_gizmo_name() < p_b->get_gizmo_name();
  7893. }
  7894. };
  7895. void Node3DEditor::add_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin) {
  7896. ERR_FAIL_COND(p_plugin.is_null());
  7897. gizmo_plugins_by_priority.push_back(p_plugin);
  7898. gizmo_plugins_by_priority.sort_custom<_GizmoPluginPriorityComparator>();
  7899. gizmo_plugins_by_name.push_back(p_plugin);
  7900. gizmo_plugins_by_name.sort_custom<_GizmoPluginNameComparator>();
  7901. _update_gizmos_menu();
  7902. }
  7903. void Node3DEditor::remove_gizmo_plugin(Ref<EditorNode3DGizmoPlugin> p_plugin) {
  7904. gizmo_plugins_by_priority.erase(p_plugin);
  7905. gizmo_plugins_by_name.erase(p_plugin);
  7906. _update_gizmos_menu();
  7907. }
  7908. DynamicBVH::ID Node3DEditor::insert_gizmo_bvh_node(Node3D *p_node, const AABB &p_aabb) {
  7909. return gizmo_bvh.insert(p_aabb, p_node);
  7910. }
  7911. void Node3DEditor::update_gizmo_bvh_node(DynamicBVH::ID p_id, const AABB &p_aabb) {
  7912. gizmo_bvh.update(p_id, p_aabb);
  7913. gizmo_bvh.optimize_incremental(1);
  7914. }
  7915. void Node3DEditor::remove_gizmo_bvh_node(DynamicBVH::ID p_id) {
  7916. gizmo_bvh.remove(p_id);
  7917. }
  7918. Vector<Node3D *> Node3DEditor::gizmo_bvh_ray_query(const Vector3 &p_ray_start, const Vector3 &p_ray_end) {
  7919. struct Result {
  7920. Vector<Node3D *> nodes;
  7921. bool operator()(void *p_data) {
  7922. nodes.append((Node3D *)p_data);
  7923. return false;
  7924. }
  7925. } result;
  7926. gizmo_bvh.ray_query(p_ray_start, p_ray_end, result);
  7927. return result.nodes;
  7928. }
  7929. Vector<Node3D *> Node3DEditor::gizmo_bvh_frustum_query(const Vector<Plane> &p_frustum) {
  7930. Vector<Vector3> points = Geometry3D::compute_convex_mesh_points(&p_frustum[0], p_frustum.size());
  7931. struct Result {
  7932. Vector<Node3D *> nodes;
  7933. bool operator()(void *p_data) {
  7934. nodes.append((Node3D *)p_data);
  7935. return false;
  7936. }
  7937. } result;
  7938. gizmo_bvh.convex_query(p_frustum.ptr(), p_frustum.size(), points.ptr(), points.size(), result);
  7939. return result.nodes;
  7940. }
  7941. Node3DEditorPlugin::Node3DEditorPlugin() {
  7942. spatial_editor = memnew(Node3DEditor);
  7943. spatial_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  7944. EditorNode::get_singleton()->get_editor_main_screen()->get_control()->add_child(spatial_editor);
  7945. spatial_editor->hide();
  7946. }
  7947. Node3DEditorPlugin::~Node3DEditorPlugin() {
  7948. }