spatial_editor_plugin.cpp 191 KB

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