node_3d_editor_gizmos.cpp 180 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796
  1. /*************************************************************************/
  2. /* node_3d_editor_gizmos.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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 "node_3d_editor_gizmos.h"
  31. #include "core/math/convex_hull.h"
  32. #include "core/math/geometry_2d.h"
  33. #include "core/math/geometry_3d.h"
  34. #include "editor/editor_node.h"
  35. #include "editor/editor_settings.h"
  36. #include "editor/editor_undo_redo_manager.h"
  37. #include "editor/plugins/node_3d_editor_plugin.h"
  38. #include "scene/3d/audio_listener_3d.h"
  39. #include "scene/3d/audio_stream_player_3d.h"
  40. #include "scene/3d/camera_3d.h"
  41. #include "scene/3d/collision_polygon_3d.h"
  42. #include "scene/3d/collision_shape_3d.h"
  43. #include "scene/3d/cpu_particles_3d.h"
  44. #include "scene/3d/decal.h"
  45. #include "scene/3d/fog_volume.h"
  46. #include "scene/3d/gpu_particles_3d.h"
  47. #include "scene/3d/gpu_particles_collision_3d.h"
  48. #include "scene/3d/joint_3d.h"
  49. #include "scene/3d/label_3d.h"
  50. #include "scene/3d/light_3d.h"
  51. #include "scene/3d/lightmap_gi.h"
  52. #include "scene/3d/lightmap_probe.h"
  53. #include "scene/3d/marker_3d.h"
  54. #include "scene/3d/mesh_instance_3d.h"
  55. #include "scene/3d/navigation_region_3d.h"
  56. #include "scene/3d/occluder_instance_3d.h"
  57. #include "scene/3d/ray_cast_3d.h"
  58. #include "scene/3d/reflection_probe.h"
  59. #include "scene/3d/shape_cast_3d.h"
  60. #include "scene/3d/soft_body_3d.h"
  61. #include "scene/3d/spring_arm_3d.h"
  62. #include "scene/3d/sprite_3d.h"
  63. #include "scene/3d/vehicle_body_3d.h"
  64. #include "scene/3d/visible_on_screen_notifier_3d.h"
  65. #include "scene/3d/voxel_gi.h"
  66. #include "scene/resources/box_shape_3d.h"
  67. #include "scene/resources/capsule_shape_3d.h"
  68. #include "scene/resources/concave_polygon_shape_3d.h"
  69. #include "scene/resources/convex_polygon_shape_3d.h"
  70. #include "scene/resources/cylinder_shape_3d.h"
  71. #include "scene/resources/height_map_shape_3d.h"
  72. #include "scene/resources/primitive_meshes.h"
  73. #include "scene/resources/separation_ray_shape_3d.h"
  74. #include "scene/resources/sphere_shape_3d.h"
  75. #include "scene/resources/surface_tool.h"
  76. #include "scene/resources/world_boundary_shape_3d.h"
  77. #include "servers/navigation_server_3d.h"
  78. #define HANDLE_HALF_SIZE 9.5
  79. bool EditorNode3DGizmo::is_editable() const {
  80. ERR_FAIL_COND_V(!spatial_node, false);
  81. Node *edited_root = spatial_node->get_tree()->get_edited_scene_root();
  82. if (spatial_node == edited_root) {
  83. return true;
  84. }
  85. if (spatial_node->get_owner() == edited_root) {
  86. return true;
  87. }
  88. if (edited_root->is_editable_instance(spatial_node->get_owner())) {
  89. return true;
  90. }
  91. return false;
  92. }
  93. void EditorNode3DGizmo::clear() {
  94. for (int i = 0; i < instances.size(); i++) {
  95. if (instances[i].instance.is_valid()) {
  96. RS::get_singleton()->free(instances[i].instance);
  97. }
  98. }
  99. billboard_handle = false;
  100. collision_segments.clear();
  101. collision_mesh = Ref<TriangleMesh>();
  102. instances.clear();
  103. handles.clear();
  104. secondary_handles.clear();
  105. }
  106. void EditorNode3DGizmo::redraw() {
  107. if (!GDVIRTUAL_CALL(_redraw)) {
  108. ERR_FAIL_COND(!gizmo_plugin);
  109. gizmo_plugin->redraw(this);
  110. }
  111. if (Node3DEditor::get_singleton()->is_current_selected_gizmo(this)) {
  112. Node3DEditor::get_singleton()->update_transform_gizmo();
  113. }
  114. }
  115. String EditorNode3DGizmo::get_handle_name(int p_id, bool p_secondary) const {
  116. String ret;
  117. if (GDVIRTUAL_CALL(_get_handle_name, p_id, p_secondary, ret)) {
  118. return ret;
  119. }
  120. ERR_FAIL_COND_V(!gizmo_plugin, "");
  121. return gizmo_plugin->get_handle_name(this, p_id, p_secondary);
  122. }
  123. bool EditorNode3DGizmo::is_handle_highlighted(int p_id, bool p_secondary) const {
  124. bool success;
  125. if (GDVIRTUAL_CALL(_is_handle_highlighted, p_id, p_secondary, success)) {
  126. return success;
  127. }
  128. ERR_FAIL_COND_V(!gizmo_plugin, false);
  129. return gizmo_plugin->is_handle_highlighted(this, p_id, p_secondary);
  130. }
  131. Variant EditorNode3DGizmo::get_handle_value(int p_id, bool p_secondary) const {
  132. Variant value;
  133. if (GDVIRTUAL_CALL(_get_handle_value, p_id, p_secondary, value)) {
  134. return value;
  135. }
  136. ERR_FAIL_COND_V(!gizmo_plugin, Variant());
  137. return gizmo_plugin->get_handle_value(this, p_id, p_secondary);
  138. }
  139. void EditorNode3DGizmo::set_handle(int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  140. if (GDVIRTUAL_CALL(_set_handle, p_id, p_secondary, p_camera, p_point)) {
  141. return;
  142. }
  143. ERR_FAIL_COND(!gizmo_plugin);
  144. gizmo_plugin->set_handle(this, p_id, p_secondary, p_camera, p_point);
  145. }
  146. void EditorNode3DGizmo::commit_handle(int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  147. if (GDVIRTUAL_CALL(_commit_handle, p_id, p_secondary, p_restore, p_cancel)) {
  148. return;
  149. }
  150. ERR_FAIL_COND(!gizmo_plugin);
  151. gizmo_plugin->commit_handle(this, p_id, p_secondary, p_restore, p_cancel);
  152. }
  153. int EditorNode3DGizmo::subgizmos_intersect_ray(Camera3D *p_camera, const Vector2 &p_point) const {
  154. int id;
  155. if (GDVIRTUAL_CALL(_subgizmos_intersect_ray, p_camera, p_point, id)) {
  156. return id;
  157. }
  158. ERR_FAIL_COND_V(!gizmo_plugin, -1);
  159. return gizmo_plugin->subgizmos_intersect_ray(this, p_camera, p_point);
  160. }
  161. Vector<int> EditorNode3DGizmo::subgizmos_intersect_frustum(const Camera3D *p_camera, const Vector<Plane> &p_frustum) const {
  162. TypedArray<Plane> frustum;
  163. frustum.resize(p_frustum.size());
  164. for (int i = 0; i < p_frustum.size(); i++) {
  165. frustum[i] = p_frustum[i];
  166. }
  167. Vector<int> ret;
  168. if (GDVIRTUAL_CALL(_subgizmos_intersect_frustum, p_camera, frustum, ret)) {
  169. return ret;
  170. }
  171. ERR_FAIL_COND_V(!gizmo_plugin, Vector<int>());
  172. return gizmo_plugin->subgizmos_intersect_frustum(this, p_camera, p_frustum);
  173. }
  174. Transform3D EditorNode3DGizmo::get_subgizmo_transform(int p_id) const {
  175. Transform3D ret;
  176. if (GDVIRTUAL_CALL(_get_subgizmo_transform, p_id, ret)) {
  177. return ret;
  178. }
  179. ERR_FAIL_COND_V(!gizmo_plugin, Transform3D());
  180. return gizmo_plugin->get_subgizmo_transform(this, p_id);
  181. }
  182. void EditorNode3DGizmo::set_subgizmo_transform(int p_id, Transform3D p_transform) {
  183. if (GDVIRTUAL_CALL(_set_subgizmo_transform, p_id, p_transform)) {
  184. return;
  185. }
  186. ERR_FAIL_COND(!gizmo_plugin);
  187. gizmo_plugin->set_subgizmo_transform(this, p_id, p_transform);
  188. }
  189. void EditorNode3DGizmo::commit_subgizmos(const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {
  190. TypedArray<Transform3D> restore;
  191. restore.resize(p_restore.size());
  192. for (int i = 0; i < p_restore.size(); i++) {
  193. restore[i] = p_restore[i];
  194. }
  195. if (GDVIRTUAL_CALL(_commit_subgizmos, p_ids, restore, p_cancel)) {
  196. return;
  197. }
  198. ERR_FAIL_COND(!gizmo_plugin);
  199. gizmo_plugin->commit_subgizmos(this, p_ids, p_restore, p_cancel);
  200. }
  201. void EditorNode3DGizmo::set_spatial_node(Node3D *p_node) {
  202. ERR_FAIL_NULL(p_node);
  203. spatial_node = p_node;
  204. }
  205. void EditorNode3DGizmo::Instance::create_instance(Node3D *p_base, bool p_hidden) {
  206. instance = RS::get_singleton()->instance_create2(mesh->get_rid(), p_base->get_world_3d()->get_scenario());
  207. RS::get_singleton()->instance_attach_object_instance_id(instance, p_base->get_instance_id());
  208. if (skin_reference.is_valid()) {
  209. RS::get_singleton()->instance_attach_skeleton(instance, skin_reference->get_skeleton());
  210. }
  211. if (extra_margin) {
  212. RS::get_singleton()->instance_set_extra_visibility_margin(instance, 1);
  213. }
  214. RS::get_singleton()->instance_geometry_set_cast_shadows_setting(instance, RS::SHADOW_CASTING_SETTING_OFF);
  215. int layer = p_hidden ? 0 : 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER;
  216. RS::get_singleton()->instance_set_layer_mask(instance, layer); //gizmos are 26
  217. RS::get_singleton()->instance_geometry_set_flag(instance, RS::INSTANCE_FLAG_IGNORE_OCCLUSION_CULLING, true);
  218. RS::get_singleton()->instance_geometry_set_flag(instance, RS::INSTANCE_FLAG_USE_BAKED_LIGHT, false);
  219. }
  220. void EditorNode3DGizmo::add_mesh(const Ref<Mesh> &p_mesh, const Ref<Material> &p_material, const Transform3D &p_xform, const Ref<SkinReference> &p_skin_reference) {
  221. ERR_FAIL_COND(!spatial_node);
  222. ERR_FAIL_COND_MSG(!p_mesh.is_valid(), "EditorNode3DGizmo.add_mesh() requires a valid Mesh resource.");
  223. Instance ins;
  224. ins.mesh = p_mesh;
  225. ins.skin_reference = p_skin_reference;
  226. ins.material = p_material;
  227. ins.xform = p_xform;
  228. if (valid) {
  229. ins.create_instance(spatial_node, hidden);
  230. RS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform() * ins.xform);
  231. if (ins.material.is_valid()) {
  232. RS::get_singleton()->instance_geometry_set_material_override(ins.instance, p_material->get_rid());
  233. }
  234. }
  235. instances.push_back(ins);
  236. }
  237. void EditorNode3DGizmo::add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material, bool p_billboard, const Color &p_modulate) {
  238. add_vertices(p_lines, p_material, Mesh::PRIMITIVE_LINES, p_billboard, p_modulate);
  239. }
  240. void EditorNode3DGizmo::add_vertices(const Vector<Vector3> &p_vertices, const Ref<Material> &p_material, Mesh::PrimitiveType p_primitive_type, bool p_billboard, const Color &p_modulate) {
  241. if (p_vertices.is_empty()) {
  242. return;
  243. }
  244. ERR_FAIL_COND(!spatial_node);
  245. Instance ins;
  246. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  247. Array a;
  248. a.resize(Mesh::ARRAY_MAX);
  249. a[Mesh::ARRAY_VERTEX] = p_vertices;
  250. Vector<Color> color;
  251. color.resize(p_vertices.size());
  252. {
  253. Color *w = color.ptrw();
  254. for (int i = 0; i < p_vertices.size(); i++) {
  255. if (is_selected()) {
  256. w[i] = Color(1, 1, 1, 0.8) * p_modulate;
  257. } else {
  258. w[i] = Color(1, 1, 1, 0.2) * p_modulate;
  259. }
  260. }
  261. }
  262. a[Mesh::ARRAY_COLOR] = color;
  263. mesh->add_surface_from_arrays(p_primitive_type, a);
  264. mesh->surface_set_material(0, p_material);
  265. if (p_billboard) {
  266. float md = 0;
  267. for (int i = 0; i < p_vertices.size(); i++) {
  268. md = MAX(0, p_vertices[i].length());
  269. }
  270. if (md) {
  271. mesh->set_custom_aabb(AABB(Vector3(-md, -md, -md), Vector3(md, md, md) * 2.0));
  272. }
  273. }
  274. ins.mesh = mesh;
  275. if (valid) {
  276. ins.create_instance(spatial_node, hidden);
  277. RS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  278. }
  279. instances.push_back(ins);
  280. }
  281. void EditorNode3DGizmo::add_unscaled_billboard(const Ref<Material> &p_material, real_t p_scale, const Color &p_modulate) {
  282. ERR_FAIL_COND(!spatial_node);
  283. Instance ins;
  284. Vector<Vector3> vs = {
  285. Vector3(-p_scale, p_scale, 0),
  286. Vector3(p_scale, p_scale, 0),
  287. Vector3(p_scale, -p_scale, 0),
  288. Vector3(-p_scale, -p_scale, 0)
  289. };
  290. Vector<Vector2> uv = {
  291. Vector2(0, 0),
  292. Vector2(1, 0),
  293. Vector2(1, 1),
  294. Vector2(0, 1)
  295. };
  296. Vector<Color> colors = {
  297. p_modulate,
  298. p_modulate,
  299. p_modulate,
  300. p_modulate
  301. };
  302. Vector<int> indices = { 0, 1, 2, 0, 2, 3 };
  303. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  304. Array a;
  305. a.resize(Mesh::ARRAY_MAX);
  306. a[Mesh::ARRAY_VERTEX] = vs;
  307. a[Mesh::ARRAY_TEX_UV] = uv;
  308. a[Mesh::ARRAY_INDEX] = indices;
  309. a[Mesh::ARRAY_COLOR] = colors;
  310. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);
  311. mesh->surface_set_material(0, p_material);
  312. float md = 0;
  313. for (int i = 0; i < vs.size(); i++) {
  314. md = MAX(0, vs[i].length());
  315. }
  316. if (md) {
  317. mesh->set_custom_aabb(AABB(Vector3(-md, -md, -md), Vector3(md, md, md) * 2.0));
  318. }
  319. selectable_icon_size = p_scale;
  320. mesh->set_custom_aabb(AABB(Vector3(-selectable_icon_size, -selectable_icon_size, -selectable_icon_size) * 100.0f, Vector3(selectable_icon_size, selectable_icon_size, selectable_icon_size) * 200.0f));
  321. ins.mesh = mesh;
  322. if (valid) {
  323. ins.create_instance(spatial_node, hidden);
  324. RS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  325. }
  326. selectable_icon_size = p_scale;
  327. instances.push_back(ins);
  328. }
  329. void EditorNode3DGizmo::add_collision_triangles(const Ref<TriangleMesh> &p_tmesh) {
  330. collision_mesh = p_tmesh;
  331. }
  332. void EditorNode3DGizmo::add_collision_segments(const Vector<Vector3> &p_lines) {
  333. int from = collision_segments.size();
  334. collision_segments.resize(from + p_lines.size());
  335. for (int i = 0; i < p_lines.size(); i++) {
  336. collision_segments.write[from + i] = p_lines[i];
  337. }
  338. }
  339. void EditorNode3DGizmo::add_handles(const Vector<Vector3> &p_handles, const Ref<Material> &p_material, const Vector<int> &p_ids, bool p_billboard, bool p_secondary) {
  340. billboard_handle = p_billboard;
  341. if (!is_selected() || !is_editable()) {
  342. return;
  343. }
  344. ERR_FAIL_COND(!spatial_node);
  345. if (p_ids.is_empty()) {
  346. ERR_FAIL_COND_MSG((!handles.is_empty() && !handle_ids.is_empty()) || (!secondary_handles.is_empty() && !secondary_handle_ids.is_empty()), "Fail");
  347. } else {
  348. ERR_FAIL_COND_MSG(handles.size() != handle_ids.size() || secondary_handles.size() != secondary_handle_ids.size(), "Fail");
  349. }
  350. bool is_current_hover_gizmo = Node3DEditor::get_singleton()->get_current_hover_gizmo() == this;
  351. bool current_hover_handle_secondary;
  352. int current_hover_handle = Node3DEditor::get_singleton()->get_current_hover_gizmo_handle(current_hover_handle_secondary);
  353. Instance ins;
  354. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  355. Array a;
  356. a.resize(RS::ARRAY_MAX);
  357. a[RS::ARRAY_VERTEX] = p_handles;
  358. Vector<Color> colors;
  359. {
  360. colors.resize(p_handles.size());
  361. Color *w = colors.ptrw();
  362. for (int i = 0; i < p_handles.size(); i++) {
  363. Color col(1, 1, 1, 1);
  364. if (is_handle_highlighted(i, p_secondary)) {
  365. col = Color(0, 0, 1, 0.9);
  366. }
  367. int id = p_ids.is_empty() ? i : p_ids[i];
  368. if (!is_current_hover_gizmo || current_hover_handle != id || p_secondary != current_hover_handle_secondary) {
  369. col.a = 0.8;
  370. }
  371. w[i] = col;
  372. }
  373. }
  374. a[RS::ARRAY_COLOR] = colors;
  375. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, a);
  376. mesh->surface_set_material(0, p_material);
  377. if (p_billboard) {
  378. float md = 0;
  379. for (int i = 0; i < p_handles.size(); i++) {
  380. md = MAX(0, p_handles[i].length());
  381. }
  382. if (md) {
  383. mesh->set_custom_aabb(AABB(Vector3(-md, -md, -md), Vector3(md, md, md) * 2.0));
  384. }
  385. }
  386. ins.mesh = mesh;
  387. ins.extra_margin = true;
  388. if (valid) {
  389. ins.create_instance(spatial_node, hidden);
  390. RS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  391. }
  392. instances.push_back(ins);
  393. Vector<Vector3> &h = p_secondary ? secondary_handles : handles;
  394. int current_size = h.size();
  395. h.resize(current_size + p_handles.size());
  396. for (int i = 0; i < p_handles.size(); i++) {
  397. h.write[current_size + i] = p_handles[i];
  398. }
  399. if (!p_ids.is_empty()) {
  400. Vector<int> &ids = p_secondary ? secondary_handle_ids : handle_ids;
  401. current_size = ids.size();
  402. ids.resize(current_size + p_ids.size());
  403. for (int i = 0; i < p_ids.size(); i++) {
  404. ids.write[current_size + i] = p_ids[i];
  405. }
  406. }
  407. }
  408. void EditorNode3DGizmo::add_solid_box(const Ref<Material> &p_material, Vector3 p_size, Vector3 p_position, const Transform3D &p_xform) {
  409. ERR_FAIL_COND(!spatial_node);
  410. BoxMesh box_mesh;
  411. box_mesh.set_size(p_size);
  412. Array arrays = box_mesh.surface_get_arrays(0);
  413. PackedVector3Array vertex = arrays[RS::ARRAY_VERTEX];
  414. Vector3 *w = vertex.ptrw();
  415. for (int i = 0; i < vertex.size(); ++i) {
  416. w[i] += p_position;
  417. }
  418. arrays[RS::ARRAY_VERTEX] = vertex;
  419. Ref<ArrayMesh> m = memnew(ArrayMesh);
  420. m->add_surface_from_arrays(box_mesh.surface_get_primitive_type(0), arrays);
  421. add_mesh(m, p_material, p_xform);
  422. }
  423. bool EditorNode3DGizmo::intersect_frustum(const Camera3D *p_camera, const Vector<Plane> &p_frustum) {
  424. ERR_FAIL_COND_V(!spatial_node, false);
  425. ERR_FAIL_COND_V(!valid, false);
  426. if (hidden && !gizmo_plugin->is_selectable_when_hidden()) {
  427. return false;
  428. }
  429. if (selectable_icon_size > 0.0f) {
  430. Vector3 origin = spatial_node->get_global_transform().get_origin();
  431. const Plane *p = p_frustum.ptr();
  432. int fc = p_frustum.size();
  433. bool any_out = false;
  434. for (int j = 0; j < fc; j++) {
  435. if (p[j].is_point_over(origin)) {
  436. any_out = true;
  437. break;
  438. }
  439. }
  440. return !any_out;
  441. }
  442. if (collision_segments.size()) {
  443. const Plane *p = p_frustum.ptr();
  444. int fc = p_frustum.size();
  445. int vc = collision_segments.size();
  446. const Vector3 *vptr = collision_segments.ptr();
  447. Transform3D t = spatial_node->get_global_transform();
  448. bool any_out = false;
  449. for (int j = 0; j < fc; j++) {
  450. for (int i = 0; i < vc; i++) {
  451. Vector3 v = t.xform(vptr[i]);
  452. if (p[j].is_point_over(v)) {
  453. any_out = true;
  454. break;
  455. }
  456. }
  457. if (any_out) {
  458. break;
  459. }
  460. }
  461. if (!any_out) {
  462. return true;
  463. }
  464. }
  465. if (collision_mesh.is_valid()) {
  466. Transform3D t = spatial_node->get_global_transform();
  467. Vector3 mesh_scale = t.get_basis().get_scale();
  468. t.orthonormalize();
  469. Transform3D it = t.affine_inverse();
  470. Vector<Plane> transformed_frustum;
  471. int plane_count = p_frustum.size();
  472. transformed_frustum.resize(plane_count);
  473. for (int i = 0; i < plane_count; i++) {
  474. transformed_frustum.write[i] = it.xform(p_frustum[i]);
  475. }
  476. Vector<Vector3> convex_points = Geometry3D::compute_convex_mesh_points(transformed_frustum.ptr(), plane_count);
  477. if (collision_mesh->inside_convex_shape(transformed_frustum.ptr(), plane_count, convex_points.ptr(), convex_points.size(), mesh_scale)) {
  478. return true;
  479. }
  480. }
  481. return false;
  482. }
  483. void EditorNode3DGizmo::handles_intersect_ray(Camera3D *p_camera, const Vector2 &p_point, bool p_shift_pressed, int &r_id, bool &r_secondary) {
  484. r_id = -1;
  485. r_secondary = false;
  486. ERR_FAIL_COND(!spatial_node);
  487. ERR_FAIL_COND(!valid);
  488. if (hidden) {
  489. return;
  490. }
  491. Transform3D camera_xform = p_camera->get_global_transform();
  492. Transform3D t = spatial_node->get_global_transform();
  493. if (billboard_handle) {
  494. t.set_look_at(t.origin, t.origin - camera_xform.basis.get_column(2), camera_xform.basis.get_column(1));
  495. }
  496. float min_d = 1e20;
  497. for (int i = 0; i < secondary_handles.size(); i++) {
  498. Vector3 hpos = t.xform(secondary_handles[i]);
  499. Vector2 p = p_camera->unproject_position(hpos);
  500. if (p.distance_to(p_point) < HANDLE_HALF_SIZE) {
  501. real_t dp = p_camera->get_transform().origin.distance_to(hpos);
  502. if (dp < min_d) {
  503. min_d = dp;
  504. if (secondary_handle_ids.is_empty()) {
  505. r_id = i;
  506. } else {
  507. r_id = secondary_handle_ids[i];
  508. }
  509. r_secondary = true;
  510. }
  511. }
  512. }
  513. if (r_id != -1 && p_shift_pressed) {
  514. return;
  515. }
  516. min_d = 1e20;
  517. for (int i = 0; i < handles.size(); i++) {
  518. Vector3 hpos = t.xform(handles[i]);
  519. Vector2 p = p_camera->unproject_position(hpos);
  520. if (p.distance_to(p_point) < HANDLE_HALF_SIZE) {
  521. real_t dp = p_camera->get_transform().origin.distance_to(hpos);
  522. if (dp < min_d) {
  523. min_d = dp;
  524. if (handle_ids.is_empty()) {
  525. r_id = i;
  526. } else {
  527. r_id = handle_ids[i];
  528. }
  529. r_secondary = false;
  530. }
  531. }
  532. }
  533. }
  534. bool EditorNode3DGizmo::intersect_ray(Camera3D *p_camera, const Point2 &p_point, Vector3 &r_pos, Vector3 &r_normal) {
  535. ERR_FAIL_COND_V(!spatial_node, false);
  536. ERR_FAIL_COND_V(!valid, false);
  537. if (hidden && !gizmo_plugin->is_selectable_when_hidden()) {
  538. return false;
  539. }
  540. if (selectable_icon_size > 0.0f) {
  541. Transform3D t = spatial_node->get_global_transform();
  542. Vector3 camera_position = p_camera->get_camera_transform().origin;
  543. if (!camera_position.is_equal_approx(t.origin)) {
  544. t.set_look_at(t.origin, camera_position);
  545. }
  546. float scale = t.origin.distance_to(p_camera->get_camera_transform().origin);
  547. if (p_camera->get_projection() == Camera3D::PROJECTION_ORTHOGONAL) {
  548. float aspect = p_camera->get_viewport()->get_visible_rect().size.aspect();
  549. float size = p_camera->get_size();
  550. scale = size / aspect;
  551. }
  552. Point2 center = p_camera->unproject_position(t.origin);
  553. Transform3D orig_camera_transform = p_camera->get_camera_transform();
  554. if (!orig_camera_transform.origin.is_equal_approx(t.origin) &&
  555. ABS(orig_camera_transform.basis.get_column(Vector3::AXIS_Z).dot(Vector3(0, 1, 0))) < 0.99) {
  556. p_camera->look_at(t.origin);
  557. }
  558. Vector3 c0 = t.xform(Vector3(selectable_icon_size, selectable_icon_size, 0) * scale);
  559. Vector3 c1 = t.xform(Vector3(-selectable_icon_size, -selectable_icon_size, 0) * scale);
  560. Point2 p0 = p_camera->unproject_position(c0);
  561. Point2 p1 = p_camera->unproject_position(c1);
  562. p_camera->set_global_transform(orig_camera_transform);
  563. Rect2 rect(p0, (p1 - p0).abs());
  564. rect.set_position(center - rect.get_size() / 2.0);
  565. if (rect.has_point(p_point)) {
  566. r_pos = t.origin;
  567. r_normal = -p_camera->project_ray_normal(p_point);
  568. return true;
  569. }
  570. }
  571. if (collision_segments.size()) {
  572. Plane camp(-p_camera->get_transform().basis.get_column(2).normalized(), p_camera->get_transform().origin);
  573. int vc = collision_segments.size();
  574. const Vector3 *vptr = collision_segments.ptr();
  575. Transform3D t = spatial_node->get_global_transform();
  576. if (billboard_handle) {
  577. t.set_look_at(t.origin, t.origin - p_camera->get_transform().basis.get_column(2), p_camera->get_transform().basis.get_column(1));
  578. }
  579. Vector3 cp;
  580. float cpd = 1e20;
  581. for (int i = 0; i < vc / 2; i++) {
  582. Vector3 a = t.xform(vptr[i * 2 + 0]);
  583. Vector3 b = t.xform(vptr[i * 2 + 1]);
  584. Vector2 s[2];
  585. s[0] = p_camera->unproject_position(a);
  586. s[1] = p_camera->unproject_position(b);
  587. Vector2 p = Geometry2D::get_closest_point_to_segment(p_point, s);
  588. float pd = p.distance_to(p_point);
  589. if (pd < cpd) {
  590. float d = s[0].distance_to(s[1]);
  591. Vector3 tcp;
  592. if (d > 0) {
  593. float d2 = s[0].distance_to(p) / d;
  594. tcp = a + (b - a) * d2;
  595. } else {
  596. tcp = a;
  597. }
  598. if (camp.distance_to(tcp) < p_camera->get_near()) {
  599. continue;
  600. }
  601. cp = tcp;
  602. cpd = pd;
  603. }
  604. }
  605. if (cpd < 8) {
  606. r_pos = cp;
  607. r_normal = -p_camera->project_ray_normal(p_point);
  608. return true;
  609. }
  610. }
  611. if (collision_mesh.is_valid()) {
  612. Transform3D gt = spatial_node->get_global_transform();
  613. if (billboard_handle) {
  614. gt.set_look_at(gt.origin, gt.origin - p_camera->get_transform().basis.get_column(2), p_camera->get_transform().basis.get_column(1));
  615. }
  616. Transform3D ai = gt.affine_inverse();
  617. Vector3 ray_from = ai.xform(p_camera->project_ray_origin(p_point));
  618. Vector3 ray_dir = ai.basis.xform(p_camera->project_ray_normal(p_point)).normalized();
  619. Vector3 rpos, rnorm;
  620. if (collision_mesh->intersect_ray(ray_from, ray_dir, rpos, rnorm)) {
  621. r_pos = gt.xform(rpos);
  622. r_normal = gt.basis.xform(rnorm).normalized();
  623. return true;
  624. }
  625. }
  626. return false;
  627. }
  628. bool EditorNode3DGizmo::is_subgizmo_selected(int p_id) const {
  629. Node3DEditor *ed = Node3DEditor::get_singleton();
  630. ERR_FAIL_COND_V(!ed, false);
  631. return ed->is_current_selected_gizmo(this) && ed->is_subgizmo_selected(p_id);
  632. }
  633. Vector<int> EditorNode3DGizmo::get_subgizmo_selection() const {
  634. Vector<int> ret;
  635. Node3DEditor *ed = Node3DEditor::get_singleton();
  636. ERR_FAIL_COND_V(!ed, ret);
  637. if (ed->is_current_selected_gizmo(this)) {
  638. ret = ed->get_subgizmo_selection();
  639. }
  640. return ret;
  641. }
  642. void EditorNode3DGizmo::create() {
  643. ERR_FAIL_COND(!spatial_node);
  644. ERR_FAIL_COND(valid);
  645. valid = true;
  646. for (int i = 0; i < instances.size(); i++) {
  647. instances.write[i].create_instance(spatial_node, hidden);
  648. }
  649. transform();
  650. }
  651. void EditorNode3DGizmo::transform() {
  652. ERR_FAIL_COND(!spatial_node);
  653. ERR_FAIL_COND(!valid);
  654. for (int i = 0; i < instances.size(); i++) {
  655. RS::get_singleton()->instance_set_transform(instances[i].instance, spatial_node->get_global_transform() * instances[i].xform);
  656. }
  657. }
  658. void EditorNode3DGizmo::free() {
  659. ERR_FAIL_COND(!spatial_node);
  660. ERR_FAIL_COND(!valid);
  661. for (int i = 0; i < instances.size(); i++) {
  662. if (instances[i].instance.is_valid()) {
  663. RS::get_singleton()->free(instances[i].instance);
  664. }
  665. instances.write[i].instance = RID();
  666. }
  667. clear();
  668. valid = false;
  669. }
  670. void EditorNode3DGizmo::set_hidden(bool p_hidden) {
  671. hidden = p_hidden;
  672. int layer = hidden ? 0 : 1 << Node3DEditorViewport::GIZMO_EDIT_LAYER;
  673. for (int i = 0; i < instances.size(); ++i) {
  674. RS::get_singleton()->instance_set_layer_mask(instances[i].instance, layer);
  675. }
  676. }
  677. void EditorNode3DGizmo::set_plugin(EditorNode3DGizmoPlugin *p_plugin) {
  678. gizmo_plugin = p_plugin;
  679. }
  680. void EditorNode3DGizmo::_bind_methods() {
  681. ClassDB::bind_method(D_METHOD("add_lines", "lines", "material", "billboard", "modulate"), &EditorNode3DGizmo::add_lines, DEFVAL(false), DEFVAL(Color(1, 1, 1)));
  682. ClassDB::bind_method(D_METHOD("add_mesh", "mesh", "material", "transform", "skeleton"), &EditorNode3DGizmo::add_mesh, DEFVAL(Variant()), DEFVAL(Transform3D()), DEFVAL(Ref<SkinReference>()));
  683. ClassDB::bind_method(D_METHOD("add_collision_segments", "segments"), &EditorNode3DGizmo::add_collision_segments);
  684. ClassDB::bind_method(D_METHOD("add_collision_triangles", "triangles"), &EditorNode3DGizmo::add_collision_triangles);
  685. ClassDB::bind_method(D_METHOD("add_unscaled_billboard", "material", "default_scale", "modulate"), &EditorNode3DGizmo::add_unscaled_billboard, DEFVAL(1), DEFVAL(Color(1, 1, 1)));
  686. ClassDB::bind_method(D_METHOD("add_handles", "handles", "material", "ids", "billboard", "secondary"), &EditorNode3DGizmo::add_handles, DEFVAL(false), DEFVAL(false));
  687. ClassDB::bind_method(D_METHOD("set_spatial_node", "node"), &EditorNode3DGizmo::_set_spatial_node);
  688. ClassDB::bind_method(D_METHOD("get_spatial_node"), &EditorNode3DGizmo::get_spatial_node);
  689. ClassDB::bind_method(D_METHOD("get_plugin"), &EditorNode3DGizmo::get_plugin);
  690. ClassDB::bind_method(D_METHOD("clear"), &EditorNode3DGizmo::clear);
  691. ClassDB::bind_method(D_METHOD("set_hidden", "hidden"), &EditorNode3DGizmo::set_hidden);
  692. ClassDB::bind_method(D_METHOD("is_subgizmo_selected", "id"), &EditorNode3DGizmo::is_subgizmo_selected);
  693. ClassDB::bind_method(D_METHOD("get_subgizmo_selection"), &EditorNode3DGizmo::get_subgizmo_selection);
  694. GDVIRTUAL_BIND(_redraw);
  695. GDVIRTUAL_BIND(_get_handle_name, "id", "secondary");
  696. GDVIRTUAL_BIND(_is_handle_highlighted, "id", "secondary");
  697. GDVIRTUAL_BIND(_get_handle_value, "id", "secondary");
  698. GDVIRTUAL_BIND(_set_handle, "id", "secondary", "camera", "point");
  699. GDVIRTUAL_BIND(_commit_handle, "id", "secondary", "restore", "cancel");
  700. GDVIRTUAL_BIND(_subgizmos_intersect_ray, "camera", "point");
  701. GDVIRTUAL_BIND(_subgizmos_intersect_frustum, "camera", "frustum");
  702. GDVIRTUAL_BIND(_set_subgizmo_transform, "id", "transform");
  703. GDVIRTUAL_BIND(_get_subgizmo_transform, "id");
  704. GDVIRTUAL_BIND(_commit_subgizmos, "ids", "restores", "cancel");
  705. }
  706. EditorNode3DGizmo::EditorNode3DGizmo() {
  707. valid = false;
  708. billboard_handle = false;
  709. hidden = false;
  710. selected = false;
  711. spatial_node = nullptr;
  712. gizmo_plugin = nullptr;
  713. selectable_icon_size = -1.0f;
  714. }
  715. EditorNode3DGizmo::~EditorNode3DGizmo() {
  716. if (gizmo_plugin != nullptr) {
  717. gizmo_plugin->unregister_gizmo(this);
  718. }
  719. clear();
  720. }
  721. /////
  722. void EditorNode3DGizmoPlugin::create_material(const String &p_name, const Color &p_color, bool p_billboard, bool p_on_top, bool p_use_vertex_color) {
  723. Color instantiated_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/instantiated");
  724. Vector<Ref<StandardMaterial3D>> mats;
  725. for (int i = 0; i < 4; i++) {
  726. bool selected = i % 2 == 1;
  727. bool instantiated = i < 2;
  728. Ref<StandardMaterial3D> material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  729. Color color = instantiated ? instantiated_color : p_color;
  730. if (!selected) {
  731. color.a *= 0.3;
  732. }
  733. material->set_albedo(color);
  734. material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  735. material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  736. material->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MIN + 1);
  737. material->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
  738. if (p_use_vertex_color) {
  739. material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  740. material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  741. }
  742. if (p_billboard) {
  743. material->set_billboard_mode(StandardMaterial3D::BILLBOARD_ENABLED);
  744. }
  745. if (p_on_top && selected) {
  746. material->set_on_top_of_alpha();
  747. }
  748. mats.push_back(material);
  749. }
  750. materials[p_name] = mats;
  751. }
  752. void EditorNode3DGizmoPlugin::create_icon_material(const String &p_name, const Ref<Texture2D> &p_texture, bool p_on_top, const Color &p_albedo) {
  753. Color instantiated_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/instantiated");
  754. Vector<Ref<StandardMaterial3D>> icons;
  755. for (int i = 0; i < 4; i++) {
  756. bool selected = i % 2 == 1;
  757. bool instantiated = i < 2;
  758. Ref<StandardMaterial3D> icon = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  759. Color color = instantiated ? instantiated_color : p_albedo;
  760. if (!selected) {
  761. color.a *= 0.85;
  762. }
  763. icon->set_albedo(color);
  764. icon->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  765. icon->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  766. icon->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  767. icon->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
  768. icon->set_depth_draw_mode(StandardMaterial3D::DEPTH_DRAW_DISABLED);
  769. icon->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  770. icon->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, p_texture);
  771. icon->set_flag(StandardMaterial3D::FLAG_FIXED_SIZE, true);
  772. icon->set_billboard_mode(StandardMaterial3D::BILLBOARD_ENABLED);
  773. icon->set_render_priority(StandardMaterial3D::RENDER_PRIORITY_MIN);
  774. if (p_on_top && selected) {
  775. icon->set_on_top_of_alpha();
  776. }
  777. icons.push_back(icon);
  778. }
  779. materials[p_name] = icons;
  780. }
  781. void EditorNode3DGizmoPlugin::create_handle_material(const String &p_name, bool p_billboard, const Ref<Texture2D> &p_icon) {
  782. Ref<StandardMaterial3D> handle_material = Ref<StandardMaterial3D>(memnew(StandardMaterial3D));
  783. handle_material->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  784. handle_material->set_flag(StandardMaterial3D::FLAG_USE_POINT_SIZE, true);
  785. Ref<Texture2D> handle_t = p_icon != nullptr ? p_icon : Node3DEditor::get_singleton()->get_theme_icon(SNAME("Editor3DHandle"), SNAME("EditorIcons"));
  786. handle_material->set_point_size(handle_t->get_width());
  787. handle_material->set_texture(StandardMaterial3D::TEXTURE_ALBEDO, handle_t);
  788. handle_material->set_albedo(Color(1, 1, 1));
  789. handle_material->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  790. handle_material->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  791. handle_material->set_on_top_of_alpha();
  792. if (p_billboard) {
  793. handle_material->set_billboard_mode(StandardMaterial3D::BILLBOARD_ENABLED);
  794. handle_material->set_on_top_of_alpha();
  795. }
  796. handle_material->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  797. materials[p_name] = Vector<Ref<StandardMaterial3D>>();
  798. materials[p_name].push_back(handle_material);
  799. }
  800. void EditorNode3DGizmoPlugin::add_material(const String &p_name, Ref<StandardMaterial3D> p_material) {
  801. materials[p_name] = Vector<Ref<StandardMaterial3D>>();
  802. materials[p_name].push_back(p_material);
  803. }
  804. Ref<StandardMaterial3D> EditorNode3DGizmoPlugin::get_material(const String &p_name, const Ref<EditorNode3DGizmo> &p_gizmo) {
  805. ERR_FAIL_COND_V(!materials.has(p_name), Ref<StandardMaterial3D>());
  806. ERR_FAIL_COND_V(materials[p_name].size() == 0, Ref<StandardMaterial3D>());
  807. if (p_gizmo.is_null() || materials[p_name].size() == 1) {
  808. return materials[p_name][0];
  809. }
  810. int index = (p_gizmo->is_selected() ? 1 : 0) + (p_gizmo->is_editable() ? 2 : 0);
  811. Ref<StandardMaterial3D> mat = materials[p_name][index];
  812. if (current_state == ON_TOP && p_gizmo->is_selected()) {
  813. mat->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, true);
  814. } else {
  815. mat->set_flag(StandardMaterial3D::FLAG_DISABLE_DEPTH_TEST, false);
  816. }
  817. return mat;
  818. }
  819. String EditorNode3DGizmoPlugin::get_gizmo_name() const {
  820. if (get_script_instance() && get_script_instance()->has_method("_get_gizmo_name")) {
  821. return get_script_instance()->call("_get_gizmo_name");
  822. }
  823. WARN_PRINT_ONCE("A 3D editor gizmo has no name defined (it will appear as \"Unnamed Gizmo\" in the \"View > Gizmos\" menu). To resolve this, override the `_get_gizmo_name()` function to return a String in the script that extends EditorNode3DGizmoPlugin.");
  824. return TTR("Unnamed Gizmo");
  825. }
  826. int EditorNode3DGizmoPlugin::get_priority() const {
  827. if (get_script_instance() && get_script_instance()->has_method("_get_priority")) {
  828. return get_script_instance()->call("_get_priority");
  829. }
  830. return 0;
  831. }
  832. Ref<EditorNode3DGizmo> EditorNode3DGizmoPlugin::get_gizmo(Node3D *p_spatial) {
  833. if (get_script_instance() && get_script_instance()->has_method("_get_gizmo")) {
  834. return get_script_instance()->call("_get_gizmo", p_spatial);
  835. }
  836. Ref<EditorNode3DGizmo> ref = create_gizmo(p_spatial);
  837. if (ref.is_null()) {
  838. return ref;
  839. }
  840. ref->set_plugin(this);
  841. ref->set_spatial_node(p_spatial);
  842. ref->set_hidden(current_state == HIDDEN);
  843. current_gizmos.push_back(ref.ptr());
  844. return ref;
  845. }
  846. void EditorNode3DGizmoPlugin::_bind_methods() {
  847. ClassDB::bind_method(D_METHOD("create_material", "name", "color", "billboard", "on_top", "use_vertex_color"), &EditorNode3DGizmoPlugin::create_material, DEFVAL(false), DEFVAL(false), DEFVAL(false));
  848. ClassDB::bind_method(D_METHOD("create_icon_material", "name", "texture", "on_top", "color"), &EditorNode3DGizmoPlugin::create_icon_material, DEFVAL(false), DEFVAL(Color(1, 1, 1, 1)));
  849. ClassDB::bind_method(D_METHOD("create_handle_material", "name", "billboard", "texture"), &EditorNode3DGizmoPlugin::create_handle_material, DEFVAL(false), DEFVAL(Variant()));
  850. ClassDB::bind_method(D_METHOD("add_material", "name", "material"), &EditorNode3DGizmoPlugin::add_material);
  851. ClassDB::bind_method(D_METHOD("get_material", "name", "gizmo"), &EditorNode3DGizmoPlugin::get_material, DEFVAL(Ref<EditorNode3DGizmo>()));
  852. GDVIRTUAL_BIND(_has_gizmo, "for_node_3d");
  853. GDVIRTUAL_BIND(_create_gizmo, "for_node_3d");
  854. GDVIRTUAL_BIND(_get_gizmo_name);
  855. GDVIRTUAL_BIND(_get_priority);
  856. GDVIRTUAL_BIND(_can_be_hidden);
  857. GDVIRTUAL_BIND(_is_selectable_when_hidden);
  858. GDVIRTUAL_BIND(_redraw, "gizmo");
  859. GDVIRTUAL_BIND(_get_handle_name, "gizmo", "handle_id", "secondary");
  860. GDVIRTUAL_BIND(_is_handle_highlighted, "gizmo", "handle_id", "secondary");
  861. GDVIRTUAL_BIND(_get_handle_value, "gizmo", "handle_id", "secondary");
  862. GDVIRTUAL_BIND(_set_handle, "gizmo", "handle_id", "secondary", "camera", "screen_pos");
  863. GDVIRTUAL_BIND(_commit_handle, "gizmo", "handle_id", "secondary", "restore", "cancel");
  864. GDVIRTUAL_BIND(_subgizmos_intersect_ray, "gizmo", "camera", "screen_pos");
  865. GDVIRTUAL_BIND(_subgizmos_intersect_frustum, "gizmo", "camera", "frustum_planes");
  866. GDVIRTUAL_BIND(_get_subgizmo_transform, "gizmo", "subgizmo_id");
  867. GDVIRTUAL_BIND(_set_subgizmo_transform, "gizmo", "subgizmo_id", "transform");
  868. GDVIRTUAL_BIND(_commit_subgizmos, "gizmo", "ids", "restores", "cancel");
  869. }
  870. bool EditorNode3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  871. bool success;
  872. if (GDVIRTUAL_CALL(_has_gizmo, p_spatial, success)) {
  873. return success;
  874. }
  875. return false;
  876. }
  877. Ref<EditorNode3DGizmo> EditorNode3DGizmoPlugin::create_gizmo(Node3D *p_spatial) {
  878. Ref<EditorNode3DGizmo> ret;
  879. if (GDVIRTUAL_CALL(_create_gizmo, p_spatial, ret)) {
  880. return ret;
  881. }
  882. Ref<EditorNode3DGizmo> ref;
  883. if (has_gizmo(p_spatial)) {
  884. ref.instantiate();
  885. }
  886. return ref;
  887. }
  888. bool EditorNode3DGizmoPlugin::can_be_hidden() const {
  889. bool ret;
  890. if (GDVIRTUAL_CALL(_can_be_hidden, ret)) {
  891. return ret;
  892. }
  893. return true;
  894. }
  895. bool EditorNode3DGizmoPlugin::is_selectable_when_hidden() const {
  896. bool ret;
  897. if (GDVIRTUAL_CALL(_is_selectable_when_hidden, ret)) {
  898. return ret;
  899. }
  900. return false;
  901. }
  902. void EditorNode3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  903. GDVIRTUAL_CALL(_redraw, p_gizmo);
  904. }
  905. bool EditorNode3DGizmoPlugin::is_handle_highlighted(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  906. bool ret;
  907. if (GDVIRTUAL_CALL(_is_handle_highlighted, Ref<EditorNode3DGizmo>(p_gizmo), p_id, p_secondary, ret)) {
  908. return ret;
  909. }
  910. return false;
  911. }
  912. String EditorNode3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  913. String ret;
  914. if (GDVIRTUAL_CALL(_get_handle_name, Ref<EditorNode3DGizmo>(p_gizmo), p_id, p_secondary, ret)) {
  915. return ret;
  916. }
  917. return "";
  918. }
  919. Variant EditorNode3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  920. Variant ret;
  921. if (GDVIRTUAL_CALL(_get_handle_value, Ref<EditorNode3DGizmo>(p_gizmo), p_id, p_secondary, ret)) {
  922. return ret;
  923. }
  924. return Variant();
  925. }
  926. void EditorNode3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  927. GDVIRTUAL_CALL(_set_handle, Ref<EditorNode3DGizmo>(p_gizmo), p_id, p_secondary, p_camera, p_point);
  928. }
  929. void EditorNode3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  930. GDVIRTUAL_CALL(_commit_handle, Ref<EditorNode3DGizmo>(p_gizmo), p_id, p_secondary, p_restore, p_cancel);
  931. }
  932. int EditorNode3DGizmoPlugin::subgizmos_intersect_ray(const EditorNode3DGizmo *p_gizmo, Camera3D *p_camera, const Vector2 &p_point) const {
  933. int ret;
  934. if (GDVIRTUAL_CALL(_subgizmos_intersect_ray, Ref<EditorNode3DGizmo>(p_gizmo), p_camera, p_point, ret)) {
  935. return ret;
  936. }
  937. return -1;
  938. }
  939. Vector<int> EditorNode3DGizmoPlugin::subgizmos_intersect_frustum(const EditorNode3DGizmo *p_gizmo, const Camera3D *p_camera, const Vector<Plane> &p_frustum) const {
  940. TypedArray<Transform3D> frustum;
  941. frustum.resize(p_frustum.size());
  942. for (int i = 0; i < p_frustum.size(); i++) {
  943. frustum[i] = p_frustum[i];
  944. }
  945. Vector<int> ret;
  946. if (GDVIRTUAL_CALL(_subgizmos_intersect_frustum, Ref<EditorNode3DGizmo>(p_gizmo), p_camera, frustum, ret)) {
  947. return ret;
  948. }
  949. return Vector<int>();
  950. }
  951. Transform3D EditorNode3DGizmoPlugin::get_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id) const {
  952. Transform3D ret;
  953. if (GDVIRTUAL_CALL(_get_subgizmo_transform, Ref<EditorNode3DGizmo>(p_gizmo), p_id, ret)) {
  954. return ret;
  955. }
  956. return Transform3D();
  957. }
  958. void EditorNode3DGizmoPlugin::set_subgizmo_transform(const EditorNode3DGizmo *p_gizmo, int p_id, Transform3D p_transform) {
  959. GDVIRTUAL_CALL(_set_subgizmo_transform, Ref<EditorNode3DGizmo>(p_gizmo), p_id, p_transform);
  960. }
  961. void EditorNode3DGizmoPlugin::commit_subgizmos(const EditorNode3DGizmo *p_gizmo, const Vector<int> &p_ids, const Vector<Transform3D> &p_restore, bool p_cancel) {
  962. TypedArray<Transform3D> restore;
  963. restore.resize(p_restore.size());
  964. for (int i = 0; i < p_restore.size(); i++) {
  965. restore[i] = p_restore[i];
  966. }
  967. GDVIRTUAL_CALL(_commit_subgizmos, Ref<EditorNode3DGizmo>(p_gizmo), p_ids, restore, p_cancel);
  968. }
  969. void EditorNode3DGizmoPlugin::set_state(int p_state) {
  970. current_state = p_state;
  971. for (int i = 0; i < current_gizmos.size(); ++i) {
  972. current_gizmos[i]->set_hidden(current_state == HIDDEN);
  973. }
  974. }
  975. int EditorNode3DGizmoPlugin::get_state() const {
  976. return current_state;
  977. }
  978. void EditorNode3DGizmoPlugin::unregister_gizmo(EditorNode3DGizmo *p_gizmo) {
  979. current_gizmos.erase(p_gizmo);
  980. }
  981. EditorNode3DGizmoPlugin::EditorNode3DGizmoPlugin() {
  982. current_state = VISIBLE;
  983. }
  984. EditorNode3DGizmoPlugin::~EditorNode3DGizmoPlugin() {
  985. for (int i = 0; i < current_gizmos.size(); ++i) {
  986. current_gizmos[i]->set_plugin(nullptr);
  987. current_gizmos[i]->get_spatial_node()->remove_gizmo(current_gizmos[i]);
  988. }
  989. if (Node3DEditor::get_singleton()) {
  990. Node3DEditor::get_singleton()->update_all_gizmos();
  991. }
  992. }
  993. //// light gizmo
  994. Light3DGizmoPlugin::Light3DGizmoPlugin() {
  995. // Enable vertex colors for the materials below as the gizmo color depends on the light color.
  996. create_material("lines_primary", Color(1, 1, 1), false, false, true);
  997. create_material("lines_secondary", Color(1, 1, 1, 0.35), false, false, true);
  998. create_material("lines_billboard", Color(1, 1, 1), true, false, true);
  999. create_icon_material("light_directional_icon", Node3DEditor::get_singleton()->get_theme_icon(SNAME("GizmoDirectionalLight"), SNAME("EditorIcons")));
  1000. create_icon_material("light_omni_icon", Node3DEditor::get_singleton()->get_theme_icon(SNAME("GizmoLight"), SNAME("EditorIcons")));
  1001. create_icon_material("light_spot_icon", Node3DEditor::get_singleton()->get_theme_icon(SNAME("GizmoSpotLight"), SNAME("EditorIcons")));
  1002. create_handle_material("handles");
  1003. create_handle_material("handles_billboard", true);
  1004. }
  1005. bool Light3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1006. return Object::cast_to<Light3D>(p_spatial) != nullptr;
  1007. }
  1008. String Light3DGizmoPlugin::get_gizmo_name() const {
  1009. return "Light3D";
  1010. }
  1011. int Light3DGizmoPlugin::get_priority() const {
  1012. return -1;
  1013. }
  1014. String Light3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  1015. if (p_id == 0) {
  1016. return "Radius";
  1017. } else {
  1018. return "Aperture";
  1019. }
  1020. }
  1021. Variant Light3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  1022. Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_spatial_node());
  1023. if (p_id == 0) {
  1024. return light->get_param(Light3D::PARAM_RANGE);
  1025. }
  1026. if (p_id == 1) {
  1027. return light->get_param(Light3D::PARAM_SPOT_ANGLE);
  1028. }
  1029. return Variant();
  1030. }
  1031. static float _find_closest_angle_to_half_pi_arc(const Vector3 &p_from, const Vector3 &p_to, float p_arc_radius, const Transform3D &p_arc_xform) {
  1032. //bleh, discrete is simpler
  1033. static const int arc_test_points = 64;
  1034. float min_d = 1e20;
  1035. Vector3 min_p;
  1036. for (int i = 0; i < arc_test_points; i++) {
  1037. float a = i * Math_PI * 0.5 / arc_test_points;
  1038. float an = (i + 1) * Math_PI * 0.5 / arc_test_points;
  1039. Vector3 p = Vector3(Math::cos(a), 0, -Math::sin(a)) * p_arc_radius;
  1040. Vector3 n = Vector3(Math::cos(an), 0, -Math::sin(an)) * p_arc_radius;
  1041. Vector3 ra, rb;
  1042. Geometry3D::get_closest_points_between_segments(p, n, p_from, p_to, ra, rb);
  1043. float d = ra.distance_to(rb);
  1044. if (d < min_d) {
  1045. min_d = d;
  1046. min_p = ra;
  1047. }
  1048. }
  1049. //min_p = p_arc_xform.affine_inverse().xform(min_p);
  1050. float a = (Math_PI * 0.5) - Vector2(min_p.x, -min_p.z).angle();
  1051. return Math::rad_to_deg(a);
  1052. }
  1053. void Light3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  1054. Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_spatial_node());
  1055. Transform3D gt = light->get_global_transform();
  1056. Transform3D gi = gt.affine_inverse();
  1057. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1058. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1059. Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  1060. if (p_id == 0) {
  1061. if (Object::cast_to<SpotLight3D>(light)) {
  1062. Vector3 ra, rb;
  1063. Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(0, 0, -4096), s[0], s[1], ra, rb);
  1064. float d = -ra.z;
  1065. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  1066. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  1067. }
  1068. if (d <= 0) { // Equal is here for negative zero.
  1069. d = 0;
  1070. }
  1071. light->set_param(Light3D::PARAM_RANGE, d);
  1072. } else if (Object::cast_to<OmniLight3D>(light)) {
  1073. Plane cp = Plane(p_camera->get_transform().basis.get_column(2), gt.origin);
  1074. Vector3 inters;
  1075. if (cp.intersects_ray(ray_from, ray_dir, &inters)) {
  1076. float r = inters.distance_to(gt.origin);
  1077. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  1078. r = Math::snapped(r, Node3DEditor::get_singleton()->get_translate_snap());
  1079. }
  1080. light->set_param(Light3D::PARAM_RANGE, r);
  1081. }
  1082. }
  1083. } else if (p_id == 1) {
  1084. float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], light->get_param(Light3D::PARAM_RANGE), gt);
  1085. light->set_param(Light3D::PARAM_SPOT_ANGLE, CLAMP(a, 0.01, 89.99));
  1086. }
  1087. }
  1088. void Light3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  1089. Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_spatial_node());
  1090. if (p_cancel) {
  1091. light->set_param(p_id == 0 ? Light3D::PARAM_RANGE : Light3D::PARAM_SPOT_ANGLE, p_restore);
  1092. } else if (p_id == 0) {
  1093. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  1094. ur->create_action(TTR("Change Light Radius"));
  1095. ur->add_do_method(light, "set_param", Light3D::PARAM_RANGE, light->get_param(Light3D::PARAM_RANGE));
  1096. ur->add_undo_method(light, "set_param", Light3D::PARAM_RANGE, p_restore);
  1097. ur->commit_action();
  1098. } else if (p_id == 1) {
  1099. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  1100. ur->create_action(TTR("Change Light Radius"));
  1101. ur->add_do_method(light, "set_param", Light3D::PARAM_SPOT_ANGLE, light->get_param(Light3D::PARAM_SPOT_ANGLE));
  1102. ur->add_undo_method(light, "set_param", Light3D::PARAM_SPOT_ANGLE, p_restore);
  1103. ur->commit_action();
  1104. }
  1105. }
  1106. void Light3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1107. Light3D *light = Object::cast_to<Light3D>(p_gizmo->get_spatial_node());
  1108. Color color = light->get_color().srgb_to_linear() * light->get_correlated_color().srgb_to_linear();
  1109. color = color.linear_to_srgb();
  1110. // Make the gizmo color as bright as possible for better visibility
  1111. color.set_hsv(color.get_h(), color.get_s(), 1);
  1112. p_gizmo->clear();
  1113. if (Object::cast_to<DirectionalLight3D>(light)) {
  1114. Ref<Material> material = get_material("lines_primary", p_gizmo);
  1115. Ref<Material> icon = get_material("light_directional_icon", p_gizmo);
  1116. const int arrow_points = 7;
  1117. const float arrow_length = 1.5;
  1118. Vector3 arrow[arrow_points] = {
  1119. Vector3(0, 0, -1),
  1120. Vector3(0, 0.8, 0),
  1121. Vector3(0, 0.3, 0),
  1122. Vector3(0, 0.3, arrow_length),
  1123. Vector3(0, -0.3, arrow_length),
  1124. Vector3(0, -0.3, 0),
  1125. Vector3(0, -0.8, 0)
  1126. };
  1127. int arrow_sides = 2;
  1128. Vector<Vector3> lines;
  1129. for (int i = 0; i < arrow_sides; i++) {
  1130. for (int j = 0; j < arrow_points; j++) {
  1131. Basis ma(Vector3(0, 0, 1), Math_PI * i / arrow_sides);
  1132. Vector3 v1 = arrow[j] - Vector3(0, 0, arrow_length);
  1133. Vector3 v2 = arrow[(j + 1) % arrow_points] - Vector3(0, 0, arrow_length);
  1134. lines.push_back(ma.xform(v1));
  1135. lines.push_back(ma.xform(v2));
  1136. }
  1137. }
  1138. p_gizmo->add_lines(lines, material, false, color);
  1139. p_gizmo->add_unscaled_billboard(icon, 0.05, color);
  1140. }
  1141. if (Object::cast_to<OmniLight3D>(light)) {
  1142. // Use both a billboard circle and 3 non-billboard circles for a better sphere-like representation
  1143. const Ref<Material> lines_material = get_material("lines_secondary", p_gizmo);
  1144. const Ref<Material> lines_billboard_material = get_material("lines_billboard", p_gizmo);
  1145. const Ref<Material> icon = get_material("light_omni_icon", p_gizmo);
  1146. OmniLight3D *on = Object::cast_to<OmniLight3D>(light);
  1147. const float r = on->get_param(Light3D::PARAM_RANGE);
  1148. Vector<Vector3> points;
  1149. Vector<Vector3> points_billboard;
  1150. for (int i = 0; i < 120; i++) {
  1151. // Create a circle
  1152. const float ra = Math::deg_to_rad((float)(i * 3));
  1153. const float rb = Math::deg_to_rad((float)((i + 1) * 3));
  1154. const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  1155. const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  1156. // Draw axis-aligned circles
  1157. points.push_back(Vector3(a.x, 0, a.y));
  1158. points.push_back(Vector3(b.x, 0, b.y));
  1159. points.push_back(Vector3(0, a.x, a.y));
  1160. points.push_back(Vector3(0, b.x, b.y));
  1161. points.push_back(Vector3(a.x, a.y, 0));
  1162. points.push_back(Vector3(b.x, b.y, 0));
  1163. // Draw a billboarded circle
  1164. points_billboard.push_back(Vector3(a.x, a.y, 0));
  1165. points_billboard.push_back(Vector3(b.x, b.y, 0));
  1166. }
  1167. p_gizmo->add_lines(points, lines_material, true, color);
  1168. p_gizmo->add_lines(points_billboard, lines_billboard_material, true, color);
  1169. p_gizmo->add_unscaled_billboard(icon, 0.05, color);
  1170. Vector<Vector3> handles;
  1171. handles.push_back(Vector3(r, 0, 0));
  1172. p_gizmo->add_handles(handles, get_material("handles_billboard"), Vector<int>(), true);
  1173. }
  1174. if (Object::cast_to<SpotLight3D>(light)) {
  1175. const Ref<Material> material_primary = get_material("lines_primary", p_gizmo);
  1176. const Ref<Material> material_secondary = get_material("lines_secondary", p_gizmo);
  1177. const Ref<Material> icon = get_material("light_spot_icon", p_gizmo);
  1178. Vector<Vector3> points_primary;
  1179. Vector<Vector3> points_secondary;
  1180. SpotLight3D *sl = Object::cast_to<SpotLight3D>(light);
  1181. float r = sl->get_param(Light3D::PARAM_RANGE);
  1182. float w = r * Math::sin(Math::deg_to_rad(sl->get_param(Light3D::PARAM_SPOT_ANGLE)));
  1183. float d = r * Math::cos(Math::deg_to_rad(sl->get_param(Light3D::PARAM_SPOT_ANGLE)));
  1184. for (int i = 0; i < 120; i++) {
  1185. // Draw a circle
  1186. const float ra = Math::deg_to_rad((float)(i * 3));
  1187. const float rb = Math::deg_to_rad((float)((i + 1) * 3));
  1188. const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;
  1189. const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;
  1190. points_primary.push_back(Vector3(a.x, a.y, -d));
  1191. points_primary.push_back(Vector3(b.x, b.y, -d));
  1192. if (i % 15 == 0) {
  1193. // Draw 8 lines from the cone origin to the sides of the circle
  1194. points_secondary.push_back(Vector3(a.x, a.y, -d));
  1195. points_secondary.push_back(Vector3());
  1196. }
  1197. }
  1198. points_primary.push_back(Vector3(0, 0, -r));
  1199. points_primary.push_back(Vector3());
  1200. p_gizmo->add_lines(points_primary, material_primary, false, color);
  1201. p_gizmo->add_lines(points_secondary, material_secondary, false, color);
  1202. Vector<Vector3> handles = {
  1203. Vector3(0, 0, -r),
  1204. Vector3(w, 0, -d)
  1205. };
  1206. p_gizmo->add_handles(handles, get_material("handles"));
  1207. p_gizmo->add_unscaled_billboard(icon, 0.05, color);
  1208. }
  1209. }
  1210. //// player gizmo
  1211. AudioStreamPlayer3DGizmoPlugin::AudioStreamPlayer3DGizmoPlugin() {
  1212. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/stream_player_3d", Color(0.4, 0.8, 1));
  1213. create_icon_material("stream_player_3d_icon", Node3DEditor::get_singleton()->get_theme_icon(SNAME("Gizmo3DSamplePlayer"), SNAME("EditorIcons")));
  1214. create_material("stream_player_3d_material_primary", gizmo_color);
  1215. create_material("stream_player_3d_material_secondary", gizmo_color * Color(1, 1, 1, 0.35));
  1216. // Enable vertex colors for the billboard material as the gizmo color depends on the
  1217. // AudioStreamPlayer3D attenuation type and source (Unit Size or Max Distance).
  1218. create_material("stream_player_3d_material_billboard", Color(1, 1, 1), true, false, true);
  1219. create_handle_material("handles");
  1220. }
  1221. bool AudioStreamPlayer3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1222. return Object::cast_to<AudioStreamPlayer3D>(p_spatial) != nullptr;
  1223. }
  1224. String AudioStreamPlayer3DGizmoPlugin::get_gizmo_name() const {
  1225. return "AudioStreamPlayer3D";
  1226. }
  1227. int AudioStreamPlayer3DGizmoPlugin::get_priority() const {
  1228. return -1;
  1229. }
  1230. String AudioStreamPlayer3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  1231. return "Emission Radius";
  1232. }
  1233. Variant AudioStreamPlayer3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  1234. AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_spatial_node());
  1235. return player->get_emission_angle();
  1236. }
  1237. void AudioStreamPlayer3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  1238. AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_spatial_node());
  1239. Transform3D gt = player->get_global_transform();
  1240. Transform3D gi = gt.affine_inverse();
  1241. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1242. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1243. Vector3 ray_to = ray_from + ray_dir * 4096;
  1244. ray_from = gi.xform(ray_from);
  1245. ray_to = gi.xform(ray_to);
  1246. float closest_dist = 1e20;
  1247. float closest_angle = 1e20;
  1248. for (int i = 0; i < 180; i++) {
  1249. float a = Math::deg_to_rad((float)i);
  1250. float an = Math::deg_to_rad((float)(i + 1));
  1251. Vector3 from(Math::sin(a), 0, -Math::cos(a));
  1252. Vector3 to(Math::sin(an), 0, -Math::cos(an));
  1253. Vector3 r1, r2;
  1254. Geometry3D::get_closest_points_between_segments(from, to, ray_from, ray_to, r1, r2);
  1255. float d = r1.distance_to(r2);
  1256. if (d < closest_dist) {
  1257. closest_dist = d;
  1258. closest_angle = i;
  1259. }
  1260. }
  1261. if (closest_angle < 91) {
  1262. player->set_emission_angle(closest_angle);
  1263. }
  1264. }
  1265. void AudioStreamPlayer3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  1266. AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_spatial_node());
  1267. if (p_cancel) {
  1268. player->set_emission_angle(p_restore);
  1269. } else {
  1270. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  1271. ur->create_action(TTR("Change AudioStreamPlayer3D Emission Angle"));
  1272. ur->add_do_method(player, "set_emission_angle", player->get_emission_angle());
  1273. ur->add_undo_method(player, "set_emission_angle", p_restore);
  1274. ur->commit_action();
  1275. }
  1276. }
  1277. void AudioStreamPlayer3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1278. const AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_spatial_node());
  1279. p_gizmo->clear();
  1280. const Ref<Material> icon = get_material("stream_player_3d_icon", p_gizmo);
  1281. if (player->get_attenuation_model() != AudioStreamPlayer3D::ATTENUATION_DISABLED || player->get_max_distance() > CMP_EPSILON) {
  1282. // Draw a circle to represent sound volume attenuation.
  1283. // Use only a billboard circle to represent radius.
  1284. // This helps distinguish AudioStreamPlayer3D gizmos from OmniLight3D gizmos.
  1285. const Ref<Material> lines_billboard_material = get_material("stream_player_3d_material_billboard", p_gizmo);
  1286. // Soft distance cap varies depending on attenuation model, as some will fade out more aggressively than others.
  1287. // Multipliers were empirically determined through testing.
  1288. float soft_multiplier;
  1289. switch (player->get_attenuation_model()) {
  1290. case AudioStreamPlayer3D::ATTENUATION_INVERSE_DISTANCE:
  1291. soft_multiplier = 12.0;
  1292. break;
  1293. case AudioStreamPlayer3D::ATTENUATION_INVERSE_SQUARE_DISTANCE:
  1294. soft_multiplier = 4.0;
  1295. break;
  1296. case AudioStreamPlayer3D::ATTENUATION_LOGARITHMIC:
  1297. soft_multiplier = 3.25;
  1298. break;
  1299. default:
  1300. // Ensures Max Distance's radius visualization is not capped by Unit Size
  1301. // (when the attenuation mode is Disabled).
  1302. soft_multiplier = 10000.0;
  1303. break;
  1304. }
  1305. // Draw the distance at which the sound can be reasonably heard.
  1306. // This can be either a hard distance cap with the Max Distance property (if set above 0.0),
  1307. // or a soft distance cap with the Unit Size property (sound never reaches true zero).
  1308. // When Max Distance is 0.0, `r` represents the distance above which the
  1309. // sound can't be heard in *most* (but not all) scenarios.
  1310. float r;
  1311. if (player->get_max_distance() > CMP_EPSILON) {
  1312. r = MIN(player->get_unit_size() * soft_multiplier, player->get_max_distance());
  1313. } else {
  1314. r = player->get_unit_size() * soft_multiplier;
  1315. }
  1316. Vector<Vector3> points_billboard;
  1317. for (int i = 0; i < 120; i++) {
  1318. // Create a circle.
  1319. const float ra = Math::deg_to_rad((float)(i * 3));
  1320. const float rb = Math::deg_to_rad((float)((i + 1) * 3));
  1321. const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  1322. const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  1323. // Draw a billboarded circle.
  1324. points_billboard.push_back(Vector3(a.x, a.y, 0));
  1325. points_billboard.push_back(Vector3(b.x, b.y, 0));
  1326. }
  1327. Color color;
  1328. switch (player->get_attenuation_model()) {
  1329. // Pick cold colors for all attenuation models (except Disabled),
  1330. // so that soft caps can be easily distinguished from hard caps
  1331. // (which use warm colors).
  1332. case AudioStreamPlayer3D::ATTENUATION_INVERSE_DISTANCE:
  1333. color = Color(0.4, 0.8, 1);
  1334. break;
  1335. case AudioStreamPlayer3D::ATTENUATION_INVERSE_SQUARE_DISTANCE:
  1336. color = Color(0.4, 0.5, 1);
  1337. break;
  1338. case AudioStreamPlayer3D::ATTENUATION_LOGARITHMIC:
  1339. color = Color(0.4, 0.2, 1);
  1340. break;
  1341. default:
  1342. // Disabled attenuation mode.
  1343. // This is never reached when Max Distance is 0, but the
  1344. // hue-inverted form of this color will be used if Max Distance is greater than 0.
  1345. color = Color(1, 1, 1);
  1346. break;
  1347. }
  1348. if (player->get_max_distance() > CMP_EPSILON) {
  1349. // Sound is hard-capped by max distance. The attenuation model still matters,
  1350. // so invert the hue of the color that was chosen above.
  1351. color.set_h(color.get_h() + 0.5);
  1352. }
  1353. p_gizmo->add_lines(points_billboard, lines_billboard_material, true, color);
  1354. }
  1355. if (player->is_emission_angle_enabled()) {
  1356. const float pc = player->get_emission_angle();
  1357. const float ofs = -Math::cos(Math::deg_to_rad(pc));
  1358. const float radius = Math::sin(Math::deg_to_rad(pc));
  1359. Vector<Vector3> points_primary;
  1360. points_primary.resize(200);
  1361. real_t step = Math_TAU / 100.0;
  1362. for (int i = 0; i < 100; i++) {
  1363. const float a = i * step;
  1364. const float an = (i + 1) * step;
  1365. const Vector3 from(Math::sin(a) * radius, Math::cos(a) * radius, ofs);
  1366. const Vector3 to(Math::sin(an) * radius, Math::cos(an) * radius, ofs);
  1367. points_primary.write[i * 2 + 0] = from;
  1368. points_primary.write[i * 2 + 1] = to;
  1369. }
  1370. const Ref<Material> material_primary = get_material("stream_player_3d_material_primary", p_gizmo);
  1371. p_gizmo->add_lines(points_primary, material_primary);
  1372. Vector<Vector3> points_secondary;
  1373. points_secondary.resize(16);
  1374. for (int i = 0; i < 8; i++) {
  1375. const float a = i * (Math_TAU / 8.0);
  1376. const Vector3 from(Math::sin(a) * radius, Math::cos(a) * radius, ofs);
  1377. points_secondary.write[i * 2 + 0] = from;
  1378. points_secondary.write[i * 2 + 1] = Vector3();
  1379. }
  1380. const Ref<Material> material_secondary = get_material("stream_player_3d_material_secondary", p_gizmo);
  1381. p_gizmo->add_lines(points_secondary, material_secondary);
  1382. Vector<Vector3> handles;
  1383. const float ha = Math::deg_to_rad(player->get_emission_angle());
  1384. handles.push_back(Vector3(Math::sin(ha), 0, -Math::cos(ha)));
  1385. p_gizmo->add_handles(handles, get_material("handles"));
  1386. }
  1387. p_gizmo->add_unscaled_billboard(icon, 0.05);
  1388. }
  1389. //////
  1390. AudioListener3DGizmoPlugin::AudioListener3DGizmoPlugin() {
  1391. create_icon_material("audio_listener_3d_icon", Node3DEditor::get_singleton()->get_theme_icon(SNAME("GizmoAudioListener3D"), SNAME("EditorIcons")));
  1392. }
  1393. bool AudioListener3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1394. return Object::cast_to<AudioListener3D>(p_spatial) != nullptr;
  1395. }
  1396. String AudioListener3DGizmoPlugin::get_gizmo_name() const {
  1397. return "AudioListener3D";
  1398. }
  1399. int AudioListener3DGizmoPlugin::get_priority() const {
  1400. return -1;
  1401. }
  1402. void AudioListener3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1403. const Ref<Material> icon = get_material("audio_listener_3d_icon", p_gizmo);
  1404. p_gizmo->add_unscaled_billboard(icon, 0.05);
  1405. }
  1406. //////
  1407. Camera3DGizmoPlugin::Camera3DGizmoPlugin() {
  1408. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/camera", Color(0.8, 0.4, 0.8));
  1409. create_material("camera_material", gizmo_color);
  1410. create_handle_material("handles");
  1411. }
  1412. bool Camera3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1413. return Object::cast_to<Camera3D>(p_spatial) != nullptr;
  1414. }
  1415. String Camera3DGizmoPlugin::get_gizmo_name() const {
  1416. return "Camera3D";
  1417. }
  1418. int Camera3DGizmoPlugin::get_priority() const {
  1419. return -1;
  1420. }
  1421. String Camera3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  1422. Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_spatial_node());
  1423. if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {
  1424. return "FOV";
  1425. } else {
  1426. return "Size";
  1427. }
  1428. }
  1429. Variant Camera3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  1430. Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_spatial_node());
  1431. if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {
  1432. return camera->get_fov();
  1433. } else {
  1434. return camera->get_size();
  1435. }
  1436. }
  1437. void Camera3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  1438. Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_spatial_node());
  1439. Transform3D gt = camera->get_global_transform();
  1440. Transform3D gi = gt.affine_inverse();
  1441. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1442. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1443. Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  1444. if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {
  1445. Transform3D gt2 = camera->get_global_transform();
  1446. float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], 1.0, gt2);
  1447. camera->set("fov", CLAMP(a * 2.0, 1, 179));
  1448. } else {
  1449. Vector3 ra, rb;
  1450. Geometry3D::get_closest_points_between_segments(Vector3(0, 0, -1), Vector3(4096, 0, -1), s[0], s[1], ra, rb);
  1451. float d = ra.x * 2.0;
  1452. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  1453. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  1454. }
  1455. d = CLAMP(d, 0.1, 16384);
  1456. camera->set("size", d);
  1457. }
  1458. }
  1459. void Camera3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  1460. Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_spatial_node());
  1461. if (camera->get_projection() == Camera3D::PROJECTION_PERSPECTIVE) {
  1462. if (p_cancel) {
  1463. camera->set("fov", p_restore);
  1464. } else {
  1465. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  1466. ur->create_action(TTR("Change Camera FOV"));
  1467. ur->add_do_property(camera, "fov", camera->get_fov());
  1468. ur->add_undo_property(camera, "fov", p_restore);
  1469. ur->commit_action();
  1470. }
  1471. } else {
  1472. if (p_cancel) {
  1473. camera->set("size", p_restore);
  1474. } else {
  1475. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  1476. ur->create_action(TTR("Change Camera Size"));
  1477. ur->add_do_property(camera, "size", camera->get_size());
  1478. ur->add_undo_property(camera, "size", p_restore);
  1479. ur->commit_action();
  1480. }
  1481. }
  1482. }
  1483. void Camera3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1484. Camera3D *camera = Object::cast_to<Camera3D>(p_gizmo->get_spatial_node());
  1485. p_gizmo->clear();
  1486. Vector<Vector3> lines;
  1487. Vector<Vector3> handles;
  1488. Ref<Material> material = get_material("camera_material", p_gizmo);
  1489. #define ADD_TRIANGLE(m_a, m_b, m_c) \
  1490. { \
  1491. lines.push_back(m_a); \
  1492. lines.push_back(m_b); \
  1493. lines.push_back(m_b); \
  1494. lines.push_back(m_c); \
  1495. lines.push_back(m_c); \
  1496. lines.push_back(m_a); \
  1497. }
  1498. #define ADD_QUAD(m_a, m_b, m_c, m_d) \
  1499. { \
  1500. lines.push_back(m_a); \
  1501. lines.push_back(m_b); \
  1502. lines.push_back(m_b); \
  1503. lines.push_back(m_c); \
  1504. lines.push_back(m_c); \
  1505. lines.push_back(m_d); \
  1506. lines.push_back(m_d); \
  1507. lines.push_back(m_a); \
  1508. }
  1509. switch (camera->get_projection()) {
  1510. case Camera3D::PROJECTION_PERSPECTIVE: {
  1511. // The real FOV is halved for accurate representation
  1512. float fov = camera->get_fov() / 2.0;
  1513. Vector3 side = Vector3(Math::sin(Math::deg_to_rad(fov)), 0, -Math::cos(Math::deg_to_rad(fov)));
  1514. Vector3 nside = side;
  1515. nside.x = -nside.x;
  1516. Vector3 up = Vector3(0, side.x, 0);
  1517. ADD_TRIANGLE(Vector3(), side + up, side - up);
  1518. ADD_TRIANGLE(Vector3(), nside + up, nside - up);
  1519. ADD_TRIANGLE(Vector3(), side + up, nside + up);
  1520. ADD_TRIANGLE(Vector3(), side - up, nside - up);
  1521. handles.push_back(side);
  1522. side.x *= 0.25;
  1523. nside.x *= 0.25;
  1524. Vector3 tup(0, up.y * 3 / 2, side.z);
  1525. ADD_TRIANGLE(tup, side + up, nside + up);
  1526. } break;
  1527. case Camera3D::PROJECTION_ORTHOGONAL: {
  1528. float size = camera->get_size();
  1529. float hsize = size * 0.5;
  1530. Vector3 right(hsize, 0, 0);
  1531. Vector3 up(0, hsize, 0);
  1532. Vector3 back(0, 0, -1.0);
  1533. Vector3 front(0, 0, 0);
  1534. ADD_QUAD(-up - right, -up + right, up + right, up - right);
  1535. ADD_QUAD(-up - right + back, -up + right + back, up + right + back, up - right + back);
  1536. ADD_QUAD(up + right, up + right + back, up - right + back, up - right);
  1537. ADD_QUAD(-up + right, -up + right + back, -up - right + back, -up - right);
  1538. handles.push_back(right + back);
  1539. right.x *= 0.25;
  1540. Vector3 tup(0, up.y * 3 / 2, back.z);
  1541. ADD_TRIANGLE(tup, right + up + back, -right + up + back);
  1542. } break;
  1543. case Camera3D::PROJECTION_FRUSTUM: {
  1544. float hsize = camera->get_size() / 2.0;
  1545. Vector3 side = Vector3(hsize, 0, -camera->get_near()).normalized();
  1546. Vector3 nside = side;
  1547. nside.x = -nside.x;
  1548. Vector3 up = Vector3(0, side.x, 0);
  1549. Vector3 offset = Vector3(camera->get_frustum_offset().x, camera->get_frustum_offset().y, 0.0);
  1550. ADD_TRIANGLE(Vector3(), side + up + offset, side - up + offset);
  1551. ADD_TRIANGLE(Vector3(), nside + up + offset, nside - up + offset);
  1552. ADD_TRIANGLE(Vector3(), side + up + offset, nside + up + offset);
  1553. ADD_TRIANGLE(Vector3(), side - up + offset, nside - up + offset);
  1554. side.x *= 0.25;
  1555. nside.x *= 0.25;
  1556. Vector3 tup(0, up.y * 3 / 2, side.z);
  1557. ADD_TRIANGLE(tup + offset, side + up + offset, nside + up + offset);
  1558. }
  1559. }
  1560. #undef ADD_TRIANGLE
  1561. #undef ADD_QUAD
  1562. p_gizmo->add_lines(lines, material);
  1563. p_gizmo->add_handles(handles, get_material("handles"));
  1564. }
  1565. //////
  1566. MeshInstance3DGizmoPlugin::MeshInstance3DGizmoPlugin() {
  1567. }
  1568. bool MeshInstance3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1569. return Object::cast_to<MeshInstance3D>(p_spatial) != nullptr && Object::cast_to<SoftBody3D>(p_spatial) == nullptr;
  1570. }
  1571. String MeshInstance3DGizmoPlugin::get_gizmo_name() const {
  1572. return "MeshInstance3D";
  1573. }
  1574. int MeshInstance3DGizmoPlugin::get_priority() const {
  1575. return -1;
  1576. }
  1577. bool MeshInstance3DGizmoPlugin::can_be_hidden() const {
  1578. return false;
  1579. }
  1580. void MeshInstance3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1581. MeshInstance3D *mesh = Object::cast_to<MeshInstance3D>(p_gizmo->get_spatial_node());
  1582. p_gizmo->clear();
  1583. Ref<Mesh> m = mesh->get_mesh();
  1584. if (!m.is_valid()) {
  1585. return; //none
  1586. }
  1587. Ref<TriangleMesh> tm = m->generate_triangle_mesh();
  1588. if (tm.is_valid()) {
  1589. p_gizmo->add_collision_triangles(tm);
  1590. }
  1591. }
  1592. /////
  1593. OccluderInstance3DGizmoPlugin::OccluderInstance3DGizmoPlugin() {
  1594. create_material("line_material", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/occluder", Color(0.8, 0.5, 1)));
  1595. create_handle_material("handles");
  1596. }
  1597. bool OccluderInstance3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1598. return Object::cast_to<OccluderInstance3D>(p_spatial) != nullptr;
  1599. }
  1600. String OccluderInstance3DGizmoPlugin::get_gizmo_name() const {
  1601. return "OccluderInstance3D";
  1602. }
  1603. int OccluderInstance3DGizmoPlugin::get_priority() const {
  1604. return -1;
  1605. }
  1606. String OccluderInstance3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  1607. const OccluderInstance3D *cs = Object::cast_to<OccluderInstance3D>(p_gizmo->get_spatial_node());
  1608. Ref<Occluder3D> o = cs->get_occluder();
  1609. if (o.is_null()) {
  1610. return "";
  1611. }
  1612. if (Object::cast_to<SphereOccluder3D>(*o)) {
  1613. return "Radius";
  1614. }
  1615. if (Object::cast_to<BoxOccluder3D>(*o) || Object::cast_to<QuadOccluder3D>(*o)) {
  1616. return "Size";
  1617. }
  1618. return "";
  1619. }
  1620. Variant OccluderInstance3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  1621. OccluderInstance3D *oi = Object::cast_to<OccluderInstance3D>(p_gizmo->get_spatial_node());
  1622. Ref<Occluder3D> o = oi->get_occluder();
  1623. if (o.is_null()) {
  1624. return Variant();
  1625. }
  1626. if (Object::cast_to<SphereOccluder3D>(*o)) {
  1627. Ref<SphereOccluder3D> so = o;
  1628. return so->get_radius();
  1629. }
  1630. if (Object::cast_to<BoxOccluder3D>(*o)) {
  1631. Ref<BoxOccluder3D> bo = o;
  1632. return bo->get_size();
  1633. }
  1634. if (Object::cast_to<QuadOccluder3D>(*o)) {
  1635. Ref<QuadOccluder3D> qo = o;
  1636. return qo->get_size();
  1637. }
  1638. return Variant();
  1639. }
  1640. void OccluderInstance3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  1641. OccluderInstance3D *oi = Object::cast_to<OccluderInstance3D>(p_gizmo->get_spatial_node());
  1642. Ref<Occluder3D> o = oi->get_occluder();
  1643. if (o.is_null()) {
  1644. return;
  1645. }
  1646. Transform3D gt = oi->get_global_transform();
  1647. Transform3D gi = gt.affine_inverse();
  1648. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1649. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1650. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  1651. bool snap_enabled = Node3DEditor::get_singleton()->is_snap_enabled();
  1652. float snap = Node3DEditor::get_singleton()->get_translate_snap();
  1653. if (Object::cast_to<SphereOccluder3D>(*o)) {
  1654. Ref<SphereOccluder3D> so = o;
  1655. Vector3 ra, rb;
  1656. Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
  1657. float d = ra.x;
  1658. if (snap_enabled) {
  1659. d = Math::snapped(d, snap);
  1660. }
  1661. if (d < 0.001) {
  1662. d = 0.001;
  1663. }
  1664. so->set_radius(d);
  1665. }
  1666. if (Object::cast_to<BoxOccluder3D>(*o)) {
  1667. Vector3 axis;
  1668. axis[p_id] = 1.0;
  1669. Ref<BoxOccluder3D> bo = o;
  1670. Vector3 ra, rb;
  1671. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  1672. float d = ra[p_id];
  1673. if (snap_enabled) {
  1674. d = Math::snapped(d, snap);
  1675. }
  1676. if (d < 0.001) {
  1677. d = 0.001;
  1678. }
  1679. Vector3 he = bo->get_size();
  1680. he[p_id] = d * 2;
  1681. bo->set_size(he);
  1682. }
  1683. if (Object::cast_to<QuadOccluder3D>(*o)) {
  1684. Ref<QuadOccluder3D> qo = o;
  1685. Plane p = Plane(Vector3(0.0f, 0.0f, 1.0f), 0.0f);
  1686. Vector3 intersection;
  1687. if (!p.intersects_segment(sg[0], sg[1], &intersection)) {
  1688. return;
  1689. }
  1690. if (p_id == 2) {
  1691. Vector2 s = Vector2(intersection.x, intersection.y) * 2.0f;
  1692. if (snap_enabled) {
  1693. s = s.snapped(Vector2(snap, snap));
  1694. }
  1695. s = s.max(Vector2(0.001, 0.001));
  1696. qo->set_size(s);
  1697. } else {
  1698. float d = intersection[p_id];
  1699. if (snap_enabled) {
  1700. d = Math::snapped(d, snap);
  1701. }
  1702. if (d < 0.001) {
  1703. d = 0.001;
  1704. }
  1705. Vector2 he = qo->get_size();
  1706. he[p_id] = d * 2.0f;
  1707. qo->set_size(he);
  1708. }
  1709. }
  1710. }
  1711. void OccluderInstance3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  1712. OccluderInstance3D *oi = Object::cast_to<OccluderInstance3D>(p_gizmo->get_spatial_node());
  1713. Ref<Occluder3D> o = oi->get_occluder();
  1714. if (o.is_null()) {
  1715. return;
  1716. }
  1717. if (Object::cast_to<SphereOccluder3D>(*o)) {
  1718. Ref<SphereOccluder3D> so = o;
  1719. if (p_cancel) {
  1720. so->set_radius(p_restore);
  1721. return;
  1722. }
  1723. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  1724. ur->create_action(TTR("Change Sphere Shape Radius"));
  1725. ur->add_do_method(so.ptr(), "set_radius", so->get_radius());
  1726. ur->add_undo_method(so.ptr(), "set_radius", p_restore);
  1727. ur->commit_action();
  1728. }
  1729. if (Object::cast_to<BoxOccluder3D>(*o)) {
  1730. Ref<BoxOccluder3D> bo = o;
  1731. if (p_cancel) {
  1732. bo->set_size(p_restore);
  1733. return;
  1734. }
  1735. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  1736. ur->create_action(TTR("Change Box Shape Size"));
  1737. ur->add_do_method(bo.ptr(), "set_size", bo->get_size());
  1738. ur->add_undo_method(bo.ptr(), "set_size", p_restore);
  1739. ur->commit_action();
  1740. }
  1741. if (Object::cast_to<QuadOccluder3D>(*o)) {
  1742. Ref<QuadOccluder3D> qo = o;
  1743. if (p_cancel) {
  1744. qo->set_size(p_restore);
  1745. return;
  1746. }
  1747. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  1748. ur->create_action(TTR("Change Box Shape Size"));
  1749. ur->add_do_method(qo.ptr(), "set_size", qo->get_size());
  1750. ur->add_undo_method(qo.ptr(), "set_size", p_restore);
  1751. ur->commit_action();
  1752. }
  1753. }
  1754. void OccluderInstance3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1755. OccluderInstance3D *occluder_instance = Object::cast_to<OccluderInstance3D>(p_gizmo->get_spatial_node());
  1756. p_gizmo->clear();
  1757. Ref<Occluder3D> o = occluder_instance->get_occluder();
  1758. if (!o.is_valid()) {
  1759. return;
  1760. }
  1761. Vector<Vector3> lines = o->get_debug_lines();
  1762. if (!lines.is_empty()) {
  1763. Ref<Material> material = get_material("line_material", p_gizmo);
  1764. p_gizmo->add_lines(lines, material);
  1765. p_gizmo->add_collision_segments(lines);
  1766. }
  1767. Ref<Material> handles_material = get_material("handles");
  1768. if (Object::cast_to<SphereOccluder3D>(*o)) {
  1769. Ref<SphereOccluder3D> so = o;
  1770. float r = so->get_radius();
  1771. Vector<Vector3> handles = { Vector3(r, 0, 0) };
  1772. p_gizmo->add_handles(handles, handles_material);
  1773. }
  1774. if (Object::cast_to<BoxOccluder3D>(*o)) {
  1775. Ref<BoxOccluder3D> bo = o;
  1776. Vector<Vector3> handles;
  1777. for (int i = 0; i < 3; i++) {
  1778. Vector3 ax;
  1779. ax[i] = bo->get_size()[i] / 2;
  1780. handles.push_back(ax);
  1781. }
  1782. p_gizmo->add_handles(handles, handles_material);
  1783. }
  1784. if (Object::cast_to<QuadOccluder3D>(*o)) {
  1785. Ref<QuadOccluder3D> qo = o;
  1786. Vector2 size = qo->get_size();
  1787. Vector3 s = Vector3(size.x, size.y, 0.0f) / 2.0f;
  1788. Vector<Vector3> handles = { Vector3(s.x, 0.0f, 0.0f), Vector3(0.0f, s.y, 0.0f), Vector3(s.x, s.y, 0.0f) };
  1789. p_gizmo->add_handles(handles, handles_material);
  1790. }
  1791. }
  1792. /////
  1793. Sprite3DGizmoPlugin::Sprite3DGizmoPlugin() {
  1794. }
  1795. bool Sprite3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1796. return Object::cast_to<Sprite3D>(p_spatial) != nullptr;
  1797. }
  1798. String Sprite3DGizmoPlugin::get_gizmo_name() const {
  1799. return "Sprite3D";
  1800. }
  1801. int Sprite3DGizmoPlugin::get_priority() const {
  1802. return -1;
  1803. }
  1804. bool Sprite3DGizmoPlugin::can_be_hidden() const {
  1805. return false;
  1806. }
  1807. void Sprite3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1808. Sprite3D *sprite = Object::cast_to<Sprite3D>(p_gizmo->get_spatial_node());
  1809. p_gizmo->clear();
  1810. Ref<TriangleMesh> tm = sprite->generate_triangle_mesh();
  1811. if (tm.is_valid()) {
  1812. p_gizmo->add_collision_triangles(tm);
  1813. }
  1814. }
  1815. ///
  1816. Label3DGizmoPlugin::Label3DGizmoPlugin() {
  1817. }
  1818. bool Label3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1819. return Object::cast_to<Label3D>(p_spatial) != nullptr;
  1820. }
  1821. String Label3DGizmoPlugin::get_gizmo_name() const {
  1822. return "Label3D";
  1823. }
  1824. int Label3DGizmoPlugin::get_priority() const {
  1825. return -1;
  1826. }
  1827. bool Label3DGizmoPlugin::can_be_hidden() const {
  1828. return false;
  1829. }
  1830. void Label3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1831. Label3D *label = Object::cast_to<Label3D>(p_gizmo->get_spatial_node());
  1832. p_gizmo->clear();
  1833. Ref<TriangleMesh> tm = label->generate_triangle_mesh();
  1834. if (tm.is_valid()) {
  1835. p_gizmo->add_collision_triangles(tm);
  1836. }
  1837. }
  1838. ///
  1839. Marker3DGizmoPlugin::Marker3DGizmoPlugin() {
  1840. pos3d_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  1841. cursor_points = Vector<Vector3>();
  1842. Vector<Color> cursor_colors;
  1843. const float cs = 0.25;
  1844. // Add more points to create a "hard stop" in the color gradient.
  1845. cursor_points.push_back(Vector3(+cs, 0, 0));
  1846. cursor_points.push_back(Vector3());
  1847. cursor_points.push_back(Vector3());
  1848. cursor_points.push_back(Vector3(-cs, 0, 0));
  1849. cursor_points.push_back(Vector3(0, +cs, 0));
  1850. cursor_points.push_back(Vector3());
  1851. cursor_points.push_back(Vector3());
  1852. cursor_points.push_back(Vector3(0, -cs, 0));
  1853. cursor_points.push_back(Vector3(0, 0, +cs));
  1854. cursor_points.push_back(Vector3());
  1855. cursor_points.push_back(Vector3());
  1856. cursor_points.push_back(Vector3(0, 0, -cs));
  1857. // Use the axis color which is brighter for the positive axis.
  1858. // Use a darkened axis color for the negative axis.
  1859. // This makes it possible to see in which direction the Marker3D node is rotated
  1860. // (which can be important depending on how it's used).
  1861. const Color color_x = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("axis_x_color"), SNAME("Editor"));
  1862. cursor_colors.push_back(color_x);
  1863. cursor_colors.push_back(color_x);
  1864. // FIXME: Use less strong darkening factor once GH-48573 is fixed.
  1865. // The current darkening factor compensates for lines being too bright in the 3D editor.
  1866. cursor_colors.push_back(color_x.lerp(Color(0, 0, 0), 0.75));
  1867. cursor_colors.push_back(color_x.lerp(Color(0, 0, 0), 0.75));
  1868. const Color color_y = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("axis_y_color"), SNAME("Editor"));
  1869. cursor_colors.push_back(color_y);
  1870. cursor_colors.push_back(color_y);
  1871. cursor_colors.push_back(color_y.lerp(Color(0, 0, 0), 0.75));
  1872. cursor_colors.push_back(color_y.lerp(Color(0, 0, 0), 0.75));
  1873. const Color color_z = EditorNode::get_singleton()->get_gui_base()->get_theme_color(SNAME("axis_z_color"), SNAME("Editor"));
  1874. cursor_colors.push_back(color_z);
  1875. cursor_colors.push_back(color_z);
  1876. cursor_colors.push_back(color_z.lerp(Color(0, 0, 0), 0.75));
  1877. cursor_colors.push_back(color_z.lerp(Color(0, 0, 0), 0.75));
  1878. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  1879. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  1880. mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1881. mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, true);
  1882. mat->set_transparency(StandardMaterial3D::TRANSPARENCY_ALPHA);
  1883. Array d;
  1884. d.resize(RS::ARRAY_MAX);
  1885. d[Mesh::ARRAY_VERTEX] = cursor_points;
  1886. d[Mesh::ARRAY_COLOR] = cursor_colors;
  1887. pos3d_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, d);
  1888. pos3d_mesh->surface_set_material(0, mat);
  1889. }
  1890. bool Marker3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1891. return Object::cast_to<Marker3D>(p_spatial) != nullptr;
  1892. }
  1893. String Marker3DGizmoPlugin::get_gizmo_name() const {
  1894. return "Marker3D";
  1895. }
  1896. int Marker3DGizmoPlugin::get_priority() const {
  1897. return -1;
  1898. }
  1899. void Marker3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1900. p_gizmo->clear();
  1901. p_gizmo->add_mesh(pos3d_mesh);
  1902. p_gizmo->add_collision_segments(cursor_points);
  1903. }
  1904. ////
  1905. PhysicalBone3DGizmoPlugin::PhysicalBone3DGizmoPlugin() {
  1906. create_material("joint_material", EDITOR_GET("editors/3d_gizmos/gizmo_colors/joint"));
  1907. }
  1908. bool PhysicalBone3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  1909. return Object::cast_to<PhysicalBone3D>(p_spatial) != nullptr;
  1910. }
  1911. String PhysicalBone3DGizmoPlugin::get_gizmo_name() const {
  1912. return "PhysicalBone3D";
  1913. }
  1914. int PhysicalBone3DGizmoPlugin::get_priority() const {
  1915. return -1;
  1916. }
  1917. void PhysicalBone3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  1918. p_gizmo->clear();
  1919. PhysicalBone3D *physical_bone = Object::cast_to<PhysicalBone3D>(p_gizmo->get_spatial_node());
  1920. if (!physical_bone) {
  1921. return;
  1922. }
  1923. Skeleton3D *sk(physical_bone->find_skeleton_parent());
  1924. if (!sk) {
  1925. return;
  1926. }
  1927. PhysicalBone3D *pb(sk->get_physical_bone(physical_bone->get_bone_id()));
  1928. if (!pb) {
  1929. return;
  1930. }
  1931. PhysicalBone3D *pbp(sk->get_physical_bone_parent(physical_bone->get_bone_id()));
  1932. if (!pbp) {
  1933. return;
  1934. }
  1935. Vector<Vector3> points;
  1936. switch (physical_bone->get_joint_type()) {
  1937. case PhysicalBone3D::JOINT_TYPE_PIN: {
  1938. Joint3DGizmoPlugin::CreatePinJointGizmo(physical_bone->get_joint_offset(), points);
  1939. } break;
  1940. case PhysicalBone3D::JOINT_TYPE_CONE: {
  1941. const PhysicalBone3D::ConeJointData *cjd(static_cast<const PhysicalBone3D::ConeJointData *>(physical_bone->get_joint_data()));
  1942. Joint3DGizmoPlugin::CreateConeTwistJointGizmo(
  1943. physical_bone->get_joint_offset(),
  1944. physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
  1945. pb->get_global_transform(),
  1946. pbp->get_global_transform(),
  1947. cjd->swing_span,
  1948. cjd->twist_span,
  1949. &points,
  1950. &points);
  1951. } break;
  1952. case PhysicalBone3D::JOINT_TYPE_HINGE: {
  1953. const PhysicalBone3D::HingeJointData *hjd(static_cast<const PhysicalBone3D::HingeJointData *>(physical_bone->get_joint_data()));
  1954. Joint3DGizmoPlugin::CreateHingeJointGizmo(
  1955. physical_bone->get_joint_offset(),
  1956. physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
  1957. pb->get_global_transform(),
  1958. pbp->get_global_transform(),
  1959. hjd->angular_limit_lower,
  1960. hjd->angular_limit_upper,
  1961. hjd->angular_limit_enabled,
  1962. points,
  1963. &points,
  1964. &points);
  1965. } break;
  1966. case PhysicalBone3D::JOINT_TYPE_SLIDER: {
  1967. const PhysicalBone3D::SliderJointData *sjd(static_cast<const PhysicalBone3D::SliderJointData *>(physical_bone->get_joint_data()));
  1968. Joint3DGizmoPlugin::CreateSliderJointGizmo(
  1969. physical_bone->get_joint_offset(),
  1970. physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
  1971. pb->get_global_transform(),
  1972. pbp->get_global_transform(),
  1973. sjd->angular_limit_lower,
  1974. sjd->angular_limit_upper,
  1975. sjd->linear_limit_lower,
  1976. sjd->linear_limit_upper,
  1977. points,
  1978. &points,
  1979. &points);
  1980. } break;
  1981. case PhysicalBone3D::JOINT_TYPE_6DOF: {
  1982. const PhysicalBone3D::SixDOFJointData *sdofjd(static_cast<const PhysicalBone3D::SixDOFJointData *>(physical_bone->get_joint_data()));
  1983. Joint3DGizmoPlugin::CreateGeneric6DOFJointGizmo(
  1984. physical_bone->get_joint_offset(),
  1985. physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
  1986. pb->get_global_transform(),
  1987. pbp->get_global_transform(),
  1988. sdofjd->axis_data[0].angular_limit_lower,
  1989. sdofjd->axis_data[0].angular_limit_upper,
  1990. sdofjd->axis_data[0].linear_limit_lower,
  1991. sdofjd->axis_data[0].linear_limit_upper,
  1992. sdofjd->axis_data[0].angular_limit_enabled,
  1993. sdofjd->axis_data[0].linear_limit_enabled,
  1994. sdofjd->axis_data[1].angular_limit_lower,
  1995. sdofjd->axis_data[1].angular_limit_upper,
  1996. sdofjd->axis_data[1].linear_limit_lower,
  1997. sdofjd->axis_data[1].linear_limit_upper,
  1998. sdofjd->axis_data[1].angular_limit_enabled,
  1999. sdofjd->axis_data[1].linear_limit_enabled,
  2000. sdofjd->axis_data[2].angular_limit_lower,
  2001. sdofjd->axis_data[2].angular_limit_upper,
  2002. sdofjd->axis_data[2].linear_limit_lower,
  2003. sdofjd->axis_data[2].linear_limit_upper,
  2004. sdofjd->axis_data[2].angular_limit_enabled,
  2005. sdofjd->axis_data[2].linear_limit_enabled,
  2006. points,
  2007. &points,
  2008. &points);
  2009. } break;
  2010. default:
  2011. return;
  2012. }
  2013. Ref<Material> material = get_material("joint_material", p_gizmo);
  2014. p_gizmo->add_collision_segments(points);
  2015. p_gizmo->add_lines(points, material);
  2016. }
  2017. /////
  2018. RayCast3DGizmoPlugin::RayCast3DGizmoPlugin() {
  2019. const Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
  2020. create_material("shape_material", gizmo_color);
  2021. const float gizmo_value = gizmo_color.get_v();
  2022. const Color gizmo_color_disabled = Color(gizmo_value, gizmo_value, gizmo_value, 0.65);
  2023. create_material("shape_material_disabled", gizmo_color_disabled);
  2024. }
  2025. bool RayCast3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2026. return Object::cast_to<RayCast3D>(p_spatial) != nullptr;
  2027. }
  2028. String RayCast3DGizmoPlugin::get_gizmo_name() const {
  2029. return "RayCast3D";
  2030. }
  2031. int RayCast3DGizmoPlugin::get_priority() const {
  2032. return -1;
  2033. }
  2034. void RayCast3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2035. RayCast3D *raycast = Object::cast_to<RayCast3D>(p_gizmo->get_spatial_node());
  2036. p_gizmo->clear();
  2037. const Ref<StandardMaterial3D> material = raycast->is_enabled() ? raycast->get_debug_material() : get_material("shape_material_disabled");
  2038. p_gizmo->add_lines(raycast->get_debug_line_vertices(), material);
  2039. if (raycast->get_debug_shape_thickness() > 1) {
  2040. p_gizmo->add_vertices(raycast->get_debug_shape_vertices(), material, Mesh::PRIMITIVE_TRIANGLE_STRIP);
  2041. }
  2042. p_gizmo->add_collision_segments(raycast->get_debug_line_vertices());
  2043. }
  2044. /////
  2045. ShapeCast3DGizmoPlugin::ShapeCast3DGizmoPlugin() {
  2046. const Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
  2047. create_material("shape_material", gizmo_color);
  2048. const float gizmo_value = gizmo_color.get_v();
  2049. const Color gizmo_color_disabled = Color(gizmo_value, gizmo_value, gizmo_value, 0.65);
  2050. create_material("shape_material_disabled", gizmo_color_disabled);
  2051. }
  2052. bool ShapeCast3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2053. return Object::cast_to<ShapeCast3D>(p_spatial) != nullptr;
  2054. }
  2055. String ShapeCast3DGizmoPlugin::get_gizmo_name() const {
  2056. return "ShapeCast3D";
  2057. }
  2058. int ShapeCast3DGizmoPlugin::get_priority() const {
  2059. return -1;
  2060. }
  2061. void ShapeCast3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2062. ShapeCast3D *shapecast = Object::cast_to<ShapeCast3D>(p_gizmo->get_spatial_node());
  2063. p_gizmo->clear();
  2064. const Ref<StandardMaterial3D> material = shapecast->is_enabled() ? shapecast->get_debug_material() : get_material("shape_material_disabled");
  2065. p_gizmo->add_lines(shapecast->get_debug_line_vertices(), material);
  2066. if (shapecast->get_shape().is_valid()) {
  2067. p_gizmo->add_lines(shapecast->get_debug_shape_vertices(), material);
  2068. }
  2069. p_gizmo->add_collision_segments(shapecast->get_debug_line_vertices());
  2070. }
  2071. /////
  2072. void SpringArm3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2073. SpringArm3D *spring_arm = Object::cast_to<SpringArm3D>(p_gizmo->get_spatial_node());
  2074. p_gizmo->clear();
  2075. Vector<Vector3> lines = {
  2076. Vector3(),
  2077. Vector3(0, 0, 1.0) * spring_arm->get_length()
  2078. };
  2079. Ref<StandardMaterial3D> material = get_material("shape_material", p_gizmo);
  2080. p_gizmo->add_lines(lines, material);
  2081. p_gizmo->add_collision_segments(lines);
  2082. }
  2083. SpringArm3DGizmoPlugin::SpringArm3DGizmoPlugin() {
  2084. Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
  2085. create_material("shape_material", gizmo_color);
  2086. }
  2087. bool SpringArm3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2088. return Object::cast_to<SpringArm3D>(p_spatial) != nullptr;
  2089. }
  2090. String SpringArm3DGizmoPlugin::get_gizmo_name() const {
  2091. return "SpringArm3D";
  2092. }
  2093. int SpringArm3DGizmoPlugin::get_priority() const {
  2094. return -1;
  2095. }
  2096. /////
  2097. VehicleWheel3DGizmoPlugin::VehicleWheel3DGizmoPlugin() {
  2098. Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
  2099. create_material("shape_material", gizmo_color);
  2100. }
  2101. bool VehicleWheel3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2102. return Object::cast_to<VehicleWheel3D>(p_spatial) != nullptr;
  2103. }
  2104. String VehicleWheel3DGizmoPlugin::get_gizmo_name() const {
  2105. return "VehicleWheel3D";
  2106. }
  2107. int VehicleWheel3DGizmoPlugin::get_priority() const {
  2108. return -1;
  2109. }
  2110. void VehicleWheel3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2111. VehicleWheel3D *car_wheel = Object::cast_to<VehicleWheel3D>(p_gizmo->get_spatial_node());
  2112. p_gizmo->clear();
  2113. Vector<Vector3> points;
  2114. float r = car_wheel->get_radius();
  2115. const int skip = 10;
  2116. for (int i = 0; i <= 360; i += skip) {
  2117. float ra = Math::deg_to_rad((float)i);
  2118. float rb = Math::deg_to_rad((float)i + skip);
  2119. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  2120. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  2121. points.push_back(Vector3(0, a.x, a.y));
  2122. points.push_back(Vector3(0, b.x, b.y));
  2123. const int springsec = 4;
  2124. for (int j = 0; j < springsec; j++) {
  2125. float t = car_wheel->get_suspension_rest_length() * 5;
  2126. points.push_back(Vector3(a.x, i / 360.0 * t / springsec + j * (t / springsec), a.y) * 0.2);
  2127. points.push_back(Vector3(b.x, (i + skip) / 360.0 * t / springsec + j * (t / springsec), b.y) * 0.2);
  2128. }
  2129. }
  2130. //travel
  2131. points.push_back(Vector3(0, 0, 0));
  2132. points.push_back(Vector3(0, car_wheel->get_suspension_rest_length(), 0));
  2133. //axis
  2134. points.push_back(Vector3(r * 0.2, car_wheel->get_suspension_rest_length(), 0));
  2135. points.push_back(Vector3(-r * 0.2, car_wheel->get_suspension_rest_length(), 0));
  2136. //axis
  2137. points.push_back(Vector3(r * 0.2, 0, 0));
  2138. points.push_back(Vector3(-r * 0.2, 0, 0));
  2139. //forward line
  2140. points.push_back(Vector3(0, -r, 0));
  2141. points.push_back(Vector3(0, -r, r * 2));
  2142. points.push_back(Vector3(0, -r, r * 2));
  2143. points.push_back(Vector3(r * 2 * 0.2, -r, r * 2 * 0.8));
  2144. points.push_back(Vector3(0, -r, r * 2));
  2145. points.push_back(Vector3(-r * 2 * 0.2, -r, r * 2 * 0.8));
  2146. Ref<Material> material = get_material("shape_material", p_gizmo);
  2147. p_gizmo->add_lines(points, material);
  2148. p_gizmo->add_collision_segments(points);
  2149. }
  2150. ///////////
  2151. SoftBody3DGizmoPlugin::SoftBody3DGizmoPlugin() {
  2152. Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
  2153. create_material("shape_material", gizmo_color);
  2154. create_handle_material("handles");
  2155. }
  2156. bool SoftBody3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2157. return Object::cast_to<SoftBody3D>(p_spatial) != nullptr;
  2158. }
  2159. String SoftBody3DGizmoPlugin::get_gizmo_name() const {
  2160. return "SoftBody3D";
  2161. }
  2162. int SoftBody3DGizmoPlugin::get_priority() const {
  2163. return -1;
  2164. }
  2165. bool SoftBody3DGizmoPlugin::is_selectable_when_hidden() const {
  2166. return true;
  2167. }
  2168. void SoftBody3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2169. SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_spatial_node());
  2170. p_gizmo->clear();
  2171. if (!soft_body || soft_body->get_mesh().is_null()) {
  2172. return;
  2173. }
  2174. // find mesh
  2175. Vector<Vector3> lines;
  2176. soft_body->get_mesh()->generate_debug_mesh_lines(lines);
  2177. if (!lines.size()) {
  2178. return;
  2179. }
  2180. Ref<TriangleMesh> tm = soft_body->get_mesh()->generate_triangle_mesh();
  2181. Vector<Vector3> points;
  2182. for (int i = 0; i < soft_body->get_mesh()->get_surface_count(); i++) {
  2183. Array arrays = soft_body->get_mesh()->surface_get_arrays(i);
  2184. ERR_CONTINUE(arrays.is_empty());
  2185. const Vector<Vector3> &vertices = arrays[Mesh::ARRAY_VERTEX];
  2186. points.append_array(vertices);
  2187. }
  2188. Ref<Material> material = get_material("shape_material", p_gizmo);
  2189. p_gizmo->add_lines(lines, material);
  2190. p_gizmo->add_handles(points, get_material("handles"));
  2191. p_gizmo->add_collision_triangles(tm);
  2192. }
  2193. String SoftBody3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2194. return "SoftBody3D pin point";
  2195. }
  2196. Variant SoftBody3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2197. SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_spatial_node());
  2198. return Variant(soft_body->is_point_pinned(p_id));
  2199. }
  2200. void SoftBody3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  2201. SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_spatial_node());
  2202. soft_body->pin_point_toggle(p_id);
  2203. }
  2204. bool SoftBody3DGizmoPlugin::is_handle_highlighted(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2205. SoftBody3D *soft_body = Object::cast_to<SoftBody3D>(p_gizmo->get_spatial_node());
  2206. return soft_body->is_point_pinned(p_id);
  2207. }
  2208. ///////////
  2209. VisibleOnScreenNotifier3DGizmoPlugin::VisibleOnScreenNotifier3DGizmoPlugin() {
  2210. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/visibility_notifier", Color(0.8, 0.5, 0.7));
  2211. create_material("visibility_notifier_material", gizmo_color);
  2212. gizmo_color.a = 0.1;
  2213. create_material("visibility_notifier_solid_material", gizmo_color);
  2214. create_handle_material("handles");
  2215. }
  2216. bool VisibleOnScreenNotifier3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2217. return Object::cast_to<VisibleOnScreenNotifier3D>(p_spatial) != nullptr;
  2218. }
  2219. String VisibleOnScreenNotifier3DGizmoPlugin::get_gizmo_name() const {
  2220. return "VisibleOnScreenNotifier3D";
  2221. }
  2222. int VisibleOnScreenNotifier3DGizmoPlugin::get_priority() const {
  2223. return -1;
  2224. }
  2225. String VisibleOnScreenNotifier3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2226. switch (p_id) {
  2227. case 0:
  2228. return "Size X";
  2229. case 1:
  2230. return "Size Y";
  2231. case 2:
  2232. return "Size Z";
  2233. case 3:
  2234. return "Pos X";
  2235. case 4:
  2236. return "Pos Y";
  2237. case 5:
  2238. return "Pos Z";
  2239. }
  2240. return "";
  2241. }
  2242. Variant VisibleOnScreenNotifier3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2243. VisibleOnScreenNotifier3D *notifier = Object::cast_to<VisibleOnScreenNotifier3D>(p_gizmo->get_spatial_node());
  2244. return notifier->get_aabb();
  2245. }
  2246. void VisibleOnScreenNotifier3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  2247. VisibleOnScreenNotifier3D *notifier = Object::cast_to<VisibleOnScreenNotifier3D>(p_gizmo->get_spatial_node());
  2248. Transform3D gt = notifier->get_global_transform();
  2249. Transform3D gi = gt.affine_inverse();
  2250. bool move = p_id >= 3;
  2251. p_id = p_id % 3;
  2252. AABB aabb = notifier->get_aabb();
  2253. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  2254. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  2255. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  2256. Vector3 ofs = aabb.get_center();
  2257. Vector3 axis;
  2258. axis[p_id] = 1.0;
  2259. if (move) {
  2260. Vector3 ra, rb;
  2261. Geometry3D::get_closest_points_between_segments(ofs - axis * 4096, ofs + axis * 4096, sg[0], sg[1], ra, rb);
  2262. float d = ra[p_id];
  2263. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  2264. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  2265. }
  2266. aabb.position[p_id] = d - 1.0 - aabb.size[p_id] * 0.5;
  2267. notifier->set_aabb(aabb);
  2268. } else {
  2269. Vector3 ra, rb;
  2270. Geometry3D::get_closest_points_between_segments(ofs, ofs + axis * 4096, sg[0], sg[1], ra, rb);
  2271. float d = ra[p_id] - ofs[p_id];
  2272. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  2273. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  2274. }
  2275. if (d < 0.001) {
  2276. d = 0.001;
  2277. }
  2278. //resize
  2279. aabb.position[p_id] = (aabb.position[p_id] + aabb.size[p_id] * 0.5) - d;
  2280. aabb.size[p_id] = d * 2;
  2281. notifier->set_aabb(aabb);
  2282. }
  2283. }
  2284. void VisibleOnScreenNotifier3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  2285. VisibleOnScreenNotifier3D *notifier = Object::cast_to<VisibleOnScreenNotifier3D>(p_gizmo->get_spatial_node());
  2286. if (p_cancel) {
  2287. notifier->set_aabb(p_restore);
  2288. return;
  2289. }
  2290. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  2291. ur->create_action(TTR("Change Notifier AABB"));
  2292. ur->add_do_method(notifier, "set_aabb", notifier->get_aabb());
  2293. ur->add_undo_method(notifier, "set_aabb", p_restore);
  2294. ur->commit_action();
  2295. }
  2296. void VisibleOnScreenNotifier3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2297. VisibleOnScreenNotifier3D *notifier = Object::cast_to<VisibleOnScreenNotifier3D>(p_gizmo->get_spatial_node());
  2298. p_gizmo->clear();
  2299. Vector<Vector3> lines;
  2300. AABB aabb = notifier->get_aabb();
  2301. for (int i = 0; i < 12; i++) {
  2302. Vector3 a, b;
  2303. aabb.get_edge(i, a, b);
  2304. lines.push_back(a);
  2305. lines.push_back(b);
  2306. }
  2307. Vector<Vector3> handles;
  2308. for (int i = 0; i < 3; i++) {
  2309. Vector3 ax;
  2310. ax[i] = aabb.position[i] + aabb.size[i];
  2311. ax[(i + 1) % 3] = aabb.position[(i + 1) % 3] + aabb.size[(i + 1) % 3] * 0.5;
  2312. ax[(i + 2) % 3] = aabb.position[(i + 2) % 3] + aabb.size[(i + 2) % 3] * 0.5;
  2313. handles.push_back(ax);
  2314. }
  2315. Vector3 center = aabb.get_center();
  2316. for (int i = 0; i < 3; i++) {
  2317. Vector3 ax;
  2318. ax[i] = 1.0;
  2319. handles.push_back(center + ax);
  2320. lines.push_back(center);
  2321. lines.push_back(center + ax);
  2322. }
  2323. Ref<Material> material = get_material("visibility_notifier_material", p_gizmo);
  2324. p_gizmo->add_lines(lines, material);
  2325. p_gizmo->add_collision_segments(lines);
  2326. if (p_gizmo->is_selected()) {
  2327. Ref<Material> solid_material = get_material("visibility_notifier_solid_material", p_gizmo);
  2328. p_gizmo->add_solid_box(solid_material, aabb.get_size(), aabb.get_center());
  2329. }
  2330. p_gizmo->add_handles(handles, get_material("handles"));
  2331. }
  2332. ////
  2333. CPUParticles3DGizmoPlugin::CPUParticles3DGizmoPlugin() {
  2334. create_icon_material("particles_icon", Node3DEditor::get_singleton()->get_theme_icon(SNAME("GizmoCPUParticles3D"), SNAME("EditorIcons")));
  2335. }
  2336. bool CPUParticles3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2337. return Object::cast_to<CPUParticles3D>(p_spatial) != nullptr;
  2338. }
  2339. String CPUParticles3DGizmoPlugin::get_gizmo_name() const {
  2340. return "CPUParticles3D";
  2341. }
  2342. int CPUParticles3DGizmoPlugin::get_priority() const {
  2343. return -1;
  2344. }
  2345. bool CPUParticles3DGizmoPlugin::is_selectable_when_hidden() const {
  2346. return true;
  2347. }
  2348. void CPUParticles3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2349. Ref<Material> icon = get_material("particles_icon", p_gizmo);
  2350. p_gizmo->add_unscaled_billboard(icon, 0.05);
  2351. }
  2352. ////
  2353. GPUParticles3DGizmoPlugin::GPUParticles3DGizmoPlugin() {
  2354. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/particles", Color(0.8, 0.7, 0.4));
  2355. create_material("particles_material", gizmo_color);
  2356. gizmo_color.a = MAX((gizmo_color.a - 0.2) * 0.02, 0.0);
  2357. create_material("particles_solid_material", gizmo_color);
  2358. create_icon_material("particles_icon", Node3DEditor::get_singleton()->get_theme_icon(SNAME("GizmoGPUParticles3D"), SNAME("EditorIcons")));
  2359. create_handle_material("handles");
  2360. }
  2361. bool GPUParticles3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2362. return Object::cast_to<GPUParticles3D>(p_spatial) != nullptr;
  2363. }
  2364. String GPUParticles3DGizmoPlugin::get_gizmo_name() const {
  2365. return "GPUParticles3D";
  2366. }
  2367. int GPUParticles3DGizmoPlugin::get_priority() const {
  2368. return -1;
  2369. }
  2370. bool GPUParticles3DGizmoPlugin::is_selectable_when_hidden() const {
  2371. return true;
  2372. }
  2373. String GPUParticles3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2374. switch (p_id) {
  2375. case 0:
  2376. return "Size X";
  2377. case 1:
  2378. return "Size Y";
  2379. case 2:
  2380. return "Size Z";
  2381. case 3:
  2382. return "Pos X";
  2383. case 4:
  2384. return "Pos Y";
  2385. case 5:
  2386. return "Pos Z";
  2387. }
  2388. return "";
  2389. }
  2390. Variant GPUParticles3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2391. GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(p_gizmo->get_spatial_node());
  2392. return particles->get_visibility_aabb();
  2393. }
  2394. void GPUParticles3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  2395. GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(p_gizmo->get_spatial_node());
  2396. Transform3D gt = particles->get_global_transform();
  2397. Transform3D gi = gt.affine_inverse();
  2398. bool move = p_id >= 3;
  2399. p_id = p_id % 3;
  2400. AABB aabb = particles->get_visibility_aabb();
  2401. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  2402. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  2403. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  2404. Vector3 ofs = aabb.get_center();
  2405. Vector3 axis;
  2406. axis[p_id] = 1.0;
  2407. if (move) {
  2408. Vector3 ra, rb;
  2409. Geometry3D::get_closest_points_between_segments(ofs - axis * 4096, ofs + axis * 4096, sg[0], sg[1], ra, rb);
  2410. float d = ra[p_id];
  2411. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  2412. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  2413. }
  2414. aabb.position[p_id] = d - 1.0 - aabb.size[p_id] * 0.5;
  2415. particles->set_visibility_aabb(aabb);
  2416. } else {
  2417. Vector3 ra, rb;
  2418. Geometry3D::get_closest_points_between_segments(ofs, ofs + axis * 4096, sg[0], sg[1], ra, rb);
  2419. float d = ra[p_id] - ofs[p_id];
  2420. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  2421. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  2422. }
  2423. if (d < 0.001) {
  2424. d = 0.001;
  2425. }
  2426. //resize
  2427. aabb.position[p_id] = (aabb.position[p_id] + aabb.size[p_id] * 0.5) - d;
  2428. aabb.size[p_id] = d * 2;
  2429. particles->set_visibility_aabb(aabb);
  2430. }
  2431. }
  2432. void GPUParticles3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  2433. GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(p_gizmo->get_spatial_node());
  2434. if (p_cancel) {
  2435. particles->set_visibility_aabb(p_restore);
  2436. return;
  2437. }
  2438. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  2439. ur->create_action(TTR("Change Particles AABB"));
  2440. ur->add_do_method(particles, "set_visibility_aabb", particles->get_visibility_aabb());
  2441. ur->add_undo_method(particles, "set_visibility_aabb", p_restore);
  2442. ur->commit_action();
  2443. }
  2444. void GPUParticles3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2445. GPUParticles3D *particles = Object::cast_to<GPUParticles3D>(p_gizmo->get_spatial_node());
  2446. p_gizmo->clear();
  2447. Vector<Vector3> lines;
  2448. AABB aabb = particles->get_visibility_aabb();
  2449. for (int i = 0; i < 12; i++) {
  2450. Vector3 a, b;
  2451. aabb.get_edge(i, a, b);
  2452. lines.push_back(a);
  2453. lines.push_back(b);
  2454. }
  2455. Vector<Vector3> handles;
  2456. for (int i = 0; i < 3; i++) {
  2457. Vector3 ax;
  2458. ax[i] = aabb.position[i] + aabb.size[i];
  2459. ax[(i + 1) % 3] = aabb.position[(i + 1) % 3] + aabb.size[(i + 1) % 3] * 0.5;
  2460. ax[(i + 2) % 3] = aabb.position[(i + 2) % 3] + aabb.size[(i + 2) % 3] * 0.5;
  2461. handles.push_back(ax);
  2462. }
  2463. Vector3 center = aabb.get_center();
  2464. for (int i = 0; i < 3; i++) {
  2465. Vector3 ax;
  2466. ax[i] = 1.0;
  2467. handles.push_back(center + ax);
  2468. lines.push_back(center);
  2469. lines.push_back(center + ax);
  2470. }
  2471. Ref<Material> material = get_material("particles_material", p_gizmo);
  2472. Ref<Material> icon = get_material("particles_icon", p_gizmo);
  2473. p_gizmo->add_lines(lines, material);
  2474. if (p_gizmo->is_selected()) {
  2475. Ref<Material> solid_material = get_material("particles_solid_material", p_gizmo);
  2476. p_gizmo->add_solid_box(solid_material, aabb.get_size(), aabb.get_center());
  2477. }
  2478. p_gizmo->add_handles(handles, get_material("handles"));
  2479. p_gizmo->add_unscaled_billboard(icon, 0.05);
  2480. }
  2481. ////
  2482. GPUParticlesCollision3DGizmoPlugin::GPUParticlesCollision3DGizmoPlugin() {
  2483. Color gizmo_color_attractor = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/particle_attractor", Color(1, 0.7, 0.5));
  2484. create_material("shape_material_attractor", gizmo_color_attractor);
  2485. gizmo_color_attractor.a = 0.15;
  2486. create_material("shape_material_attractor_internal", gizmo_color_attractor);
  2487. Color gizmo_color_collision = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/particle_collision", Color(0.5, 0.7, 1));
  2488. create_material("shape_material_collision", gizmo_color_collision);
  2489. gizmo_color_collision.a = 0.15;
  2490. create_material("shape_material_collision_internal", gizmo_color_collision);
  2491. create_handle_material("handles");
  2492. }
  2493. bool GPUParticlesCollision3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2494. return (Object::cast_to<GPUParticlesCollision3D>(p_spatial) != nullptr) || (Object::cast_to<GPUParticlesAttractor3D>(p_spatial) != nullptr);
  2495. }
  2496. String GPUParticlesCollision3DGizmoPlugin::get_gizmo_name() const {
  2497. return "GPUParticlesCollision3D";
  2498. }
  2499. int GPUParticlesCollision3DGizmoPlugin::get_priority() const {
  2500. return -1;
  2501. }
  2502. String GPUParticlesCollision3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2503. const Node3D *cs = p_gizmo->get_spatial_node();
  2504. if (Object::cast_to<GPUParticlesCollisionSphere3D>(cs) || Object::cast_to<GPUParticlesAttractorSphere3D>(cs)) {
  2505. return "Radius";
  2506. }
  2507. if (Object::cast_to<GPUParticlesCollisionBox3D>(cs) || Object::cast_to<GPUParticlesAttractorBox3D>(cs) || Object::cast_to<GPUParticlesAttractorVectorField3D>(cs) || Object::cast_to<GPUParticlesCollisionSDF3D>(cs) || Object::cast_to<GPUParticlesCollisionHeightField3D>(cs)) {
  2508. return "Extents";
  2509. }
  2510. return "";
  2511. }
  2512. Variant GPUParticlesCollision3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2513. const Node3D *cs = p_gizmo->get_spatial_node();
  2514. if (Object::cast_to<GPUParticlesCollisionSphere3D>(cs) || Object::cast_to<GPUParticlesAttractorSphere3D>(cs)) {
  2515. return p_gizmo->get_spatial_node()->call("get_radius");
  2516. }
  2517. if (Object::cast_to<GPUParticlesCollisionBox3D>(cs) || Object::cast_to<GPUParticlesAttractorBox3D>(cs) || Object::cast_to<GPUParticlesAttractorVectorField3D>(cs) || Object::cast_to<GPUParticlesCollisionSDF3D>(cs) || Object::cast_to<GPUParticlesCollisionHeightField3D>(cs)) {
  2518. return Vector3(p_gizmo->get_spatial_node()->call("get_extents"));
  2519. }
  2520. return Variant();
  2521. }
  2522. void GPUParticlesCollision3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  2523. Node3D *sn = p_gizmo->get_spatial_node();
  2524. Transform3D gt = sn->get_global_transform();
  2525. Transform3D gi = gt.affine_inverse();
  2526. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  2527. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  2528. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  2529. if (Object::cast_to<GPUParticlesCollisionSphere3D>(sn) || Object::cast_to<GPUParticlesAttractorSphere3D>(sn)) {
  2530. Vector3 ra, rb;
  2531. Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
  2532. float d = ra.x;
  2533. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  2534. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  2535. }
  2536. if (d < 0.001) {
  2537. d = 0.001;
  2538. }
  2539. sn->call("set_radius", d);
  2540. }
  2541. if (Object::cast_to<GPUParticlesCollisionBox3D>(sn) || Object::cast_to<GPUParticlesAttractorBox3D>(sn) || Object::cast_to<GPUParticlesAttractorVectorField3D>(sn) || Object::cast_to<GPUParticlesCollisionSDF3D>(sn) || Object::cast_to<GPUParticlesCollisionHeightField3D>(sn)) {
  2542. Vector3 axis;
  2543. axis[p_id] = 1.0;
  2544. Vector3 ra, rb;
  2545. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  2546. float d = ra[p_id];
  2547. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  2548. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  2549. }
  2550. if (d < 0.001) {
  2551. d = 0.001;
  2552. }
  2553. Vector3 he = sn->call("get_extents");
  2554. he[p_id] = d;
  2555. sn->call("set_extents", he);
  2556. }
  2557. }
  2558. void GPUParticlesCollision3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  2559. Node3D *sn = p_gizmo->get_spatial_node();
  2560. if (Object::cast_to<GPUParticlesCollisionSphere3D>(sn) || Object::cast_to<GPUParticlesAttractorSphere3D>(sn)) {
  2561. if (p_cancel) {
  2562. sn->call("set_radius", p_restore);
  2563. return;
  2564. }
  2565. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  2566. ur->create_action(TTR("Change Radius"));
  2567. ur->add_do_method(sn, "set_radius", sn->call("get_radius"));
  2568. ur->add_undo_method(sn, "set_radius", p_restore);
  2569. ur->commit_action();
  2570. }
  2571. if (Object::cast_to<GPUParticlesCollisionBox3D>(sn) || Object::cast_to<GPUParticlesAttractorBox3D>(sn) || Object::cast_to<GPUParticlesAttractorVectorField3D>(sn) || Object::cast_to<GPUParticlesCollisionSDF3D>(sn) || Object::cast_to<GPUParticlesCollisionHeightField3D>(sn)) {
  2572. if (p_cancel) {
  2573. sn->call("set_extents", p_restore);
  2574. return;
  2575. }
  2576. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  2577. ur->create_action(TTR("Change Box Shape Extents"));
  2578. ur->add_do_method(sn, "set_extents", sn->call("get_extents"));
  2579. ur->add_undo_method(sn, "set_extents", p_restore);
  2580. ur->commit_action();
  2581. }
  2582. }
  2583. void GPUParticlesCollision3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2584. Node3D *cs = p_gizmo->get_spatial_node();
  2585. p_gizmo->clear();
  2586. Ref<Material> material;
  2587. Ref<Material> material_internal;
  2588. if (Object::cast_to<GPUParticlesAttractor3D>(cs)) {
  2589. material = get_material("shape_material_attractor", p_gizmo);
  2590. material_internal = get_material("shape_material_attractor_internal", p_gizmo);
  2591. } else {
  2592. material = get_material("shape_material_collision", p_gizmo);
  2593. material_internal = get_material("shape_material_collision_internal", p_gizmo);
  2594. }
  2595. const Ref<Material> handles_material = get_material("handles");
  2596. if (Object::cast_to<GPUParticlesCollisionSphere3D>(cs) || Object::cast_to<GPUParticlesAttractorSphere3D>(cs)) {
  2597. float r = cs->call("get_radius");
  2598. Vector<Vector3> points;
  2599. for (int i = 0; i <= 360; i++) {
  2600. float ra = Math::deg_to_rad((float)i);
  2601. float rb = Math::deg_to_rad((float)i + 1);
  2602. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  2603. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  2604. points.push_back(Vector3(a.x, 0, a.y));
  2605. points.push_back(Vector3(b.x, 0, b.y));
  2606. points.push_back(Vector3(0, a.x, a.y));
  2607. points.push_back(Vector3(0, b.x, b.y));
  2608. points.push_back(Vector3(a.x, a.y, 0));
  2609. points.push_back(Vector3(b.x, b.y, 0));
  2610. }
  2611. Vector<Vector3> collision_segments;
  2612. for (int i = 0; i < 64; i++) {
  2613. float ra = i * (Math_TAU / 64.0);
  2614. float rb = (i + 1) * (Math_TAU / 64.0);
  2615. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  2616. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  2617. collision_segments.push_back(Vector3(a.x, 0, a.y));
  2618. collision_segments.push_back(Vector3(b.x, 0, b.y));
  2619. collision_segments.push_back(Vector3(0, a.x, a.y));
  2620. collision_segments.push_back(Vector3(0, b.x, b.y));
  2621. collision_segments.push_back(Vector3(a.x, a.y, 0));
  2622. collision_segments.push_back(Vector3(b.x, b.y, 0));
  2623. }
  2624. p_gizmo->add_lines(points, material);
  2625. p_gizmo->add_collision_segments(collision_segments);
  2626. Vector<Vector3> handles;
  2627. handles.push_back(Vector3(r, 0, 0));
  2628. p_gizmo->add_handles(handles, handles_material);
  2629. }
  2630. if (Object::cast_to<GPUParticlesCollisionBox3D>(cs) || Object::cast_to<GPUParticlesAttractorBox3D>(cs) || Object::cast_to<GPUParticlesAttractorVectorField3D>(cs) || Object::cast_to<GPUParticlesCollisionSDF3D>(cs) || Object::cast_to<GPUParticlesCollisionHeightField3D>(cs)) {
  2631. Vector<Vector3> lines;
  2632. AABB aabb;
  2633. aabb.position = -cs->call("get_extents").operator Vector3();
  2634. aabb.size = aabb.position * -2;
  2635. for (int i = 0; i < 12; i++) {
  2636. Vector3 a, b;
  2637. aabb.get_edge(i, a, b);
  2638. lines.push_back(a);
  2639. lines.push_back(b);
  2640. }
  2641. Vector<Vector3> handles;
  2642. for (int i = 0; i < 3; i++) {
  2643. Vector3 ax;
  2644. ax[i] = cs->call("get_extents").operator Vector3()[i];
  2645. handles.push_back(ax);
  2646. }
  2647. p_gizmo->add_lines(lines, material);
  2648. p_gizmo->add_collision_segments(lines);
  2649. p_gizmo->add_handles(handles, handles_material);
  2650. GPUParticlesCollisionSDF3D *col_sdf = Object::cast_to<GPUParticlesCollisionSDF3D>(cs);
  2651. if (col_sdf) {
  2652. static const int subdivs[GPUParticlesCollisionSDF3D::RESOLUTION_MAX] = { 16, 32, 64, 128, 256, 512 };
  2653. int subdiv = subdivs[col_sdf->get_resolution()];
  2654. float cell_size = aabb.get_longest_axis_size() / subdiv;
  2655. lines.clear();
  2656. for (int i = 1; i < subdiv; i++) {
  2657. for (int j = 0; j < 3; j++) {
  2658. if (cell_size * i > aabb.size[j]) {
  2659. continue;
  2660. }
  2661. int j_n1 = (j + 1) % 3;
  2662. int j_n2 = (j + 2) % 3;
  2663. for (int k = 0; k < 4; k++) {
  2664. Vector3 from = aabb.position, to = aabb.position;
  2665. from[j] += cell_size * i;
  2666. to[j] += cell_size * i;
  2667. if (k & 1) {
  2668. to[j_n1] += aabb.size[j_n1];
  2669. } else {
  2670. to[j_n2] += aabb.size[j_n2];
  2671. }
  2672. if (k & 2) {
  2673. from[j_n1] += aabb.size[j_n1];
  2674. from[j_n2] += aabb.size[j_n2];
  2675. }
  2676. lines.push_back(from);
  2677. lines.push_back(to);
  2678. }
  2679. }
  2680. }
  2681. p_gizmo->add_lines(lines, material_internal);
  2682. }
  2683. }
  2684. }
  2685. /////
  2686. ////
  2687. ReflectionProbeGizmoPlugin::ReflectionProbeGizmoPlugin() {
  2688. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/reflection_probe", Color(0.6, 1, 0.5));
  2689. create_material("reflection_probe_material", gizmo_color);
  2690. gizmo_color.a = 0.5;
  2691. create_material("reflection_internal_material", gizmo_color);
  2692. gizmo_color.a = 0.1;
  2693. create_material("reflection_probe_solid_material", gizmo_color);
  2694. create_icon_material("reflection_probe_icon", Node3DEditor::get_singleton()->get_theme_icon(SNAME("GizmoReflectionProbe"), SNAME("EditorIcons")));
  2695. create_handle_material("handles");
  2696. }
  2697. bool ReflectionProbeGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2698. return Object::cast_to<ReflectionProbe>(p_spatial) != nullptr;
  2699. }
  2700. String ReflectionProbeGizmoPlugin::get_gizmo_name() const {
  2701. return "ReflectionProbe";
  2702. }
  2703. int ReflectionProbeGizmoPlugin::get_priority() const {
  2704. return -1;
  2705. }
  2706. String ReflectionProbeGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2707. switch (p_id) {
  2708. case 0:
  2709. return "Extents X";
  2710. case 1:
  2711. return "Extents Y";
  2712. case 2:
  2713. return "Extents Z";
  2714. case 3:
  2715. return "Origin X";
  2716. case 4:
  2717. return "Origin Y";
  2718. case 5:
  2719. return "Origin Z";
  2720. }
  2721. return "";
  2722. }
  2723. Variant ReflectionProbeGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2724. ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_spatial_node());
  2725. return AABB(probe->get_extents(), probe->get_origin_offset());
  2726. }
  2727. void ReflectionProbeGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  2728. ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_spatial_node());
  2729. Transform3D gt = probe->get_global_transform();
  2730. Transform3D gi = gt.affine_inverse();
  2731. if (p_id < 3) {
  2732. Vector3 extents = probe->get_extents();
  2733. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  2734. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  2735. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };
  2736. Vector3 axis;
  2737. axis[p_id] = 1.0;
  2738. Vector3 ra, rb;
  2739. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
  2740. float d = ra[p_id];
  2741. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  2742. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  2743. }
  2744. if (d < 0.001) {
  2745. d = 0.001;
  2746. }
  2747. extents[p_id] = d;
  2748. probe->set_extents(extents);
  2749. } else {
  2750. p_id -= 3;
  2751. Vector3 origin = probe->get_origin_offset();
  2752. origin[p_id] = 0;
  2753. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  2754. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  2755. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };
  2756. Vector3 axis;
  2757. axis[p_id] = 1.0;
  2758. Vector3 ra, rb;
  2759. Geometry3D::get_closest_points_between_segments(origin - axis * 16384, origin + axis * 16384, sg[0], sg[1], ra, rb);
  2760. // Adjust the actual position to account for the gizmo handle position
  2761. float d = ra[p_id] + 0.25;
  2762. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  2763. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  2764. }
  2765. origin[p_id] = d;
  2766. probe->set_origin_offset(origin);
  2767. }
  2768. }
  2769. void ReflectionProbeGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  2770. ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_spatial_node());
  2771. AABB restore = p_restore;
  2772. if (p_cancel) {
  2773. probe->set_extents(restore.position);
  2774. probe->set_origin_offset(restore.size);
  2775. return;
  2776. }
  2777. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  2778. ur->create_action(TTR("Change Probe Extents"));
  2779. ur->add_do_method(probe, "set_extents", probe->get_extents());
  2780. ur->add_do_method(probe, "set_origin_offset", probe->get_origin_offset());
  2781. ur->add_undo_method(probe, "set_extents", restore.position);
  2782. ur->add_undo_method(probe, "set_origin_offset", restore.size);
  2783. ur->commit_action();
  2784. }
  2785. void ReflectionProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2786. ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_spatial_node());
  2787. p_gizmo->clear();
  2788. Vector<Vector3> lines;
  2789. Vector<Vector3> internal_lines;
  2790. Vector3 extents = probe->get_extents();
  2791. AABB aabb;
  2792. aabb.position = -extents;
  2793. aabb.size = extents * 2;
  2794. for (int i = 0; i < 12; i++) {
  2795. Vector3 a, b;
  2796. aabb.get_edge(i, a, b);
  2797. lines.push_back(a);
  2798. lines.push_back(b);
  2799. }
  2800. for (int i = 0; i < 8; i++) {
  2801. Vector3 ep = aabb.get_endpoint(i);
  2802. internal_lines.push_back(probe->get_origin_offset());
  2803. internal_lines.push_back(ep);
  2804. }
  2805. Vector<Vector3> handles;
  2806. for (int i = 0; i < 3; i++) {
  2807. Vector3 ax;
  2808. ax[i] = aabb.position[i] + aabb.size[i];
  2809. handles.push_back(ax);
  2810. }
  2811. for (int i = 0; i < 3; i++) {
  2812. Vector3 orig_handle = probe->get_origin_offset();
  2813. orig_handle[i] -= 0.25;
  2814. lines.push_back(orig_handle);
  2815. handles.push_back(orig_handle);
  2816. orig_handle[i] += 0.5;
  2817. lines.push_back(orig_handle);
  2818. }
  2819. Ref<Material> material = get_material("reflection_probe_material", p_gizmo);
  2820. Ref<Material> material_internal = get_material("reflection_internal_material", p_gizmo);
  2821. Ref<Material> icon = get_material("reflection_probe_icon", p_gizmo);
  2822. p_gizmo->add_lines(lines, material);
  2823. p_gizmo->add_lines(internal_lines, material_internal);
  2824. if (p_gizmo->is_selected()) {
  2825. Ref<Material> solid_material = get_material("reflection_probe_solid_material", p_gizmo);
  2826. p_gizmo->add_solid_box(solid_material, probe->get_extents() * 2.0);
  2827. }
  2828. p_gizmo->add_unscaled_billboard(icon, 0.05);
  2829. p_gizmo->add_handles(handles, get_material("handles"));
  2830. }
  2831. ///////////////////////////////
  2832. ////
  2833. DecalGizmoPlugin::DecalGizmoPlugin() {
  2834. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/decal", Color(0.6, 0.5, 1.0));
  2835. create_material("decal_material", gizmo_color);
  2836. create_handle_material("handles");
  2837. }
  2838. bool DecalGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2839. return Object::cast_to<Decal>(p_spatial) != nullptr;
  2840. }
  2841. String DecalGizmoPlugin::get_gizmo_name() const {
  2842. return "Decal";
  2843. }
  2844. int DecalGizmoPlugin::get_priority() const {
  2845. return -1;
  2846. }
  2847. String DecalGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2848. switch (p_id) {
  2849. case 0:
  2850. return "Extents X";
  2851. case 1:
  2852. return "Extents Y";
  2853. case 2:
  2854. return "Extents Z";
  2855. }
  2856. return "";
  2857. }
  2858. Variant DecalGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2859. Decal *decal = Object::cast_to<Decal>(p_gizmo->get_spatial_node());
  2860. return decal->get_extents();
  2861. }
  2862. void DecalGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  2863. Decal *decal = Object::cast_to<Decal>(p_gizmo->get_spatial_node());
  2864. Transform3D gt = decal->get_global_transform();
  2865. Transform3D gi = gt.affine_inverse();
  2866. Vector3 extents = decal->get_extents();
  2867. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  2868. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  2869. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };
  2870. Vector3 axis;
  2871. axis[p_id] = 1.0;
  2872. Vector3 ra, rb;
  2873. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
  2874. float d = ra[p_id];
  2875. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  2876. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  2877. }
  2878. if (d < 0.001) {
  2879. d = 0.001;
  2880. }
  2881. extents[p_id] = d;
  2882. decal->set_extents(extents);
  2883. }
  2884. void DecalGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  2885. Decal *decal = Object::cast_to<Decal>(p_gizmo->get_spatial_node());
  2886. Vector3 restore = p_restore;
  2887. if (p_cancel) {
  2888. decal->set_extents(restore);
  2889. return;
  2890. }
  2891. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  2892. ur->create_action(TTR("Change Decal Extents"));
  2893. ur->add_do_method(decal, "set_extents", decal->get_extents());
  2894. ur->add_undo_method(decal, "set_extents", restore);
  2895. ur->commit_action();
  2896. }
  2897. void DecalGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  2898. Decal *decal = Object::cast_to<Decal>(p_gizmo->get_spatial_node());
  2899. p_gizmo->clear();
  2900. Vector<Vector3> lines;
  2901. Vector3 extents = decal->get_extents();
  2902. AABB aabb;
  2903. aabb.position = -extents;
  2904. aabb.size = extents * 2;
  2905. for (int i = 0; i < 12; i++) {
  2906. Vector3 a, b;
  2907. aabb.get_edge(i, a, b);
  2908. if (a.y == b.y) {
  2909. lines.push_back(a);
  2910. lines.push_back(b);
  2911. } else {
  2912. Vector3 ah = a.lerp(b, 0.2);
  2913. lines.push_back(a);
  2914. lines.push_back(ah);
  2915. Vector3 bh = b.lerp(a, 0.2);
  2916. lines.push_back(b);
  2917. lines.push_back(bh);
  2918. }
  2919. }
  2920. lines.push_back(Vector3(0, extents.y, 0));
  2921. lines.push_back(Vector3(0, extents.y * 1.2, 0));
  2922. Vector<Vector3> handles;
  2923. for (int i = 0; i < 3; i++) {
  2924. Vector3 ax;
  2925. ax[i] = aabb.position[i] + aabb.size[i];
  2926. handles.push_back(ax);
  2927. }
  2928. Ref<Material> material = get_material("decal_material", p_gizmo);
  2929. p_gizmo->add_lines(lines, material);
  2930. p_gizmo->add_handles(handles, get_material("handles"));
  2931. }
  2932. ///////////////////////////////
  2933. VoxelGIGizmoPlugin::VoxelGIGizmoPlugin() {
  2934. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/voxel_gi", Color(0.5, 1, 0.6));
  2935. create_material("voxel_gi_material", gizmo_color);
  2936. // This gizmo draws a lot of lines. Use a low opacity to make it not too intrusive.
  2937. gizmo_color.a = 0.1;
  2938. create_material("voxel_gi_internal_material", gizmo_color);
  2939. gizmo_color.a = 0.05;
  2940. create_material("voxel_gi_solid_material", gizmo_color);
  2941. create_icon_material("voxel_gi_icon", Node3DEditor::get_singleton()->get_theme_icon(SNAME("GizmoVoxelGI"), SNAME("EditorIcons")));
  2942. create_handle_material("handles");
  2943. }
  2944. bool VoxelGIGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  2945. return Object::cast_to<VoxelGI>(p_spatial) != nullptr;
  2946. }
  2947. String VoxelGIGizmoPlugin::get_gizmo_name() const {
  2948. return "VoxelGI";
  2949. }
  2950. int VoxelGIGizmoPlugin::get_priority() const {
  2951. return -1;
  2952. }
  2953. String VoxelGIGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2954. switch (p_id) {
  2955. case 0:
  2956. return "Extents X";
  2957. case 1:
  2958. return "Extents Y";
  2959. case 2:
  2960. return "Extents Z";
  2961. }
  2962. return "";
  2963. }
  2964. Variant VoxelGIGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  2965. VoxelGI *probe = Object::cast_to<VoxelGI>(p_gizmo->get_spatial_node());
  2966. return probe->get_extents();
  2967. }
  2968. void VoxelGIGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  2969. VoxelGI *probe = Object::cast_to<VoxelGI>(p_gizmo->get_spatial_node());
  2970. Transform3D gt = probe->get_global_transform();
  2971. Transform3D gi = gt.affine_inverse();
  2972. Vector3 extents = probe->get_extents();
  2973. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  2974. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  2975. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };
  2976. Vector3 axis;
  2977. axis[p_id] = 1.0;
  2978. Vector3 ra, rb;
  2979. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
  2980. float d = ra[p_id];
  2981. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  2982. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  2983. }
  2984. if (d < 0.001) {
  2985. d = 0.001;
  2986. }
  2987. extents[p_id] = d;
  2988. probe->set_extents(extents);
  2989. }
  2990. void VoxelGIGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  2991. VoxelGI *probe = Object::cast_to<VoxelGI>(p_gizmo->get_spatial_node());
  2992. Vector3 restore = p_restore;
  2993. if (p_cancel) {
  2994. probe->set_extents(restore);
  2995. return;
  2996. }
  2997. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  2998. ur->create_action(TTR("Change Probe Extents"));
  2999. ur->add_do_method(probe, "set_extents", probe->get_extents());
  3000. ur->add_undo_method(probe, "set_extents", restore);
  3001. ur->commit_action();
  3002. }
  3003. void VoxelGIGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  3004. VoxelGI *probe = Object::cast_to<VoxelGI>(p_gizmo->get_spatial_node());
  3005. Ref<Material> material = get_material("voxel_gi_material", p_gizmo);
  3006. Ref<Material> icon = get_material("voxel_gi_icon", p_gizmo);
  3007. Ref<Material> material_internal = get_material("voxel_gi_internal_material", p_gizmo);
  3008. p_gizmo->clear();
  3009. Vector<Vector3> lines;
  3010. Vector3 extents = probe->get_extents();
  3011. static const int subdivs[VoxelGI::SUBDIV_MAX] = { 64, 128, 256, 512 };
  3012. AABB aabb = AABB(-extents, extents * 2);
  3013. int subdiv = subdivs[probe->get_subdiv()];
  3014. float cell_size = aabb.get_longest_axis_size() / subdiv;
  3015. for (int i = 0; i < 12; i++) {
  3016. Vector3 a, b;
  3017. aabb.get_edge(i, a, b);
  3018. lines.push_back(a);
  3019. lines.push_back(b);
  3020. }
  3021. p_gizmo->add_lines(lines, material);
  3022. lines.clear();
  3023. for (int i = 1; i < subdiv; i++) {
  3024. for (int j = 0; j < 3; j++) {
  3025. if (cell_size * i > aabb.size[j]) {
  3026. continue;
  3027. }
  3028. int j_n1 = (j + 1) % 3;
  3029. int j_n2 = (j + 2) % 3;
  3030. for (int k = 0; k < 4; k++) {
  3031. Vector3 from = aabb.position, to = aabb.position;
  3032. from[j] += cell_size * i;
  3033. to[j] += cell_size * i;
  3034. if (k & 1) {
  3035. to[j_n1] += aabb.size[j_n1];
  3036. } else {
  3037. to[j_n2] += aabb.size[j_n2];
  3038. }
  3039. if (k & 2) {
  3040. from[j_n1] += aabb.size[j_n1];
  3041. from[j_n2] += aabb.size[j_n2];
  3042. }
  3043. lines.push_back(from);
  3044. lines.push_back(to);
  3045. }
  3046. }
  3047. }
  3048. p_gizmo->add_lines(lines, material_internal);
  3049. Vector<Vector3> handles;
  3050. for (int i = 0; i < 3; i++) {
  3051. Vector3 ax;
  3052. ax[i] = aabb.position[i] + aabb.size[i];
  3053. handles.push_back(ax);
  3054. }
  3055. if (p_gizmo->is_selected()) {
  3056. Ref<Material> solid_material = get_material("voxel_gi_solid_material", p_gizmo);
  3057. p_gizmo->add_solid_box(solid_material, aabb.get_size());
  3058. }
  3059. p_gizmo->add_unscaled_billboard(icon, 0.05);
  3060. p_gizmo->add_handles(handles, get_material("handles"));
  3061. }
  3062. ////
  3063. LightmapGIGizmoPlugin::LightmapGIGizmoPlugin() {
  3064. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/lightmap_lines", Color(0.5, 0.6, 1));
  3065. gizmo_color.a = 0.1;
  3066. create_material("lightmap_lines", gizmo_color);
  3067. Ref<StandardMaterial3D> mat = memnew(StandardMaterial3D);
  3068. mat->set_shading_mode(StandardMaterial3D::SHADING_MODE_UNSHADED);
  3069. mat->set_cull_mode(StandardMaterial3D::CULL_DISABLED);
  3070. mat->set_flag(StandardMaterial3D::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  3071. mat->set_flag(StandardMaterial3D::FLAG_SRGB_VERTEX_COLOR, false);
  3072. add_material("lightmap_probe_material", mat);
  3073. create_icon_material("baked_indirect_light_icon", Node3DEditor::get_singleton()->get_theme_icon(SNAME("GizmoLightmapGI"), SNAME("EditorIcons")));
  3074. }
  3075. bool LightmapGIGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  3076. return Object::cast_to<LightmapGI>(p_spatial) != nullptr;
  3077. }
  3078. String LightmapGIGizmoPlugin::get_gizmo_name() const {
  3079. return "LightmapGI";
  3080. }
  3081. int LightmapGIGizmoPlugin::get_priority() const {
  3082. return -1;
  3083. }
  3084. void LightmapGIGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  3085. Ref<Material> icon = get_material("baked_indirect_light_icon", p_gizmo);
  3086. LightmapGI *baker = Object::cast_to<LightmapGI>(p_gizmo->get_spatial_node());
  3087. Ref<LightmapGIData> data = baker->get_light_data();
  3088. p_gizmo->add_unscaled_billboard(icon, 0.05);
  3089. if (data.is_null()) {
  3090. return;
  3091. }
  3092. Ref<Material> material_lines = get_material("lightmap_lines", p_gizmo);
  3093. Ref<Material> material_probes = get_material("lightmap_probe_material", p_gizmo);
  3094. p_gizmo->clear();
  3095. Vector<Vector3> lines;
  3096. HashSet<Vector2i> lines_found;
  3097. Vector<Vector3> points = data->get_capture_points();
  3098. if (points.size() == 0) {
  3099. return;
  3100. }
  3101. Vector<Color> sh = data->get_capture_sh();
  3102. if (sh.size() != points.size() * 9) {
  3103. return;
  3104. }
  3105. Vector<int> tetrahedrons = data->get_capture_tetrahedra();
  3106. for (int i = 0; i < tetrahedrons.size(); i += 4) {
  3107. for (int j = 0; j < 4; j++) {
  3108. for (int k = j + 1; k < 4; k++) {
  3109. Vector2i pair;
  3110. pair.x = tetrahedrons[i + j];
  3111. pair.y = tetrahedrons[i + k];
  3112. if (pair.y < pair.x) {
  3113. SWAP(pair.x, pair.y);
  3114. }
  3115. if (lines_found.has(pair)) {
  3116. continue;
  3117. }
  3118. lines_found.insert(pair);
  3119. lines.push_back(points[pair.x]);
  3120. lines.push_back(points[pair.y]);
  3121. }
  3122. }
  3123. }
  3124. p_gizmo->add_lines(lines, material_lines);
  3125. int stack_count = 8;
  3126. int sector_count = 16;
  3127. float sector_step = (Math_PI * 2.0) / sector_count;
  3128. float stack_step = Math_PI / stack_count;
  3129. Vector<Vector3> vertices;
  3130. Vector<Color> colors;
  3131. Vector<int> indices;
  3132. float radius = 0.3;
  3133. for (int p = 0; p < points.size(); p++) {
  3134. int vertex_base = vertices.size();
  3135. Vector3 sh_col[9];
  3136. for (int i = 0; i < 9; i++) {
  3137. sh_col[i].x = sh[p * 9 + i].r;
  3138. sh_col[i].y = sh[p * 9 + i].g;
  3139. sh_col[i].z = sh[p * 9 + i].b;
  3140. }
  3141. for (int i = 0; i <= stack_count; ++i) {
  3142. float stack_angle = Math_PI / 2 - i * stack_step; // starting from pi/2 to -pi/2
  3143. float xy = radius * Math::cos(stack_angle); // r * cos(u)
  3144. float z = radius * Math::sin(stack_angle); // r * sin(u)
  3145. // add (sector_count+1) vertices per stack
  3146. // the first and last vertices have same position and normal, but different tex coords
  3147. for (int j = 0; j <= sector_count; ++j) {
  3148. float sector_angle = j * sector_step; // starting from 0 to 2pi
  3149. // vertex position (x, y, z)
  3150. float x = xy * Math::cos(sector_angle); // r * cos(u) * cos(v)
  3151. float y = xy * Math::sin(sector_angle); // r * cos(u) * sin(v)
  3152. Vector3 n = Vector3(x, z, y);
  3153. vertices.push_back(points[p] + n);
  3154. n.normalize();
  3155. const float c1 = 0.429043;
  3156. const float c2 = 0.511664;
  3157. const float c3 = 0.743125;
  3158. const float c4 = 0.886227;
  3159. const float c5 = 0.247708;
  3160. Vector3 light = (c1 * sh_col[8] * (n.x * n.x - n.y * n.y) +
  3161. c3 * sh_col[6] * n.z * n.z +
  3162. c4 * sh_col[0] -
  3163. c5 * sh_col[6] +
  3164. 2.0 * c1 * sh_col[4] * n.x * n.y +
  3165. 2.0 * c1 * sh_col[7] * n.x * n.z +
  3166. 2.0 * c1 * sh_col[5] * n.y * n.z +
  3167. 2.0 * c2 * sh_col[3] * n.x +
  3168. 2.0 * c2 * sh_col[1] * n.y +
  3169. 2.0 * c2 * sh_col[2] * n.z);
  3170. colors.push_back(Color(light.x, light.y, light.z, 1));
  3171. }
  3172. }
  3173. for (int i = 0; i < stack_count; ++i) {
  3174. int k1 = i * (sector_count + 1); // beginning of current stack
  3175. int k2 = k1 + sector_count + 1; // beginning of next stack
  3176. for (int j = 0; j < sector_count; ++j, ++k1, ++k2) {
  3177. // 2 triangles per sector excluding first and last stacks
  3178. // k1 => k2 => k1+1
  3179. if (i != 0) {
  3180. indices.push_back(vertex_base + k1);
  3181. indices.push_back(vertex_base + k2);
  3182. indices.push_back(vertex_base + k1 + 1);
  3183. }
  3184. // k1+1 => k2 => k2+1
  3185. if (i != (stack_count - 1)) {
  3186. indices.push_back(vertex_base + k1 + 1);
  3187. indices.push_back(vertex_base + k2);
  3188. indices.push_back(vertex_base + k2 + 1);
  3189. }
  3190. }
  3191. }
  3192. }
  3193. Array array;
  3194. array.resize(RS::ARRAY_MAX);
  3195. array[RS::ARRAY_VERTEX] = vertices;
  3196. array[RS::ARRAY_INDEX] = indices;
  3197. array[RS::ARRAY_COLOR] = colors;
  3198. Ref<ArrayMesh> mesh;
  3199. mesh.instantiate();
  3200. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, array, Array(), Dictionary(), 0); //no compression
  3201. mesh->surface_set_material(0, material_probes);
  3202. p_gizmo->add_mesh(mesh);
  3203. }
  3204. /////////
  3205. LightmapProbeGizmoPlugin::LightmapProbeGizmoPlugin() {
  3206. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/lightprobe_lines", Color(0.5, 0.6, 1));
  3207. gizmo_color.a = 0.3;
  3208. create_material("lightprobe_lines", gizmo_color);
  3209. }
  3210. bool LightmapProbeGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  3211. return Object::cast_to<LightmapProbe>(p_spatial) != nullptr;
  3212. }
  3213. String LightmapProbeGizmoPlugin::get_gizmo_name() const {
  3214. return "LightmapProbe";
  3215. }
  3216. int LightmapProbeGizmoPlugin::get_priority() const {
  3217. return -1;
  3218. }
  3219. void LightmapProbeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  3220. Ref<Material> material_lines = get_material("lightprobe_lines", p_gizmo);
  3221. p_gizmo->clear();
  3222. Vector<Vector3> lines;
  3223. int stack_count = 8;
  3224. int sector_count = 16;
  3225. float sector_step = (Math_PI * 2.0) / sector_count;
  3226. float stack_step = Math_PI / stack_count;
  3227. Vector<Vector3> vertices;
  3228. float radius = 0.2;
  3229. for (int i = 0; i <= stack_count; ++i) {
  3230. float stack_angle = Math_PI / 2 - i * stack_step; // starting from pi/2 to -pi/2
  3231. float xy = radius * Math::cos(stack_angle); // r * cos(u)
  3232. float z = radius * Math::sin(stack_angle); // r * sin(u)
  3233. // add (sector_count+1) vertices per stack
  3234. // the first and last vertices have same position and normal, but different tex coords
  3235. for (int j = 0; j <= sector_count; ++j) {
  3236. float sector_angle = j * sector_step; // starting from 0 to 2pi
  3237. // vertex position (x, y, z)
  3238. float x = xy * Math::cos(sector_angle); // r * cos(u) * cos(v)
  3239. float y = xy * Math::sin(sector_angle); // r * cos(u) * sin(v)
  3240. Vector3 n = Vector3(x, z, y);
  3241. vertices.push_back(n);
  3242. }
  3243. }
  3244. for (int i = 0; i < stack_count; ++i) {
  3245. int k1 = i * (sector_count + 1); // beginning of current stack
  3246. int k2 = k1 + sector_count + 1; // beginning of next stack
  3247. for (int j = 0; j < sector_count; ++j, ++k1, ++k2) {
  3248. // 2 triangles per sector excluding first and last stacks
  3249. // k1 => k2 => k1+1
  3250. if (i != 0) {
  3251. lines.push_back(vertices[k1]);
  3252. lines.push_back(vertices[k2]);
  3253. lines.push_back(vertices[k1]);
  3254. lines.push_back(vertices[k1 + 1]);
  3255. }
  3256. if (i != (stack_count - 1)) {
  3257. lines.push_back(vertices[k1 + 1]);
  3258. lines.push_back(vertices[k2]);
  3259. lines.push_back(vertices[k2]);
  3260. lines.push_back(vertices[k2 + 1]);
  3261. }
  3262. }
  3263. }
  3264. p_gizmo->add_lines(lines, material_lines);
  3265. }
  3266. ////
  3267. CollisionObject3DGizmoPlugin::CollisionObject3DGizmoPlugin() {
  3268. const Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
  3269. create_material("shape_material", gizmo_color);
  3270. const float gizmo_value = gizmo_color.get_v();
  3271. const Color gizmo_color_disabled = Color(gizmo_value, gizmo_value, gizmo_value, 0.65);
  3272. create_material("shape_material_disabled", gizmo_color_disabled);
  3273. }
  3274. bool CollisionObject3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  3275. return Object::cast_to<CollisionObject3D>(p_spatial) != nullptr;
  3276. }
  3277. String CollisionObject3DGizmoPlugin::get_gizmo_name() const {
  3278. return "CollisionObject3D";
  3279. }
  3280. int CollisionObject3DGizmoPlugin::get_priority() const {
  3281. return -2;
  3282. }
  3283. void CollisionObject3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  3284. CollisionObject3D *co = Object::cast_to<CollisionObject3D>(p_gizmo->get_spatial_node());
  3285. p_gizmo->clear();
  3286. List<uint32_t> owners;
  3287. co->get_shape_owners(&owners);
  3288. for (uint32_t &owner_id : owners) {
  3289. Transform3D xform = co->shape_owner_get_transform(owner_id);
  3290. Object *owner = co->shape_owner_get_owner(owner_id);
  3291. // Exclude CollisionShape3D and CollisionPolygon3D as they have their gizmo.
  3292. if (!Object::cast_to<CollisionShape3D>(owner) && !Object::cast_to<CollisionPolygon3D>(owner)) {
  3293. Ref<Material> material = get_material(!co->is_shape_owner_disabled(owner_id) ? "shape_material" : "shape_material_disabled", p_gizmo);
  3294. for (int shape_id = 0; shape_id < co->shape_owner_get_shape_count(owner_id); shape_id++) {
  3295. Ref<Shape3D> s = co->shape_owner_get_shape(owner_id, shape_id);
  3296. if (s.is_null()) {
  3297. continue;
  3298. }
  3299. SurfaceTool st;
  3300. st.append_from(s->get_debug_mesh(), 0, xform);
  3301. p_gizmo->add_mesh(st.commit(), material);
  3302. p_gizmo->add_collision_segments(s->get_debug_mesh_lines());
  3303. }
  3304. }
  3305. }
  3306. }
  3307. ////
  3308. CollisionShape3DGizmoPlugin::CollisionShape3DGizmoPlugin() {
  3309. const Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
  3310. create_material("shape_material", gizmo_color);
  3311. const float gizmo_value = gizmo_color.get_v();
  3312. const Color gizmo_color_disabled = Color(gizmo_value, gizmo_value, gizmo_value, 0.65);
  3313. create_material("shape_material_disabled", gizmo_color_disabled);
  3314. create_handle_material("handles");
  3315. }
  3316. bool CollisionShape3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  3317. return Object::cast_to<CollisionShape3D>(p_spatial) != nullptr;
  3318. }
  3319. String CollisionShape3DGizmoPlugin::get_gizmo_name() const {
  3320. return "CollisionShape3D";
  3321. }
  3322. int CollisionShape3DGizmoPlugin::get_priority() const {
  3323. return -1;
  3324. }
  3325. String CollisionShape3DGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  3326. const CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(p_gizmo->get_spatial_node());
  3327. Ref<Shape3D> s = cs->get_shape();
  3328. if (s.is_null()) {
  3329. return "";
  3330. }
  3331. if (Object::cast_to<SphereShape3D>(*s)) {
  3332. return "Radius";
  3333. }
  3334. if (Object::cast_to<BoxShape3D>(*s)) {
  3335. return "Size";
  3336. }
  3337. if (Object::cast_to<CapsuleShape3D>(*s)) {
  3338. return p_id == 0 ? "Radius" : "Height";
  3339. }
  3340. if (Object::cast_to<CylinderShape3D>(*s)) {
  3341. return p_id == 0 ? "Radius" : "Height";
  3342. }
  3343. if (Object::cast_to<SeparationRayShape3D>(*s)) {
  3344. return "Length";
  3345. }
  3346. return "";
  3347. }
  3348. Variant CollisionShape3DGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  3349. CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(p_gizmo->get_spatial_node());
  3350. Ref<Shape3D> s = cs->get_shape();
  3351. if (s.is_null()) {
  3352. return Variant();
  3353. }
  3354. if (Object::cast_to<SphereShape3D>(*s)) {
  3355. Ref<SphereShape3D> ss = s;
  3356. return ss->get_radius();
  3357. }
  3358. if (Object::cast_to<BoxShape3D>(*s)) {
  3359. Ref<BoxShape3D> bs = s;
  3360. return bs->get_size();
  3361. }
  3362. if (Object::cast_to<CapsuleShape3D>(*s)) {
  3363. Ref<CapsuleShape3D> cs2 = s;
  3364. return Vector2(cs2->get_radius(), cs2->get_height());
  3365. }
  3366. if (Object::cast_to<CylinderShape3D>(*s)) {
  3367. Ref<CylinderShape3D> cs2 = s;
  3368. return p_id == 0 ? cs2->get_radius() : cs2->get_height();
  3369. }
  3370. if (Object::cast_to<SeparationRayShape3D>(*s)) {
  3371. Ref<SeparationRayShape3D> cs2 = s;
  3372. return cs2->get_length();
  3373. }
  3374. return Variant();
  3375. }
  3376. void CollisionShape3DGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  3377. CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(p_gizmo->get_spatial_node());
  3378. Ref<Shape3D> s = cs->get_shape();
  3379. if (s.is_null()) {
  3380. return;
  3381. }
  3382. Transform3D gt = cs->get_global_transform();
  3383. Transform3D gi = gt.affine_inverse();
  3384. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  3385. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  3386. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  3387. if (Object::cast_to<SphereShape3D>(*s)) {
  3388. Ref<SphereShape3D> ss = s;
  3389. Vector3 ra, rb;
  3390. Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
  3391. float d = ra.x;
  3392. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  3393. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  3394. }
  3395. if (d < 0.001) {
  3396. d = 0.001;
  3397. }
  3398. ss->set_radius(d);
  3399. }
  3400. if (Object::cast_to<SeparationRayShape3D>(*s)) {
  3401. Ref<SeparationRayShape3D> rs = s;
  3402. Vector3 ra, rb;
  3403. Geometry3D::get_closest_points_between_segments(Vector3(), Vector3(0, 0, 4096), sg[0], sg[1], ra, rb);
  3404. float d = ra.z;
  3405. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  3406. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  3407. }
  3408. if (d < 0.001) {
  3409. d = 0.001;
  3410. }
  3411. rs->set_length(d);
  3412. }
  3413. if (Object::cast_to<BoxShape3D>(*s)) {
  3414. Vector3 axis;
  3415. axis[p_id] = 1.0;
  3416. Ref<BoxShape3D> bs = s;
  3417. Vector3 ra, rb;
  3418. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  3419. float d = ra[p_id];
  3420. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  3421. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  3422. }
  3423. if (d < 0.001) {
  3424. d = 0.001;
  3425. }
  3426. Vector3 he = bs->get_size();
  3427. he[p_id] = d * 2;
  3428. bs->set_size(he);
  3429. }
  3430. if (Object::cast_to<CapsuleShape3D>(*s)) {
  3431. Vector3 axis;
  3432. axis[p_id == 0 ? 0 : 1] = 1.0;
  3433. Ref<CapsuleShape3D> cs2 = s;
  3434. Vector3 ra, rb;
  3435. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  3436. float d = axis.dot(ra);
  3437. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  3438. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  3439. }
  3440. if (d < 0.001) {
  3441. d = 0.001;
  3442. }
  3443. if (p_id == 0) {
  3444. cs2->set_radius(d);
  3445. } else if (p_id == 1) {
  3446. cs2->set_height(d * 2.0);
  3447. }
  3448. }
  3449. if (Object::cast_to<CylinderShape3D>(*s)) {
  3450. Vector3 axis;
  3451. axis[p_id == 0 ? 0 : 1] = 1.0;
  3452. Ref<CylinderShape3D> cs2 = s;
  3453. Vector3 ra, rb;
  3454. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  3455. float d = axis.dot(ra);
  3456. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  3457. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  3458. }
  3459. if (d < 0.001) {
  3460. d = 0.001;
  3461. }
  3462. if (p_id == 0) {
  3463. cs2->set_radius(d);
  3464. } else if (p_id == 1) {
  3465. cs2->set_height(d * 2.0);
  3466. }
  3467. }
  3468. }
  3469. void CollisionShape3DGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  3470. CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(p_gizmo->get_spatial_node());
  3471. Ref<Shape3D> s = cs->get_shape();
  3472. if (s.is_null()) {
  3473. return;
  3474. }
  3475. if (Object::cast_to<SphereShape3D>(*s)) {
  3476. Ref<SphereShape3D> ss = s;
  3477. if (p_cancel) {
  3478. ss->set_radius(p_restore);
  3479. return;
  3480. }
  3481. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  3482. ur->create_action(TTR("Change Sphere Shape Radius"));
  3483. ur->add_do_method(ss.ptr(), "set_radius", ss->get_radius());
  3484. ur->add_undo_method(ss.ptr(), "set_radius", p_restore);
  3485. ur->commit_action();
  3486. }
  3487. if (Object::cast_to<BoxShape3D>(*s)) {
  3488. Ref<BoxShape3D> ss = s;
  3489. if (p_cancel) {
  3490. ss->set_size(p_restore);
  3491. return;
  3492. }
  3493. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  3494. ur->create_action(TTR("Change Box Shape Size"));
  3495. ur->add_do_method(ss.ptr(), "set_size", ss->get_size());
  3496. ur->add_undo_method(ss.ptr(), "set_size", p_restore);
  3497. ur->commit_action();
  3498. }
  3499. if (Object::cast_to<CapsuleShape3D>(*s)) {
  3500. Ref<CapsuleShape3D> ss = s;
  3501. Vector2 values = p_restore;
  3502. if (p_cancel) {
  3503. ss->set_radius(values[0]);
  3504. ss->set_height(values[1]);
  3505. return;
  3506. }
  3507. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  3508. if (p_id == 0) {
  3509. ur->create_action(TTR("Change Capsule Shape Radius"));
  3510. ur->add_do_method(ss.ptr(), "set_radius", ss->get_radius());
  3511. } else {
  3512. ur->create_action(TTR("Change Capsule Shape Height"));
  3513. ur->add_do_method(ss.ptr(), "set_height", ss->get_height());
  3514. }
  3515. ur->add_undo_method(ss.ptr(), "set_radius", values[0]);
  3516. ur->add_undo_method(ss.ptr(), "set_height", values[1]);
  3517. ur->commit_action();
  3518. }
  3519. if (Object::cast_to<CylinderShape3D>(*s)) {
  3520. Ref<CylinderShape3D> ss = s;
  3521. if (p_cancel) {
  3522. if (p_id == 0) {
  3523. ss->set_radius(p_restore);
  3524. } else {
  3525. ss->set_height(p_restore);
  3526. }
  3527. return;
  3528. }
  3529. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  3530. if (p_id == 0) {
  3531. ur->create_action(TTR("Change Cylinder Shape Radius"));
  3532. ur->add_do_method(ss.ptr(), "set_radius", ss->get_radius());
  3533. ur->add_undo_method(ss.ptr(), "set_radius", p_restore);
  3534. } else {
  3535. ur->create_action(
  3536. ///
  3537. ////////
  3538. TTR("Change Cylinder Shape Height"));
  3539. ur->add_do_method(ss.ptr(), "set_height", ss->get_height());
  3540. ur->add_undo_method(ss.ptr(), "set_height", p_restore);
  3541. }
  3542. ur->commit_action();
  3543. }
  3544. if (Object::cast_to<SeparationRayShape3D>(*s)) {
  3545. Ref<SeparationRayShape3D> ss = s;
  3546. if (p_cancel) {
  3547. ss->set_length(p_restore);
  3548. return;
  3549. }
  3550. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  3551. ur->create_action(TTR("Change Separation Ray Shape Length"));
  3552. ur->add_do_method(ss.ptr(), "set_length", ss->get_length());
  3553. ur->add_undo_method(ss.ptr(), "set_length", p_restore);
  3554. ur->commit_action();
  3555. }
  3556. }
  3557. void CollisionShape3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  3558. CollisionShape3D *cs = Object::cast_to<CollisionShape3D>(p_gizmo->get_spatial_node());
  3559. p_gizmo->clear();
  3560. Ref<Shape3D> s = cs->get_shape();
  3561. if (s.is_null()) {
  3562. return;
  3563. }
  3564. const Ref<Material> material =
  3565. get_material(!cs->is_disabled() ? "shape_material" : "shape_material_disabled", p_gizmo);
  3566. Ref<Material> handles_material = get_material("handles");
  3567. if (Object::cast_to<SphereShape3D>(*s)) {
  3568. Ref<SphereShape3D> sp = s;
  3569. float r = sp->get_radius();
  3570. Vector<Vector3> points;
  3571. for (int i = 0; i <= 360; i++) {
  3572. float ra = Math::deg_to_rad((float)i);
  3573. float rb = Math::deg_to_rad((float)i + 1);
  3574. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  3575. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  3576. points.push_back(Vector3(a.x, 0, a.y));
  3577. points.push_back(Vector3(b.x, 0, b.y));
  3578. points.push_back(Vector3(0, a.x, a.y));
  3579. points.push_back(Vector3(0, b.x, b.y));
  3580. points.push_back(Vector3(a.x, a.y, 0));
  3581. points.push_back(Vector3(b.x, b.y, 0));
  3582. }
  3583. Vector<Vector3> collision_segments;
  3584. for (int i = 0; i < 64; i++) {
  3585. float ra = i * (Math_TAU / 64.0);
  3586. float rb = (i + 1) * (Math_TAU / 64.0);
  3587. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  3588. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  3589. collision_segments.push_back(Vector3(a.x, 0, a.y));
  3590. collision_segments.push_back(Vector3(b.x, 0, b.y));
  3591. collision_segments.push_back(Vector3(0, a.x, a.y));
  3592. collision_segments.push_back(Vector3(0, b.x, b.y));
  3593. collision_segments.push_back(Vector3(a.x, a.y, 0));
  3594. collision_segments.push_back(Vector3(b.x, b.y, 0));
  3595. }
  3596. p_gizmo->add_lines(points, material);
  3597. p_gizmo->add_collision_segments(collision_segments);
  3598. Vector<Vector3> handles;
  3599. handles.push_back(Vector3(r, 0, 0));
  3600. p_gizmo->add_handles(handles, handles_material);
  3601. }
  3602. if (Object::cast_to<BoxShape3D>(*s)) {
  3603. Ref<BoxShape3D> bs = s;
  3604. Vector<Vector3> lines;
  3605. AABB aabb;
  3606. aabb.position = -bs->get_size() / 2;
  3607. aabb.size = bs->get_size();
  3608. for (int i = 0; i < 12; i++) {
  3609. Vector3 a, b;
  3610. aabb.get_edge(i, a, b);
  3611. lines.push_back(a);
  3612. lines.push_back(b);
  3613. }
  3614. Vector<Vector3> handles;
  3615. for (int i = 0; i < 3; i++) {
  3616. Vector3 ax;
  3617. ax[i] = bs->get_size()[i] / 2;
  3618. handles.push_back(ax);
  3619. }
  3620. p_gizmo->add_lines(lines, material);
  3621. p_gizmo->add_collision_segments(lines);
  3622. p_gizmo->add_handles(handles, handles_material);
  3623. }
  3624. if (Object::cast_to<CapsuleShape3D>(*s)) {
  3625. Ref<CapsuleShape3D> cs2 = s;
  3626. float radius = cs2->get_radius();
  3627. float height = cs2->get_height();
  3628. Vector<Vector3> points;
  3629. Vector3 d(0, height * 0.5 - radius, 0);
  3630. for (int i = 0; i < 360; i++) {
  3631. float ra = Math::deg_to_rad((float)i);
  3632. float rb = Math::deg_to_rad((float)i + 1);
  3633. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
  3634. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;
  3635. points.push_back(Vector3(a.x, 0, a.y) + d);
  3636. points.push_back(Vector3(b.x, 0, b.y) + d);
  3637. points.push_back(Vector3(a.x, 0, a.y) - d);
  3638. points.push_back(Vector3(b.x, 0, b.y) - d);
  3639. if (i % 90 == 0) {
  3640. points.push_back(Vector3(a.x, 0, a.y) + d);
  3641. points.push_back(Vector3(a.x, 0, a.y) - d);
  3642. }
  3643. Vector3 dud = i < 180 ? d : -d;
  3644. points.push_back(Vector3(0, a.x, a.y) + dud);
  3645. points.push_back(Vector3(0, b.x, b.y) + dud);
  3646. points.push_back(Vector3(a.y, a.x, 0) + dud);
  3647. points.push_back(Vector3(b.y, b.x, 0) + dud);
  3648. }
  3649. p_gizmo->add_lines(points, material);
  3650. Vector<Vector3> collision_segments;
  3651. for (int i = 0; i < 64; i++) {
  3652. float ra = i * (Math_TAU / 64.0);
  3653. float rb = (i + 1) * (Math_TAU / 64.0);
  3654. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
  3655. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;
  3656. collision_segments.push_back(Vector3(a.x, 0, a.y) + d);
  3657. collision_segments.push_back(Vector3(b.x, 0, b.y) + d);
  3658. collision_segments.push_back(Vector3(a.x, 0, a.y) - d);
  3659. collision_segments.push_back(Vector3(b.x, 0, b.y) - d);
  3660. if (i % 16 == 0) {
  3661. collision_segments.push_back(Vector3(a.x, 0, a.y) + d);
  3662. collision_segments.push_back(Vector3(a.x, 0, a.y) - d);
  3663. }
  3664. Vector3 dud = i < 32 ? d : -d;
  3665. collision_segments.push_back(Vector3(0, a.x, a.y) + dud);
  3666. collision_segments.push_back(Vector3(0, b.x, b.y) + dud);
  3667. collision_segments.push_back(Vector3(a.y, a.x, 0) + dud);
  3668. collision_segments.push_back(Vector3(b.y, b.x, 0) + dud);
  3669. }
  3670. p_gizmo->add_collision_segments(collision_segments);
  3671. Vector<Vector3> handles = {
  3672. Vector3(cs2->get_radius(), 0, 0),
  3673. Vector3(0, cs2->get_height() * 0.5, 0)
  3674. };
  3675. p_gizmo->add_handles(handles, handles_material);
  3676. }
  3677. if (Object::cast_to<CylinderShape3D>(*s)) {
  3678. Ref<CylinderShape3D> cs2 = s;
  3679. float radius = cs2->get_radius();
  3680. float height = cs2->get_height();
  3681. Vector<Vector3> points;
  3682. Vector3 d(0, height * 0.5, 0);
  3683. for (int i = 0; i < 360; i++) {
  3684. float ra = Math::deg_to_rad((float)i);
  3685. float rb = Math::deg_to_rad((float)i + 1);
  3686. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
  3687. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;
  3688. points.push_back(Vector3(a.x, 0, a.y) + d);
  3689. points.push_back(Vector3(b.x, 0, b.y) + d);
  3690. points.push_back(Vector3(a.x, 0, a.y) - d);
  3691. points.push_back(Vector3(b.x, 0, b.y) - d);
  3692. if (i % 90 == 0) {
  3693. points.push_back(Vector3(a.x, 0, a.y) + d);
  3694. points.push_back(Vector3(a.x, 0, a.y) - d);
  3695. }
  3696. }
  3697. p_gizmo->add_lines(points, material);
  3698. Vector<Vector3> collision_segments;
  3699. for (int i = 0; i < 64; i++) {
  3700. float ra = i * (Math_TAU / 64.0);
  3701. float rb = (i + 1) * (Math_TAU / 64.0);
  3702. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
  3703. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;
  3704. collision_segments.push_back(Vector3(a.x, 0, a.y) + d);
  3705. collision_segments.push_back(Vector3(b.x, 0, b.y) + d);
  3706. collision_segments.push_back(Vector3(a.x, 0, a.y) - d);
  3707. collision_segments.push_back(Vector3(b.x, 0, b.y) - d);
  3708. if (i % 16 == 0) {
  3709. collision_segments.push_back(Vector3(a.x, 0, a.y) + d);
  3710. collision_segments.push_back(Vector3(a.x, 0, a.y) - d);
  3711. }
  3712. }
  3713. p_gizmo->add_collision_segments(collision_segments);
  3714. Vector<Vector3> handles = {
  3715. Vector3(cs2->get_radius(), 0, 0),
  3716. Vector3(0, cs2->get_height() * 0.5, 0)
  3717. };
  3718. p_gizmo->add_handles(handles, handles_material);
  3719. }
  3720. if (Object::cast_to<WorldBoundaryShape3D>(*s)) {
  3721. Ref<WorldBoundaryShape3D> wbs = s;
  3722. const Plane &p = wbs->get_plane();
  3723. Vector3 n1 = p.get_any_perpendicular_normal();
  3724. Vector3 n2 = p.normal.cross(n1).normalized();
  3725. Vector3 pface[4] = {
  3726. p.normal * p.d + n1 * 10.0 + n2 * 10.0,
  3727. p.normal * p.d + n1 * 10.0 + n2 * -10.0,
  3728. p.normal * p.d + n1 * -10.0 + n2 * -10.0,
  3729. p.normal * p.d + n1 * -10.0 + n2 * 10.0,
  3730. };
  3731. Vector<Vector3> points = {
  3732. pface[0],
  3733. pface[1],
  3734. pface[1],
  3735. pface[2],
  3736. pface[2],
  3737. pface[3],
  3738. pface[3],
  3739. pface[0],
  3740. p.normal * p.d,
  3741. p.normal * p.d + p.normal * 3
  3742. };
  3743. p_gizmo->add_lines(points, material);
  3744. p_gizmo->add_collision_segments(points);
  3745. }
  3746. if (Object::cast_to<ConvexPolygonShape3D>(*s)) {
  3747. Vector<Vector3> points = Object::cast_to<ConvexPolygonShape3D>(*s)->get_points();
  3748. if (points.size() > 3) {
  3749. Vector<Vector3> varr = Variant(points);
  3750. Geometry3D::MeshData md;
  3751. Error err = ConvexHullComputer::convex_hull(varr, md);
  3752. if (err == OK) {
  3753. Vector<Vector3> points2;
  3754. points2.resize(md.edges.size() * 2);
  3755. for (int i = 0; i < md.edges.size(); i++) {
  3756. points2.write[i * 2 + 0] = md.vertices[md.edges[i].a];
  3757. points2.write[i * 2 + 1] = md.vertices[md.edges[i].b];
  3758. }
  3759. p_gizmo->add_lines(points2, material);
  3760. p_gizmo->add_collision_segments(points2);
  3761. }
  3762. }
  3763. }
  3764. if (Object::cast_to<ConcavePolygonShape3D>(*s)) {
  3765. Ref<ConcavePolygonShape3D> cs2 = s;
  3766. Ref<ArrayMesh> mesh = cs2->get_debug_mesh();
  3767. p_gizmo->add_mesh(mesh, material);
  3768. p_gizmo->add_collision_segments(cs2->get_debug_mesh_lines());
  3769. }
  3770. if (Object::cast_to<SeparationRayShape3D>(*s)) {
  3771. Ref<SeparationRayShape3D> rs = s;
  3772. Vector<Vector3> points = {
  3773. Vector3(),
  3774. Vector3(0, 0, rs->get_length())
  3775. };
  3776. p_gizmo->add_lines(points, material);
  3777. p_gizmo->add_collision_segments(points);
  3778. Vector<Vector3> handles;
  3779. handles.push_back(Vector3(0, 0, rs->get_length()));
  3780. p_gizmo->add_handles(handles, handles_material);
  3781. }
  3782. if (Object::cast_to<HeightMapShape3D>(*s)) {
  3783. Ref<HeightMapShape3D> hms = s;
  3784. Ref<ArrayMesh> mesh = hms->get_debug_mesh();
  3785. p_gizmo->add_mesh(mesh, material);
  3786. }
  3787. }
  3788. /////
  3789. CollisionPolygon3DGizmoPlugin::CollisionPolygon3DGizmoPlugin() {
  3790. const Color gizmo_color = EDITOR_GET("editors/3d_gizmos/gizmo_colors/shape");
  3791. create_material("shape_material", gizmo_color);
  3792. const float gizmo_value = gizmo_color.get_v();
  3793. const Color gizmo_color_disabled = Color(gizmo_value, gizmo_value, gizmo_value, 0.65);
  3794. create_material("shape_material_disabled", gizmo_color_disabled);
  3795. }
  3796. bool CollisionPolygon3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  3797. return Object::cast_to<CollisionPolygon3D>(p_spatial) != nullptr;
  3798. }
  3799. String CollisionPolygon3DGizmoPlugin::get_gizmo_name() const {
  3800. return "CollisionPolygon3D";
  3801. }
  3802. int CollisionPolygon3DGizmoPlugin::get_priority() const {
  3803. return -1;
  3804. }
  3805. void CollisionPolygon3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  3806. CollisionPolygon3D *polygon = Object::cast_to<CollisionPolygon3D>(p_gizmo->get_spatial_node());
  3807. p_gizmo->clear();
  3808. Vector<Vector2> points = polygon->get_polygon();
  3809. float depth = polygon->get_depth() * 0.5;
  3810. Vector<Vector3> lines;
  3811. for (int i = 0; i < points.size(); i++) {
  3812. int n = (i + 1) % points.size();
  3813. lines.push_back(Vector3(points[i].x, points[i].y, depth));
  3814. lines.push_back(Vector3(points[n].x, points[n].y, depth));
  3815. lines.push_back(Vector3(points[i].x, points[i].y, -depth));
  3816. lines.push_back(Vector3(points[n].x, points[n].y, -depth));
  3817. lines.push_back(Vector3(points[i].x, points[i].y, depth));
  3818. lines.push_back(Vector3(points[i].x, points[i].y, -depth));
  3819. }
  3820. const Ref<Material> material =
  3821. get_material(!polygon->is_disabled() ? "shape_material" : "shape_material_disabled", p_gizmo);
  3822. p_gizmo->add_lines(lines, material);
  3823. p_gizmo->add_collision_segments(lines);
  3824. }
  3825. ////
  3826. NavigationRegion3DGizmoPlugin::NavigationRegion3DGizmoPlugin() {
  3827. create_material("face_material", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color(), false, false, true);
  3828. create_material("face_material_disabled", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_disabled_color(), false, false, true);
  3829. create_material("edge_material", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_color());
  3830. create_material("edge_material_disabled", NavigationServer3D::get_singleton()->get_debug_navigation_geometry_edge_disabled_color());
  3831. }
  3832. bool NavigationRegion3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  3833. return Object::cast_to<NavigationRegion3D>(p_spatial) != nullptr;
  3834. }
  3835. String NavigationRegion3DGizmoPlugin::get_gizmo_name() const {
  3836. return "NavigationRegion3D";
  3837. }
  3838. int NavigationRegion3DGizmoPlugin::get_priority() const {
  3839. return -1;
  3840. }
  3841. void NavigationRegion3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  3842. NavigationRegion3D *navigationregion = Object::cast_to<NavigationRegion3D>(p_gizmo->get_spatial_node());
  3843. p_gizmo->clear();
  3844. Ref<NavigationMesh> navigationmesh = navigationregion->get_navigation_mesh();
  3845. if (navigationmesh.is_null()) {
  3846. return;
  3847. }
  3848. Vector<Vector3> vertices = navigationmesh->get_vertices();
  3849. const Vector3 *vr = vertices.ptr();
  3850. List<Face3> faces;
  3851. for (int i = 0; i < navigationmesh->get_polygon_count(); i++) {
  3852. Vector<int> p = navigationmesh->get_polygon(i);
  3853. for (int j = 2; j < p.size(); j++) {
  3854. Face3 f;
  3855. f.vertex[0] = vr[p[0]];
  3856. f.vertex[1] = vr[p[j - 1]];
  3857. f.vertex[2] = vr[p[j]];
  3858. faces.push_back(f);
  3859. }
  3860. }
  3861. if (faces.is_empty()) {
  3862. return;
  3863. }
  3864. HashMap<_EdgeKey, bool, _EdgeKey> edge_map;
  3865. Vector<Vector3> tmeshfaces;
  3866. tmeshfaces.resize(faces.size() * 3);
  3867. {
  3868. Vector3 *tw = tmeshfaces.ptrw();
  3869. int tidx = 0;
  3870. for (const Face3 &f : faces) {
  3871. for (int j = 0; j < 3; j++) {
  3872. tw[tidx++] = f.vertex[j];
  3873. _EdgeKey ek;
  3874. ek.from = f.vertex[j].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON));
  3875. ek.to = f.vertex[(j + 1) % 3].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON));
  3876. if (ek.from < ek.to) {
  3877. SWAP(ek.from, ek.to);
  3878. }
  3879. HashMap<_EdgeKey, bool, _EdgeKey>::Iterator F = edge_map.find(ek);
  3880. if (F) {
  3881. F->value = false;
  3882. } else {
  3883. edge_map[ek] = true;
  3884. }
  3885. }
  3886. }
  3887. }
  3888. Vector<Vector3> lines;
  3889. for (const KeyValue<_EdgeKey, bool> &E : edge_map) {
  3890. if (E.value) {
  3891. lines.push_back(E.key.from);
  3892. lines.push_back(E.key.to);
  3893. }
  3894. }
  3895. Ref<TriangleMesh> tmesh = memnew(TriangleMesh);
  3896. tmesh->create(tmeshfaces);
  3897. p_gizmo->add_collision_triangles(tmesh);
  3898. p_gizmo->add_collision_segments(lines);
  3899. Ref<ArrayMesh> debug_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  3900. int polygon_count = navigationmesh->get_polygon_count();
  3901. // build geometry face surface
  3902. Vector<Vector3> face_vertex_array;
  3903. face_vertex_array.resize(polygon_count * 3);
  3904. for (int i = 0; i < polygon_count; i++) {
  3905. Vector<int> polygon = navigationmesh->get_polygon(i);
  3906. face_vertex_array.push_back(vertices[polygon[0]]);
  3907. face_vertex_array.push_back(vertices[polygon[1]]);
  3908. face_vertex_array.push_back(vertices[polygon[2]]);
  3909. }
  3910. Array face_mesh_array;
  3911. face_mesh_array.resize(Mesh::ARRAY_MAX);
  3912. face_mesh_array[Mesh::ARRAY_VERTEX] = face_vertex_array;
  3913. // if enabled add vertex colors to colorize each face individually
  3914. RandomPCG rand;
  3915. bool enabled_geometry_face_random_color = NavigationServer3D::get_singleton()->get_debug_navigation_enable_geometry_face_random_color();
  3916. if (enabled_geometry_face_random_color) {
  3917. Color debug_navigation_geometry_face_color = NavigationServer3D::get_singleton()->get_debug_navigation_geometry_face_color();
  3918. Color polygon_color = debug_navigation_geometry_face_color;
  3919. Vector<Color> face_color_array;
  3920. face_color_array.resize(polygon_count * 3);
  3921. for (int i = 0; i < polygon_count; i++) {
  3922. // Generate the polygon color, slightly randomly modified from the settings one.
  3923. polygon_color.set_hsv(debug_navigation_geometry_face_color.get_h() + rand.random(-1.0, 1.0) * 0.1, debug_navigation_geometry_face_color.get_s(), debug_navigation_geometry_face_color.get_v() + rand.random(-1.0, 1.0) * 0.2);
  3924. polygon_color.a = debug_navigation_geometry_face_color.a;
  3925. Vector<int> polygon = navigationmesh->get_polygon(i);
  3926. face_color_array.push_back(polygon_color);
  3927. face_color_array.push_back(polygon_color);
  3928. face_color_array.push_back(polygon_color);
  3929. }
  3930. face_mesh_array[Mesh::ARRAY_COLOR] = face_color_array;
  3931. }
  3932. debug_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, face_mesh_array);
  3933. p_gizmo->add_mesh(debug_mesh, navigationregion->is_enabled() ? get_material("face_material", p_gizmo) : get_material("face_material_disabled", p_gizmo));
  3934. // if enabled build geometry edge line surface
  3935. bool enabled_edge_lines = NavigationServer3D::get_singleton()->get_debug_navigation_enable_edge_lines();
  3936. if (enabled_edge_lines) {
  3937. Vector<Vector3> line_vertex_array;
  3938. line_vertex_array.resize(polygon_count * 6);
  3939. for (int i = 0; i < polygon_count; i++) {
  3940. Vector<int> polygon = navigationmesh->get_polygon(i);
  3941. line_vertex_array.push_back(vertices[polygon[0]]);
  3942. line_vertex_array.push_back(vertices[polygon[1]]);
  3943. line_vertex_array.push_back(vertices[polygon[1]]);
  3944. line_vertex_array.push_back(vertices[polygon[2]]);
  3945. line_vertex_array.push_back(vertices[polygon[2]]);
  3946. line_vertex_array.push_back(vertices[polygon[0]]);
  3947. }
  3948. p_gizmo->add_lines(line_vertex_array, navigationregion->is_enabled() ? get_material("edge_material", p_gizmo) : get_material("edge_material_disabled", p_gizmo));
  3949. }
  3950. }
  3951. //////
  3952. #define BODY_A_RADIUS 0.25
  3953. #define BODY_B_RADIUS 0.27
  3954. Basis JointGizmosDrawer::look_body(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) {
  3955. const Vector3 &p_eye(p_joint_transform.origin);
  3956. const Vector3 &p_target(p_body_transform.origin);
  3957. Vector3 v_x, v_y, v_z;
  3958. // Look the body with X
  3959. v_x = p_target - p_eye;
  3960. v_x.normalize();
  3961. v_z = v_x.cross(Vector3(0, 1, 0));
  3962. v_z.normalize();
  3963. v_y = v_z.cross(v_x);
  3964. v_y.normalize();
  3965. Basis base;
  3966. base.set_columns(v_x, v_y, v_z);
  3967. // Absorb current joint transform
  3968. base = p_joint_transform.basis.inverse() * base;
  3969. return base;
  3970. }
  3971. Basis JointGizmosDrawer::look_body_toward(Vector3::Axis p_axis, const Transform3D &joint_transform, const Transform3D &body_transform) {
  3972. switch (p_axis) {
  3973. case Vector3::AXIS_X:
  3974. return look_body_toward_x(joint_transform, body_transform);
  3975. case Vector3::AXIS_Y:
  3976. return look_body_toward_y(joint_transform, body_transform);
  3977. case Vector3::AXIS_Z:
  3978. return look_body_toward_z(joint_transform, body_transform);
  3979. default:
  3980. return Basis();
  3981. }
  3982. }
  3983. Basis JointGizmosDrawer::look_body_toward_x(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) {
  3984. const Vector3 &p_eye(p_joint_transform.origin);
  3985. const Vector3 &p_target(p_body_transform.origin);
  3986. const Vector3 p_front(p_joint_transform.basis.get_column(0));
  3987. Vector3 v_x, v_y, v_z;
  3988. // Look the body with X
  3989. v_x = p_target - p_eye;
  3990. v_x.normalize();
  3991. v_y = p_front.cross(v_x);
  3992. v_y.normalize();
  3993. v_z = v_y.cross(p_front);
  3994. v_z.normalize();
  3995. // Clamp X to FRONT axis
  3996. v_x = p_front;
  3997. v_x.normalize();
  3998. Basis base;
  3999. base.set_columns(v_x, v_y, v_z);
  4000. // Absorb current joint transform
  4001. base = p_joint_transform.basis.inverse() * base;
  4002. return base;
  4003. }
  4004. Basis JointGizmosDrawer::look_body_toward_y(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) {
  4005. const Vector3 &p_eye(p_joint_transform.origin);
  4006. const Vector3 &p_target(p_body_transform.origin);
  4007. const Vector3 p_up(p_joint_transform.basis.get_column(1));
  4008. Vector3 v_x, v_y, v_z;
  4009. // Look the body with X
  4010. v_x = p_target - p_eye;
  4011. v_x.normalize();
  4012. v_z = v_x.cross(p_up);
  4013. v_z.normalize();
  4014. v_x = p_up.cross(v_z);
  4015. v_x.normalize();
  4016. // Clamp Y to UP axis
  4017. v_y = p_up;
  4018. v_y.normalize();
  4019. Basis base;
  4020. base.set_columns(v_x, v_y, v_z);
  4021. // Absorb current joint transform
  4022. base = p_joint_transform.basis.inverse() * base;
  4023. return base;
  4024. }
  4025. Basis JointGizmosDrawer::look_body_toward_z(const Transform3D &p_joint_transform, const Transform3D &p_body_transform) {
  4026. const Vector3 &p_eye(p_joint_transform.origin);
  4027. const Vector3 &p_target(p_body_transform.origin);
  4028. const Vector3 p_lateral(p_joint_transform.basis.get_column(2));
  4029. Vector3 v_x, v_y, v_z;
  4030. // Look the body with X
  4031. v_x = p_target - p_eye;
  4032. v_x.normalize();
  4033. v_z = p_lateral;
  4034. v_z.normalize();
  4035. v_y = v_z.cross(v_x);
  4036. v_y.normalize();
  4037. // Clamp X to Z axis
  4038. v_x = v_y.cross(v_z);
  4039. v_x.normalize();
  4040. Basis base;
  4041. base.set_columns(v_x, v_y, v_z);
  4042. // Absorb current joint transform
  4043. base = p_joint_transform.basis.inverse() * base;
  4044. return base;
  4045. }
  4046. void JointGizmosDrawer::draw_circle(Vector3::Axis p_axis, real_t p_radius, const Transform3D &p_offset, const Basis &p_base, real_t p_limit_lower, real_t p_limit_upper, Vector<Vector3> &r_points, bool p_inverse) {
  4047. if (p_limit_lower == p_limit_upper) {
  4048. r_points.push_back(p_offset.translated_local(Vector3()).origin);
  4049. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(0.5, 0, 0))).origin);
  4050. } else {
  4051. if (p_limit_lower > p_limit_upper) {
  4052. p_limit_lower = -Math_PI;
  4053. p_limit_upper = Math_PI;
  4054. }
  4055. const int points = 32;
  4056. for (int i = 0; i < points; i++) {
  4057. real_t s = p_limit_lower + i * (p_limit_upper - p_limit_lower) / points;
  4058. real_t n = p_limit_lower + (i + 1) * (p_limit_upper - p_limit_lower) / points;
  4059. Vector3 from;
  4060. Vector3 to;
  4061. switch (p_axis) {
  4062. case Vector3::AXIS_X:
  4063. if (p_inverse) {
  4064. from = p_base.xform(Vector3(0, Math::sin(s), Math::cos(s))) * p_radius;
  4065. to = p_base.xform(Vector3(0, Math::sin(n), Math::cos(n))) * p_radius;
  4066. } else {
  4067. from = p_base.xform(Vector3(0, -Math::sin(s), Math::cos(s))) * p_radius;
  4068. to = p_base.xform(Vector3(0, -Math::sin(n), Math::cos(n))) * p_radius;
  4069. }
  4070. break;
  4071. case Vector3::AXIS_Y:
  4072. if (p_inverse) {
  4073. from = p_base.xform(Vector3(Math::cos(s), 0, -Math::sin(s))) * p_radius;
  4074. to = p_base.xform(Vector3(Math::cos(n), 0, -Math::sin(n))) * p_radius;
  4075. } else {
  4076. from = p_base.xform(Vector3(Math::cos(s), 0, Math::sin(s))) * p_radius;
  4077. to = p_base.xform(Vector3(Math::cos(n), 0, Math::sin(n))) * p_radius;
  4078. }
  4079. break;
  4080. case Vector3::AXIS_Z:
  4081. from = p_base.xform(Vector3(Math::cos(s), Math::sin(s), 0)) * p_radius;
  4082. to = p_base.xform(Vector3(Math::cos(n), Math::sin(n), 0)) * p_radius;
  4083. break;
  4084. }
  4085. if (i == points - 1) {
  4086. r_points.push_back(p_offset.translated_local(to).origin);
  4087. r_points.push_back(p_offset.translated_local(Vector3()).origin);
  4088. }
  4089. if (i == 0) {
  4090. r_points.push_back(p_offset.translated_local(from).origin);
  4091. r_points.push_back(p_offset.translated_local(Vector3()).origin);
  4092. }
  4093. r_points.push_back(p_offset.translated_local(from).origin);
  4094. r_points.push_back(p_offset.translated_local(to).origin);
  4095. }
  4096. r_points.push_back(p_offset.translated_local(Vector3(0, p_radius * 1.5, 0)).origin);
  4097. r_points.push_back(p_offset.translated_local(Vector3()).origin);
  4098. }
  4099. }
  4100. void JointGizmosDrawer::draw_cone(const Transform3D &p_offset, const Basis &p_base, real_t p_swing, real_t p_twist, Vector<Vector3> &r_points) {
  4101. float r = 1.0;
  4102. float w = r * Math::sin(p_swing);
  4103. float d = r * Math::cos(p_swing);
  4104. //swing
  4105. for (int i = 0; i < 360; i += 10) {
  4106. float ra = Math::deg_to_rad((float)i);
  4107. float rb = Math::deg_to_rad((float)i + 10);
  4108. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;
  4109. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;
  4110. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(d, a.x, a.y))).origin);
  4111. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(d, b.x, b.y))).origin);
  4112. if (i % 90 == 0) {
  4113. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(d, a.x, a.y))).origin);
  4114. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3())).origin);
  4115. }
  4116. }
  4117. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3())).origin);
  4118. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(1, 0, 0))).origin);
  4119. /// Twist
  4120. float ts = Math::rad_to_deg(p_twist);
  4121. ts = MIN(ts, 720);
  4122. for (int i = 0; i < int(ts); i += 5) {
  4123. float ra = Math::deg_to_rad((float)i);
  4124. float rb = Math::deg_to_rad((float)i + 5);
  4125. float c = i / 720.0;
  4126. float cn = (i + 5) / 720.0;
  4127. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w * c;
  4128. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w * cn;
  4129. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(c, a.x, a.y))).origin);
  4130. r_points.push_back(p_offset.translated_local(p_base.xform(Vector3(cn, b.x, b.y))).origin);
  4131. }
  4132. }
  4133. ////
  4134. Joint3DGizmoPlugin::Joint3DGizmoPlugin() {
  4135. create_material("joint_material", EDITOR_GET("editors/3d_gizmos/gizmo_colors/joint"));
  4136. create_material("joint_body_a_material", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/joint_body_a", Color(0.6, 0.8, 1)));
  4137. create_material("joint_body_b_material", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/joint_body_b", Color(0.6, 0.9, 1)));
  4138. update_timer = memnew(Timer);
  4139. update_timer->set_name("JointGizmoUpdateTimer");
  4140. update_timer->set_wait_time(1.0 / 120.0);
  4141. update_timer->connect("timeout", callable_mp(this, &Joint3DGizmoPlugin::incremental_update_gizmos));
  4142. update_timer->set_autostart(true);
  4143. EditorNode::get_singleton()->call_deferred(SNAME("add_child"), update_timer);
  4144. }
  4145. void Joint3DGizmoPlugin::incremental_update_gizmos() {
  4146. if (!current_gizmos.is_empty()) {
  4147. update_idx++;
  4148. update_idx = update_idx % current_gizmos.size();
  4149. redraw(current_gizmos[update_idx]);
  4150. }
  4151. }
  4152. bool Joint3DGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  4153. return Object::cast_to<Joint3D>(p_spatial) != nullptr;
  4154. }
  4155. String Joint3DGizmoPlugin::get_gizmo_name() const {
  4156. return "Joint3D";
  4157. }
  4158. int Joint3DGizmoPlugin::get_priority() const {
  4159. return -1;
  4160. }
  4161. void Joint3DGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  4162. Joint3D *joint = Object::cast_to<Joint3D>(p_gizmo->get_spatial_node());
  4163. p_gizmo->clear();
  4164. Node3D *node_body_a = nullptr;
  4165. if (!joint->get_node_a().is_empty()) {
  4166. node_body_a = Object::cast_to<Node3D>(joint->get_node(joint->get_node_a()));
  4167. }
  4168. Node3D *node_body_b = nullptr;
  4169. if (!joint->get_node_b().is_empty()) {
  4170. node_body_b = Object::cast_to<Node3D>(joint->get_node(joint->get_node_b()));
  4171. }
  4172. if (!node_body_a && !node_body_b) {
  4173. return;
  4174. }
  4175. Ref<Material> common_material = get_material("joint_material", p_gizmo);
  4176. Ref<Material> body_a_material = get_material("joint_body_a_material", p_gizmo);
  4177. Ref<Material> body_b_material = get_material("joint_body_b_material", p_gizmo);
  4178. Vector<Vector3> points;
  4179. Vector<Vector3> body_a_points;
  4180. Vector<Vector3> body_b_points;
  4181. if (Object::cast_to<PinJoint3D>(joint)) {
  4182. CreatePinJointGizmo(Transform3D(), points);
  4183. p_gizmo->add_collision_segments(points);
  4184. p_gizmo->add_lines(points, common_material);
  4185. }
  4186. HingeJoint3D *hinge = Object::cast_to<HingeJoint3D>(joint);
  4187. if (hinge) {
  4188. CreateHingeJointGizmo(
  4189. Transform3D(),
  4190. hinge->get_global_transform(),
  4191. node_body_a ? node_body_a->get_global_transform() : Transform3D(),
  4192. node_body_b ? node_body_b->get_global_transform() : Transform3D(),
  4193. hinge->get_param(HingeJoint3D::PARAM_LIMIT_LOWER),
  4194. hinge->get_param(HingeJoint3D::PARAM_LIMIT_UPPER),
  4195. hinge->get_flag(HingeJoint3D::FLAG_USE_LIMIT),
  4196. points,
  4197. node_body_a ? &body_a_points : nullptr,
  4198. node_body_b ? &body_b_points : nullptr);
  4199. p_gizmo->add_collision_segments(points);
  4200. p_gizmo->add_collision_segments(body_a_points);
  4201. p_gizmo->add_collision_segments(body_b_points);
  4202. p_gizmo->add_lines(points, common_material);
  4203. p_gizmo->add_lines(body_a_points, body_a_material);
  4204. p_gizmo->add_lines(body_b_points, body_b_material);
  4205. }
  4206. SliderJoint3D *slider = Object::cast_to<SliderJoint3D>(joint);
  4207. if (slider) {
  4208. CreateSliderJointGizmo(
  4209. Transform3D(),
  4210. slider->get_global_transform(),
  4211. node_body_a ? node_body_a->get_global_transform() : Transform3D(),
  4212. node_body_b ? node_body_b->get_global_transform() : Transform3D(),
  4213. slider->get_param(SliderJoint3D::PARAM_ANGULAR_LIMIT_LOWER),
  4214. slider->get_param(SliderJoint3D::PARAM_ANGULAR_LIMIT_UPPER),
  4215. slider->get_param(SliderJoint3D::PARAM_LINEAR_LIMIT_LOWER),
  4216. slider->get_param(SliderJoint3D::PARAM_LINEAR_LIMIT_UPPER),
  4217. points,
  4218. node_body_a ? &body_a_points : nullptr,
  4219. node_body_b ? &body_b_points : nullptr);
  4220. p_gizmo->add_collision_segments(points);
  4221. p_gizmo->add_collision_segments(body_a_points);
  4222. p_gizmo->add_collision_segments(body_b_points);
  4223. p_gizmo->add_lines(points, common_material);
  4224. p_gizmo->add_lines(body_a_points, body_a_material);
  4225. p_gizmo->add_lines(body_b_points, body_b_material);
  4226. }
  4227. ConeTwistJoint3D *cone = Object::cast_to<ConeTwistJoint3D>(joint);
  4228. if (cone) {
  4229. CreateConeTwistJointGizmo(
  4230. Transform3D(),
  4231. cone->get_global_transform(),
  4232. node_body_a ? node_body_a->get_global_transform() : Transform3D(),
  4233. node_body_b ? node_body_b->get_global_transform() : Transform3D(),
  4234. cone->get_param(ConeTwistJoint3D::PARAM_SWING_SPAN),
  4235. cone->get_param(ConeTwistJoint3D::PARAM_TWIST_SPAN),
  4236. node_body_a ? &body_a_points : nullptr,
  4237. node_body_b ? &body_b_points : nullptr);
  4238. p_gizmo->add_collision_segments(body_a_points);
  4239. p_gizmo->add_collision_segments(body_b_points);
  4240. p_gizmo->add_lines(body_a_points, body_a_material);
  4241. p_gizmo->add_lines(body_b_points, body_b_material);
  4242. }
  4243. Generic6DOFJoint3D *gen = Object::cast_to<Generic6DOFJoint3D>(joint);
  4244. if (gen) {
  4245. CreateGeneric6DOFJointGizmo(
  4246. Transform3D(),
  4247. gen->get_global_transform(),
  4248. node_body_a ? node_body_a->get_global_transform() : Transform3D(),
  4249. node_body_b ? node_body_b->get_global_transform() : Transform3D(),
  4250. gen->get_param_x(Generic6DOFJoint3D::PARAM_ANGULAR_LOWER_LIMIT),
  4251. gen->get_param_x(Generic6DOFJoint3D::PARAM_ANGULAR_UPPER_LIMIT),
  4252. gen->get_param_x(Generic6DOFJoint3D::PARAM_LINEAR_LOWER_LIMIT),
  4253. gen->get_param_x(Generic6DOFJoint3D::PARAM_LINEAR_UPPER_LIMIT),
  4254. gen->get_flag_x(Generic6DOFJoint3D::FLAG_ENABLE_ANGULAR_LIMIT),
  4255. gen->get_flag_x(Generic6DOFJoint3D::FLAG_ENABLE_LINEAR_LIMIT),
  4256. gen->get_param_y(Generic6DOFJoint3D::PARAM_ANGULAR_LOWER_LIMIT),
  4257. gen->get_param_y(Generic6DOFJoint3D::PARAM_ANGULAR_UPPER_LIMIT),
  4258. gen->get_param_y(Generic6DOFJoint3D::PARAM_LINEAR_LOWER_LIMIT),
  4259. gen->get_param_y(Generic6DOFJoint3D::PARAM_LINEAR_UPPER_LIMIT),
  4260. gen->get_flag_y(Generic6DOFJoint3D::FLAG_ENABLE_ANGULAR_LIMIT),
  4261. gen->get_flag_y(Generic6DOFJoint3D::FLAG_ENABLE_LINEAR_LIMIT),
  4262. gen->get_param_z(Generic6DOFJoint3D::PARAM_ANGULAR_LOWER_LIMIT),
  4263. gen->get_param_z(Generic6DOFJoint3D::PARAM_ANGULAR_UPPER_LIMIT),
  4264. gen->get_param_z(Generic6DOFJoint3D::PARAM_LINEAR_LOWER_LIMIT),
  4265. gen->get_param_z(Generic6DOFJoint3D::PARAM_LINEAR_UPPER_LIMIT),
  4266. gen->get_flag_z(Generic6DOFJoint3D::FLAG_ENABLE_ANGULAR_LIMIT),
  4267. gen->get_flag_z(Generic6DOFJoint3D::FLAG_ENABLE_LINEAR_LIMIT),
  4268. points,
  4269. node_body_a ? &body_a_points : nullptr,
  4270. node_body_a ? &body_b_points : nullptr);
  4271. p_gizmo->add_collision_segments(points);
  4272. p_gizmo->add_collision_segments(body_a_points);
  4273. p_gizmo->add_collision_segments(body_b_points);
  4274. p_gizmo->add_lines(points, common_material);
  4275. p_gizmo->add_lines(body_a_points, body_a_material);
  4276. p_gizmo->add_lines(body_b_points, body_b_material);
  4277. }
  4278. }
  4279. void Joint3DGizmoPlugin::CreatePinJointGizmo(const Transform3D &p_offset, Vector<Vector3> &r_cursor_points) {
  4280. float cs = 0.25;
  4281. r_cursor_points.push_back(p_offset.translated_local(Vector3(+cs, 0, 0)).origin);
  4282. r_cursor_points.push_back(p_offset.translated_local(Vector3(-cs, 0, 0)).origin);
  4283. r_cursor_points.push_back(p_offset.translated_local(Vector3(0, +cs, 0)).origin);
  4284. r_cursor_points.push_back(p_offset.translated_local(Vector3(0, -cs, 0)).origin);
  4285. r_cursor_points.push_back(p_offset.translated_local(Vector3(0, 0, +cs)).origin);
  4286. r_cursor_points.push_back(p_offset.translated_local(Vector3(0, 0, -cs)).origin);
  4287. }
  4288. void Joint3DGizmoPlugin::CreateHingeJointGizmo(const Transform3D &p_offset, const Transform3D &p_trs_joint, const Transform3D &p_trs_body_a, const Transform3D &p_trs_body_b, real_t p_limit_lower, real_t p_limit_upper, bool p_use_limit, Vector<Vector3> &r_common_points, Vector<Vector3> *r_body_a_points, Vector<Vector3> *r_body_b_points) {
  4289. r_common_points.push_back(p_offset.translated_local(Vector3(0, 0, 0.5)).origin);
  4290. r_common_points.push_back(p_offset.translated_local(Vector3(0, 0, -0.5)).origin);
  4291. if (!p_use_limit) {
  4292. p_limit_upper = -1;
  4293. p_limit_lower = 0;
  4294. }
  4295. if (r_body_a_points) {
  4296. JointGizmosDrawer::draw_circle(Vector3::AXIS_Z,
  4297. BODY_A_RADIUS,
  4298. p_offset,
  4299. JointGizmosDrawer::look_body_toward_z(p_trs_joint, p_trs_body_a),
  4300. p_limit_lower,
  4301. p_limit_upper,
  4302. *r_body_a_points);
  4303. }
  4304. if (r_body_b_points) {
  4305. JointGizmosDrawer::draw_circle(Vector3::AXIS_Z,
  4306. BODY_B_RADIUS,
  4307. p_offset,
  4308. JointGizmosDrawer::look_body_toward_z(p_trs_joint, p_trs_body_b),
  4309. p_limit_lower,
  4310. p_limit_upper,
  4311. *r_body_b_points);
  4312. }
  4313. }
  4314. void Joint3DGizmoPlugin::CreateSliderJointGizmo(const Transform3D &p_offset, const Transform3D &p_trs_joint, const Transform3D &p_trs_body_a, const Transform3D &p_trs_body_b, real_t p_angular_limit_lower, real_t p_angular_limit_upper, real_t p_linear_limit_lower, real_t p_linear_limit_upper, Vector<Vector3> &r_points, Vector<Vector3> *r_body_a_points, Vector<Vector3> *r_body_b_points) {
  4315. p_linear_limit_lower = -p_linear_limit_lower;
  4316. p_linear_limit_upper = -p_linear_limit_upper;
  4317. float cs = 0.25;
  4318. r_points.push_back(p_offset.translated_local(Vector3(0, 0, 0.5)).origin);
  4319. r_points.push_back(p_offset.translated_local(Vector3(0, 0, -0.5)).origin);
  4320. if (p_linear_limit_lower >= p_linear_limit_upper) {
  4321. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, 0, 0)).origin);
  4322. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, 0, 0)).origin);
  4323. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, -cs)).origin);
  4324. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, cs)).origin);
  4325. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, cs)).origin);
  4326. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, cs)).origin);
  4327. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, cs)).origin);
  4328. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, -cs)).origin);
  4329. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, cs, -cs)).origin);
  4330. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_upper, -cs, -cs)).origin);
  4331. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, -cs)).origin);
  4332. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, cs)).origin);
  4333. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, cs)).origin);
  4334. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, cs)).origin);
  4335. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, cs)).origin);
  4336. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, -cs)).origin);
  4337. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, cs, -cs)).origin);
  4338. r_points.push_back(p_offset.translated_local(Vector3(p_linear_limit_lower, -cs, -cs)).origin);
  4339. } else {
  4340. r_points.push_back(p_offset.translated_local(Vector3(+cs * 2, 0, 0)).origin);
  4341. r_points.push_back(p_offset.translated_local(Vector3(-cs * 2, 0, 0)).origin);
  4342. }
  4343. if (r_body_a_points) {
  4344. JointGizmosDrawer::draw_circle(
  4345. Vector3::AXIS_X,
  4346. BODY_A_RADIUS,
  4347. p_offset,
  4348. JointGizmosDrawer::look_body_toward(Vector3::AXIS_X, p_trs_joint, p_trs_body_a),
  4349. p_angular_limit_lower,
  4350. p_angular_limit_upper,
  4351. *r_body_a_points);
  4352. }
  4353. if (r_body_b_points) {
  4354. JointGizmosDrawer::draw_circle(
  4355. Vector3::AXIS_X,
  4356. BODY_B_RADIUS,
  4357. p_offset,
  4358. JointGizmosDrawer::look_body_toward(Vector3::AXIS_X, p_trs_joint, p_trs_body_b),
  4359. p_angular_limit_lower,
  4360. p_angular_limit_upper,
  4361. *r_body_b_points,
  4362. true);
  4363. }
  4364. }
  4365. void Joint3DGizmoPlugin::CreateConeTwistJointGizmo(const Transform3D &p_offset, const Transform3D &p_trs_joint, const Transform3D &p_trs_body_a, const Transform3D &p_trs_body_b, real_t p_swing, real_t p_twist, Vector<Vector3> *r_body_a_points, Vector<Vector3> *r_body_b_points) {
  4366. if (r_body_a_points) {
  4367. JointGizmosDrawer::draw_cone(
  4368. p_offset,
  4369. JointGizmosDrawer::look_body(p_trs_joint, p_trs_body_a),
  4370. p_swing,
  4371. p_twist,
  4372. *r_body_a_points);
  4373. }
  4374. if (r_body_b_points) {
  4375. JointGizmosDrawer::draw_cone(
  4376. p_offset,
  4377. JointGizmosDrawer::look_body(p_trs_joint, p_trs_body_b),
  4378. p_swing,
  4379. p_twist,
  4380. *r_body_b_points);
  4381. }
  4382. }
  4383. void Joint3DGizmoPlugin::CreateGeneric6DOFJointGizmo(
  4384. const Transform3D &p_offset,
  4385. const Transform3D &p_trs_joint,
  4386. const Transform3D &p_trs_body_a,
  4387. const Transform3D &p_trs_body_b,
  4388. real_t p_angular_limit_lower_x,
  4389. real_t p_angular_limit_upper_x,
  4390. real_t p_linear_limit_lower_x,
  4391. real_t p_linear_limit_upper_x,
  4392. bool p_enable_angular_limit_x,
  4393. bool p_enable_linear_limit_x,
  4394. real_t p_angular_limit_lower_y,
  4395. real_t p_angular_limit_upper_y,
  4396. real_t p_linear_limit_lower_y,
  4397. real_t p_linear_limit_upper_y,
  4398. bool p_enable_angular_limit_y,
  4399. bool p_enable_linear_limit_y,
  4400. real_t p_angular_limit_lower_z,
  4401. real_t p_angular_limit_upper_z,
  4402. real_t p_linear_limit_lower_z,
  4403. real_t p_linear_limit_upper_z,
  4404. bool p_enable_angular_limit_z,
  4405. bool p_enable_linear_limit_z,
  4406. Vector<Vector3> &r_points,
  4407. Vector<Vector3> *r_body_a_points,
  4408. Vector<Vector3> *r_body_b_points) {
  4409. float cs = 0.25;
  4410. for (int ax = 0; ax < 3; ax++) {
  4411. float ll = 0;
  4412. float ul = 0;
  4413. float lll = 0;
  4414. float lul = 0;
  4415. int a1 = 0;
  4416. int a2 = 0;
  4417. int a3 = 0;
  4418. bool enable_ang = false;
  4419. bool enable_lin = false;
  4420. switch (ax) {
  4421. case 0:
  4422. ll = p_angular_limit_lower_x;
  4423. ul = p_angular_limit_upper_x;
  4424. lll = -p_linear_limit_lower_x;
  4425. lul = -p_linear_limit_upper_x;
  4426. enable_ang = p_enable_angular_limit_x;
  4427. enable_lin = p_enable_linear_limit_x;
  4428. a1 = 0;
  4429. a2 = 1;
  4430. a3 = 2;
  4431. break;
  4432. case 1:
  4433. ll = p_angular_limit_lower_y;
  4434. ul = p_angular_limit_upper_y;
  4435. lll = -p_linear_limit_lower_y;
  4436. lul = -p_linear_limit_upper_y;
  4437. enable_ang = p_enable_angular_limit_y;
  4438. enable_lin = p_enable_linear_limit_y;
  4439. a1 = 1;
  4440. a2 = 2;
  4441. a3 = 0;
  4442. break;
  4443. case 2:
  4444. ll = p_angular_limit_lower_z;
  4445. ul = p_angular_limit_upper_z;
  4446. lll = -p_linear_limit_lower_z;
  4447. lul = -p_linear_limit_upper_z;
  4448. enable_ang = p_enable_angular_limit_z;
  4449. enable_lin = p_enable_linear_limit_z;
  4450. a1 = 2;
  4451. a2 = 0;
  4452. a3 = 1;
  4453. break;
  4454. }
  4455. #define ADD_VTX(x, y, z) \
  4456. { \
  4457. Vector3 v; \
  4458. v[a1] = (x); \
  4459. v[a2] = (y); \
  4460. v[a3] = (z); \
  4461. r_points.push_back(p_offset.translated_local(v).origin); \
  4462. }
  4463. if (enable_lin && lll >= lul) {
  4464. ADD_VTX(lul, 0, 0);
  4465. ADD_VTX(lll, 0, 0);
  4466. ADD_VTX(lul, -cs, -cs);
  4467. ADD_VTX(lul, -cs, cs);
  4468. ADD_VTX(lul, -cs, cs);
  4469. ADD_VTX(lul, cs, cs);
  4470. ADD_VTX(lul, cs, cs);
  4471. ADD_VTX(lul, cs, -cs);
  4472. ADD_VTX(lul, cs, -cs);
  4473. ADD_VTX(lul, -cs, -cs);
  4474. ADD_VTX(lll, -cs, -cs);
  4475. ADD_VTX(lll, -cs, cs);
  4476. ADD_VTX(lll, -cs, cs);
  4477. ADD_VTX(lll, cs, cs);
  4478. ADD_VTX(lll, cs, cs);
  4479. ADD_VTX(lll, cs, -cs);
  4480. ADD_VTX(lll, cs, -cs);
  4481. ADD_VTX(lll, -cs, -cs);
  4482. } else {
  4483. ADD_VTX(+cs * 2, 0, 0);
  4484. ADD_VTX(-cs * 2, 0, 0);
  4485. }
  4486. if (!enable_ang) {
  4487. ll = 0;
  4488. ul = -1;
  4489. }
  4490. if (r_body_a_points) {
  4491. JointGizmosDrawer::draw_circle(
  4492. static_cast<Vector3::Axis>(ax),
  4493. BODY_A_RADIUS,
  4494. p_offset,
  4495. JointGizmosDrawer::look_body_toward(static_cast<Vector3::Axis>(ax), p_trs_joint, p_trs_body_a),
  4496. ll,
  4497. ul,
  4498. *r_body_a_points,
  4499. true);
  4500. }
  4501. if (r_body_b_points) {
  4502. JointGizmosDrawer::draw_circle(
  4503. static_cast<Vector3::Axis>(ax),
  4504. BODY_B_RADIUS,
  4505. p_offset,
  4506. JointGizmosDrawer::look_body_toward(static_cast<Vector3::Axis>(ax), p_trs_joint, p_trs_body_b),
  4507. ll,
  4508. ul,
  4509. *r_body_b_points);
  4510. }
  4511. }
  4512. #undef ADD_VTX
  4513. }
  4514. ////
  4515. FogVolumeGizmoPlugin::FogVolumeGizmoPlugin() {
  4516. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/fog_volume", Color(0.5, 0.7, 1));
  4517. create_material("shape_material", gizmo_color);
  4518. gizmo_color.a = 0.15;
  4519. create_material("shape_material_internal", gizmo_color);
  4520. create_handle_material("handles");
  4521. }
  4522. bool FogVolumeGizmoPlugin::has_gizmo(Node3D *p_spatial) {
  4523. return (Object::cast_to<FogVolume>(p_spatial) != nullptr);
  4524. }
  4525. String FogVolumeGizmoPlugin::get_gizmo_name() const {
  4526. return "FogVolume";
  4527. }
  4528. int FogVolumeGizmoPlugin::get_priority() const {
  4529. return -1;
  4530. }
  4531. String FogVolumeGizmoPlugin::get_handle_name(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  4532. return "Extents";
  4533. }
  4534. Variant FogVolumeGizmoPlugin::get_handle_value(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary) const {
  4535. return Vector3(p_gizmo->get_spatial_node()->call("get_extents"));
  4536. }
  4537. void FogVolumeGizmoPlugin::set_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, Camera3D *p_camera, const Point2 &p_point) {
  4538. Node3D *sn = p_gizmo->get_spatial_node();
  4539. Transform3D gt = sn->get_global_transform();
  4540. Transform3D gi = gt.affine_inverse();
  4541. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  4542. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  4543. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  4544. Vector3 axis;
  4545. axis[p_id] = 1.0;
  4546. Vector3 ra, rb;
  4547. Geometry3D::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  4548. float d = ra[p_id];
  4549. if (Node3DEditor::get_singleton()->is_snap_enabled()) {
  4550. d = Math::snapped(d, Node3DEditor::get_singleton()->get_translate_snap());
  4551. }
  4552. if (d < 0.001) {
  4553. d = 0.001;
  4554. }
  4555. Vector3 he = sn->call("get_extents");
  4556. he[p_id] = d;
  4557. sn->call("set_extents", he);
  4558. }
  4559. void FogVolumeGizmoPlugin::commit_handle(const EditorNode3DGizmo *p_gizmo, int p_id, bool p_secondary, const Variant &p_restore, bool p_cancel) {
  4560. Node3D *sn = p_gizmo->get_spatial_node();
  4561. if (p_cancel) {
  4562. sn->call("set_extents", p_restore);
  4563. return;
  4564. }
  4565. Ref<EditorUndoRedoManager> &ur = EditorNode::get_undo_redo();
  4566. ur->create_action(TTR("Change Fog Volume Extents"));
  4567. ur->add_do_method(sn, "set_extents", sn->call("get_extents"));
  4568. ur->add_undo_method(sn, "set_extents", p_restore);
  4569. ur->commit_action();
  4570. }
  4571. void FogVolumeGizmoPlugin::redraw(EditorNode3DGizmo *p_gizmo) {
  4572. Node3D *cs = p_gizmo->get_spatial_node();
  4573. p_gizmo->clear();
  4574. if (RS::FogVolumeShape(int(p_gizmo->get_spatial_node()->call("get_shape"))) != RS::FOG_VOLUME_SHAPE_WORLD) {
  4575. const Ref<Material> material =
  4576. get_material("shape_material", p_gizmo);
  4577. const Ref<Material> material_internal =
  4578. get_material("shape_material_internal", p_gizmo);
  4579. Ref<Material> handles_material = get_material("handles");
  4580. Vector<Vector3> lines;
  4581. AABB aabb;
  4582. aabb.position = -cs->call("get_extents").operator Vector3();
  4583. aabb.size = aabb.position * -2;
  4584. for (int i = 0; i < 12; i++) {
  4585. Vector3 a, b;
  4586. aabb.get_edge(i, a, b);
  4587. lines.push_back(a);
  4588. lines.push_back(b);
  4589. }
  4590. Vector<Vector3> handles;
  4591. for (int i = 0; i < 3; i++) {
  4592. Vector3 ax;
  4593. ax[i] = cs->call("get_extents").operator Vector3()[i];
  4594. handles.push_back(ax);
  4595. }
  4596. p_gizmo->add_lines(lines, material);
  4597. p_gizmo->add_collision_segments(lines);
  4598. p_gizmo->add_handles(handles, handles_material);
  4599. }
  4600. }
  4601. /////