spatial_editor_plugin.cpp 192 KB

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