spatial_editor_gizmos.cpp 165 KB

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