spatial_editor_gizmos.cpp 157 KB

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