spatial_editor_gizmos.cpp 167 KB

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