spatial_editor_plugin.cpp 227 KB

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