spatial_editor_plugin.cpp 192 KB

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