spatial_editor_gizmos.cpp 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/geometry.h"
  32. #include "core/math/quick_hull.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/particles.h"
  44. #include "scene/3d/physics_joint.h"
  45. #include "scene/3d/position_3d.h"
  46. #include "scene/3d/ray_cast.h"
  47. #include "scene/3d/reflection_probe.h"
  48. #include "scene/3d/soft_body.h"
  49. #include "scene/3d/spring_arm.h"
  50. #include "scene/3d/sprite_3d.h"
  51. #include "scene/3d/vehicle_body.h"
  52. #include "scene/3d/visibility_notifier.h"
  53. #include "scene/resources/box_shape.h"
  54. #include "scene/resources/capsule_shape.h"
  55. #include "scene/resources/concave_polygon_shape.h"
  56. #include "scene/resources/convex_polygon_shape.h"
  57. #include "scene/resources/cylinder_shape.h"
  58. #include "scene/resources/height_map_shape.h"
  59. #include "scene/resources/plane_shape.h"
  60. #include "scene/resources/primitive_meshes.h"
  61. #include "scene/resources/ray_shape.h"
  62. #include "scene/resources/sphere_shape.h"
  63. #include "scene/resources/surface_tool.h"
  64. #define HANDLE_HALF_SIZE 9.5
  65. bool EditorSpatialGizmo::is_editable() const {
  66. ERR_FAIL_COND_V(!spatial_node, false);
  67. Node *edited_root = spatial_node->get_tree()->get_edited_scene_root();
  68. if (spatial_node == edited_root)
  69. return true;
  70. if (spatial_node->get_owner() == edited_root)
  71. return true;
  72. if (edited_root->is_editable_instance(spatial_node->get_owner()))
  73. return true;
  74. return false;
  75. }
  76. void EditorSpatialGizmo::clear() {
  77. for (int i = 0; i < instances.size(); i++) {
  78. if (instances[i].instance.is_valid())
  79. VS::get_singleton()->free(instances[i].instance);
  80. }
  81. billboard_handle = false;
  82. collision_segments.clear();
  83. collision_mesh = Ref<TriangleMesh>();
  84. instances.clear();
  85. handles.clear();
  86. secondary_handles.clear();
  87. }
  88. void EditorSpatialGizmo::redraw() {
  89. if (get_script_instance() && get_script_instance()->has_method("redraw")) {
  90. get_script_instance()->call("redraw");
  91. return;
  92. }
  93. ERR_FAIL_COND(!gizmo_plugin);
  94. gizmo_plugin->redraw(this);
  95. }
  96. String EditorSpatialGizmo::get_handle_name(int p_idx) const {
  97. if (get_script_instance() && get_script_instance()->has_method("get_handle_name")) {
  98. return get_script_instance()->call("get_handle_name", p_idx);
  99. }
  100. ERR_FAIL_COND_V(!gizmo_plugin, "");
  101. return gizmo_plugin->get_handle_name(this, p_idx);
  102. }
  103. bool EditorSpatialGizmo::is_handle_highlighted(int p_idx) const {
  104. if (get_script_instance() && get_script_instance()->has_method("is_handle_highlighted")) {
  105. return get_script_instance()->call("is_handle_highlighted", p_idx);
  106. }
  107. ERR_FAIL_COND_V(!gizmo_plugin, false);
  108. return gizmo_plugin->is_handle_highlighted(this, p_idx);
  109. }
  110. Variant EditorSpatialGizmo::get_handle_value(int p_idx) {
  111. if (get_script_instance() && get_script_instance()->has_method("get_handle_value")) {
  112. return get_script_instance()->call("get_handle_value", p_idx);
  113. }
  114. ERR_FAIL_COND_V(!gizmo_plugin, Variant());
  115. return gizmo_plugin->get_handle_value(this, p_idx);
  116. }
  117. void EditorSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
  118. if (get_script_instance() && get_script_instance()->has_method("set_handle")) {
  119. get_script_instance()->call("set_handle", p_idx, p_camera, p_point);
  120. return;
  121. }
  122. ERR_FAIL_COND(!gizmo_plugin);
  123. gizmo_plugin->set_handle(this, p_idx, p_camera, p_point);
  124. }
  125. void EditorSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
  126. if (get_script_instance() && get_script_instance()->has_method("commit_handle")) {
  127. get_script_instance()->call("commit_handle", p_idx, p_restore, p_cancel);
  128. return;
  129. }
  130. ERR_FAIL_COND(!gizmo_plugin);
  131. gizmo_plugin->commit_handle(this, p_idx, p_restore, p_cancel);
  132. }
  133. void EditorSpatialGizmo::set_spatial_node(Spatial *p_node) {
  134. ERR_FAIL_NULL(p_node);
  135. spatial_node = p_node;
  136. }
  137. void EditorSpatialGizmo::Instance::create_instance(Spatial *p_base, bool p_hidden) {
  138. instance = VS::get_singleton()->instance_create2(mesh->get_rid(), p_base->get_world()->get_scenario());
  139. VS::get_singleton()->instance_attach_object_instance_id(instance, p_base->get_instance_id());
  140. if (skin_reference.is_valid())
  141. VS::get_singleton()->instance_attach_skeleton(instance, skin_reference->get_skeleton());
  142. if (extra_margin)
  143. VS::get_singleton()->instance_set_extra_visibility_margin(instance, 1);
  144. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(instance, VS::SHADOW_CASTING_SETTING_OFF);
  145. int layer = p_hidden ? 0 : 1 << SpatialEditorViewport::GIZMO_EDIT_LAYER;
  146. VS::get_singleton()->instance_set_layer_mask(instance, layer); //gizmos are 26
  147. }
  148. void EditorSpatialGizmo::add_mesh(const Ref<ArrayMesh> &p_mesh, bool p_billboard, const Ref<SkinReference> &p_skin_reference, const Ref<Material> &p_material) {
  149. ERR_FAIL_COND(!spatial_node);
  150. Instance ins;
  151. ins.billboard = p_billboard;
  152. ins.mesh = p_mesh;
  153. ins.skin_reference = p_skin_reference;
  154. ins.material = p_material;
  155. if (valid) {
  156. ins.create_instance(spatial_node, hidden);
  157. VS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  158. if (ins.material.is_valid()) {
  159. VS::get_singleton()->instance_geometry_set_material_override(ins.instance, p_material->get_rid());
  160. }
  161. }
  162. instances.push_back(ins);
  163. }
  164. void EditorSpatialGizmo::add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material, bool p_billboard, const Color &p_modulate) {
  165. if (p_lines.empty()) {
  166. return;
  167. }
  168. ERR_FAIL_COND(!spatial_node);
  169. Instance ins;
  170. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  171. Array a;
  172. a.resize(Mesh::ARRAY_MAX);
  173. a[Mesh::ARRAY_VERTEX] = p_lines;
  174. PoolVector<Color> color;
  175. color.resize(p_lines.size());
  176. {
  177. PoolVector<Color>::Write w = color.write();
  178. for (int i = 0; i < p_lines.size(); i++) {
  179. if (is_selected())
  180. w[i] = Color(1, 1, 1, 0.8) * p_modulate;
  181. else
  182. w[i] = Color(1, 1, 1, 0.2) * p_modulate;
  183. }
  184. }
  185. a[Mesh::ARRAY_COLOR] = color;
  186. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, a);
  187. mesh->surface_set_material(0, p_material);
  188. if (p_billboard) {
  189. float md = 0;
  190. for (int i = 0; i < p_lines.size(); i++) {
  191. md = MAX(0, p_lines[i].length());
  192. }
  193. if (md) {
  194. mesh->set_custom_aabb(AABB(Vector3(-md, -md, -md), Vector3(md, md, md) * 2.0));
  195. }
  196. }
  197. ins.billboard = p_billboard;
  198. ins.mesh = mesh;
  199. if (valid) {
  200. ins.create_instance(spatial_node, hidden);
  201. VS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  202. }
  203. instances.push_back(ins);
  204. }
  205. void EditorSpatialGizmo::add_unscaled_billboard(const Ref<Material> &p_material, float p_scale, const Color &p_modulate) {
  206. ERR_FAIL_COND(!spatial_node);
  207. Instance ins;
  208. Vector<Vector3> vs;
  209. Vector<Vector2> uv;
  210. Vector<Color> colors;
  211. vs.push_back(Vector3(-p_scale, p_scale, 0));
  212. vs.push_back(Vector3(p_scale, p_scale, 0));
  213. vs.push_back(Vector3(p_scale, -p_scale, 0));
  214. vs.push_back(Vector3(-p_scale, -p_scale, 0));
  215. uv.push_back(Vector2(0, 0));
  216. uv.push_back(Vector2(1, 0));
  217. uv.push_back(Vector2(1, 1));
  218. uv.push_back(Vector2(0, 1));
  219. colors.push_back(p_modulate);
  220. colors.push_back(p_modulate);
  221. colors.push_back(p_modulate);
  222. colors.push_back(p_modulate);
  223. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  224. Array a;
  225. a.resize(Mesh::ARRAY_MAX);
  226. a[Mesh::ARRAY_VERTEX] = vs;
  227. a[Mesh::ARRAY_TEX_UV] = uv;
  228. a[Mesh::ARRAY_COLOR] = colors;
  229. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLE_FAN, a);
  230. mesh->surface_set_material(0, p_material);
  231. float md = 0;
  232. for (int i = 0; i < vs.size(); i++) {
  233. md = MAX(0, vs[i].length());
  234. }
  235. if (md) {
  236. mesh->set_custom_aabb(AABB(Vector3(-md, -md, -md), Vector3(md, md, md) * 2.0));
  237. }
  238. selectable_icon_size = p_scale;
  239. 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));
  240. ins.mesh = mesh;
  241. ins.unscaled = true;
  242. ins.billboard = true;
  243. if (valid) {
  244. ins.create_instance(spatial_node, hidden);
  245. VS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  246. }
  247. selectable_icon_size = p_scale;
  248. instances.push_back(ins);
  249. }
  250. void EditorSpatialGizmo::add_collision_triangles(const Ref<TriangleMesh> &p_tmesh) {
  251. collision_mesh = p_tmesh;
  252. }
  253. void EditorSpatialGizmo::add_collision_segments(const Vector<Vector3> &p_lines) {
  254. int from = collision_segments.size();
  255. collision_segments.resize(from + p_lines.size());
  256. for (int i = 0; i < p_lines.size(); i++) {
  257. collision_segments.write[from + i] = p_lines[i];
  258. }
  259. }
  260. void EditorSpatialGizmo::add_handles(const Vector<Vector3> &p_handles, const Ref<Material> &p_material, bool p_billboard, bool p_secondary) {
  261. billboard_handle = p_billboard;
  262. if (!is_selected() || !is_editable())
  263. return;
  264. ERR_FAIL_COND(!spatial_node);
  265. Instance ins;
  266. Ref<ArrayMesh> mesh = memnew(ArrayMesh);
  267. Array a;
  268. a.resize(VS::ARRAY_MAX);
  269. a[VS::ARRAY_VERTEX] = p_handles;
  270. PoolVector<Color> colors;
  271. {
  272. colors.resize(p_handles.size());
  273. PoolVector<Color>::Write w = colors.write();
  274. for (int i = 0; i < p_handles.size(); i++) {
  275. Color col(1, 1, 1, 1);
  276. if (is_handle_highlighted(i))
  277. col = Color(0, 0, 1, 0.9);
  278. if (SpatialEditor::get_singleton()->get_over_gizmo_handle() != i)
  279. col.a = 0.8;
  280. w[i] = col;
  281. }
  282. }
  283. a[VS::ARRAY_COLOR] = colors;
  284. mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, a);
  285. mesh->surface_set_material(0, p_material);
  286. if (p_billboard) {
  287. float md = 0;
  288. for (int i = 0; i < p_handles.size(); i++) {
  289. md = MAX(0, p_handles[i].length());
  290. }
  291. if (md) {
  292. mesh->set_custom_aabb(AABB(Vector3(-md, -md, -md), Vector3(md, md, md) * 2.0));
  293. }
  294. }
  295. ins.mesh = mesh;
  296. ins.billboard = p_billboard;
  297. ins.extra_margin = true;
  298. if (valid) {
  299. ins.create_instance(spatial_node, hidden);
  300. VS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  301. }
  302. instances.push_back(ins);
  303. if (!p_secondary) {
  304. int chs = handles.size();
  305. handles.resize(chs + p_handles.size());
  306. for (int i = 0; i < p_handles.size(); i++) {
  307. handles.write[i + chs] = p_handles[i];
  308. }
  309. } else {
  310. int chs = secondary_handles.size();
  311. secondary_handles.resize(chs + p_handles.size());
  312. for (int i = 0; i < p_handles.size(); i++) {
  313. secondary_handles.write[i + chs] = p_handles[i];
  314. }
  315. }
  316. }
  317. void EditorSpatialGizmo::add_solid_box(Ref<Material> &p_material, Vector3 p_size, Vector3 p_position) {
  318. ERR_FAIL_COND(!spatial_node);
  319. CubeMesh cubem;
  320. cubem.set_size(p_size);
  321. Array arrays = cubem.surface_get_arrays(0);
  322. PoolVector3Array vertex = arrays[VS::ARRAY_VERTEX];
  323. PoolVector3Array::Write w = vertex.write();
  324. for (int i = 0; i < vertex.size(); ++i) {
  325. w[i] += p_position;
  326. }
  327. arrays[VS::ARRAY_VERTEX] = vertex;
  328. Ref<ArrayMesh> m = memnew(ArrayMesh);
  329. m->add_surface_from_arrays(cubem.surface_get_primitive_type(0), arrays);
  330. m->surface_set_material(0, p_material);
  331. add_mesh(m);
  332. }
  333. bool EditorSpatialGizmo::intersect_frustum(const Camera *p_camera, const Vector<Plane> &p_frustum) {
  334. ERR_FAIL_COND_V(!spatial_node, false);
  335. ERR_FAIL_COND_V(!valid, false);
  336. if (hidden && !gizmo_plugin->is_selectable_when_hidden()) return false;
  337. if (selectable_icon_size > 0.0f) {
  338. Vector3 origin = spatial_node->get_global_transform().get_origin();
  339. const Plane *p = p_frustum.ptr();
  340. int fc = p_frustum.size();
  341. bool any_out = false;
  342. for (int j = 0; j < fc; j++) {
  343. if (p[j].is_point_over(origin)) {
  344. any_out = true;
  345. break;
  346. }
  347. }
  348. return !any_out;
  349. }
  350. if (collision_segments.size()) {
  351. const Plane *p = p_frustum.ptr();
  352. int fc = p_frustum.size();
  353. int vc = collision_segments.size();
  354. const Vector3 *vptr = collision_segments.ptr();
  355. Transform t = spatial_node->get_global_transform();
  356. bool any_out = false;
  357. for (int j = 0; j < fc; j++) {
  358. for (int i = 0; i < vc; i++) {
  359. Vector3 v = t.xform(vptr[i]);
  360. if (p[j].is_point_over(v)) {
  361. any_out = true;
  362. break;
  363. }
  364. }
  365. if (any_out) break;
  366. }
  367. if (!any_out) return true;
  368. }
  369. if (collision_mesh.is_valid()) {
  370. Transform t = spatial_node->get_global_transform();
  371. Vector3 mesh_scale = t.get_basis().get_scale();
  372. t.orthonormalize();
  373. Transform it = t.affine_inverse();
  374. Vector<Plane> transformed_frustum;
  375. for (int i = 0; i < 4; i++) {
  376. transformed_frustum.push_back(it.xform(p_frustum[i]));
  377. }
  378. if (collision_mesh->inside_convex_shape(transformed_frustum.ptr(), transformed_frustum.size(), mesh_scale)) {
  379. return true;
  380. }
  381. }
  382. return false;
  383. }
  384. 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) {
  385. ERR_FAIL_COND_V(!spatial_node, false);
  386. ERR_FAIL_COND_V(!valid, false);
  387. if (hidden && !gizmo_plugin->is_selectable_when_hidden()) return false;
  388. if (r_gizmo_handle && !hidden) {
  389. Transform t = spatial_node->get_global_transform();
  390. if (billboard_handle) {
  391. t.set_look_at(t.origin, t.origin - p_camera->get_transform().basis.get_axis(2), p_camera->get_transform().basis.get_axis(1));
  392. }
  393. float min_d = 1e20;
  394. int idx = -1;
  395. for (int i = 0; i < secondary_handles.size(); i++) {
  396. Vector3 hpos = t.xform(secondary_handles[i]);
  397. Vector2 p = p_camera->unproject_position(hpos);
  398. if (p.distance_to(p_point) < HANDLE_HALF_SIZE) {
  399. real_t dp = p_camera->get_transform().origin.distance_to(hpos);
  400. if (dp < min_d) {
  401. r_pos = t.xform(hpos);
  402. r_normal = p_camera->get_transform().basis.get_axis(2);
  403. min_d = dp;
  404. idx = i + handles.size();
  405. }
  406. }
  407. }
  408. if (p_sec_first && idx != -1) {
  409. *r_gizmo_handle = idx;
  410. return true;
  411. }
  412. min_d = 1e20;
  413. for (int i = 0; i < handles.size(); i++) {
  414. Vector3 hpos = t.xform(handles[i]);
  415. Vector2 p = p_camera->unproject_position(hpos);
  416. if (p.distance_to(p_point) < HANDLE_HALF_SIZE) {
  417. real_t dp = p_camera->get_transform().origin.distance_to(hpos);
  418. if (dp < min_d) {
  419. r_pos = t.xform(hpos);
  420. r_normal = p_camera->get_transform().basis.get_axis(2);
  421. min_d = dp;
  422. idx = i;
  423. }
  424. }
  425. }
  426. if (idx >= 0) {
  427. *r_gizmo_handle = idx;
  428. return true;
  429. }
  430. }
  431. if (selectable_icon_size > 0.0f) {
  432. Transform t = spatial_node->get_global_transform();
  433. Vector3 camera_position = p_camera->get_camera_transform().origin;
  434. if (camera_position.distance_squared_to(t.origin) > 0.01) {
  435. t.set_look_at(t.origin, camera_position, Vector3(0, 1, 0));
  436. }
  437. float scale = t.origin.distance_to(p_camera->get_camera_transform().origin);
  438. if (p_camera->get_projection() == Camera::PROJECTION_ORTHOGONAL) {
  439. float aspect = p_camera->get_viewport()->get_visible_rect().size.aspect();
  440. float size = p_camera->get_size();
  441. scale = size / aspect;
  442. }
  443. Point2 center = p_camera->unproject_position(t.origin);
  444. Transform orig_camera_transform = p_camera->get_camera_transform();
  445. if (orig_camera_transform.origin.distance_squared_to(t.origin) > 0.01 &&
  446. ABS(orig_camera_transform.basis.get_axis(Vector3::AXIS_Z).dot(Vector3(0, 1, 0))) < 0.99) {
  447. p_camera->look_at(t.origin, Vector3(0, 1, 0));
  448. }
  449. Vector3 c0 = t.xform(Vector3(selectable_icon_size, selectable_icon_size, 0) * scale);
  450. Vector3 c1 = t.xform(Vector3(-selectable_icon_size, -selectable_icon_size, 0) * scale);
  451. Point2 p0 = p_camera->unproject_position(c0);
  452. Point2 p1 = p_camera->unproject_position(c1);
  453. p_camera->set_global_transform(orig_camera_transform);
  454. Rect2 rect(p0, (p1 - p0).abs());
  455. rect.set_position(center - rect.get_size() / 2.0);
  456. if (rect.has_point(p_point)) {
  457. r_pos = t.origin;
  458. r_normal = -p_camera->project_ray_normal(p_point);
  459. return true;
  460. }
  461. return false;
  462. }
  463. if (collision_segments.size()) {
  464. Plane camp(p_camera->get_transform().origin, (-p_camera->get_transform().basis.get_axis(2)).normalized());
  465. int vc = collision_segments.size();
  466. const Vector3 *vptr = collision_segments.ptr();
  467. Transform t = spatial_node->get_global_transform();
  468. if (billboard_handle) {
  469. t.set_look_at(t.origin, t.origin - p_camera->get_transform().basis.get_axis(2), p_camera->get_transform().basis.get_axis(1));
  470. }
  471. Vector3 cp;
  472. float cpd = 1e20;
  473. for (int i = 0; i < vc / 2; i++) {
  474. Vector3 a = t.xform(vptr[i * 2 + 0]);
  475. Vector3 b = t.xform(vptr[i * 2 + 1]);
  476. Vector2 s[2];
  477. s[0] = p_camera->unproject_position(a);
  478. s[1] = p_camera->unproject_position(b);
  479. Vector2 p = Geometry::get_closest_point_to_segment_2d(p_point, s);
  480. float pd = p.distance_to(p_point);
  481. if (pd < cpd) {
  482. float d = s[0].distance_to(s[1]);
  483. Vector3 tcp;
  484. if (d > 0) {
  485. float d2 = s[0].distance_to(p) / d;
  486. tcp = a + (b - a) * d2;
  487. } else {
  488. tcp = a;
  489. }
  490. if (camp.distance_to(tcp) < p_camera->get_znear())
  491. continue;
  492. cp = tcp;
  493. cpd = pd;
  494. }
  495. }
  496. if (cpd < 8) {
  497. r_pos = cp;
  498. r_normal = -p_camera->project_ray_normal(p_point);
  499. return true;
  500. }
  501. return false;
  502. }
  503. if (collision_mesh.is_valid()) {
  504. Transform gt = spatial_node->get_global_transform();
  505. if (billboard_handle) {
  506. gt.set_look_at(gt.origin, gt.origin - p_camera->get_transform().basis.get_axis(2), p_camera->get_transform().basis.get_axis(1));
  507. }
  508. Transform ai = gt.affine_inverse();
  509. Vector3 ray_from = ai.xform(p_camera->project_ray_origin(p_point));
  510. Vector3 ray_dir = ai.basis.xform(p_camera->project_ray_normal(p_point)).normalized();
  511. Vector3 rpos, rnorm;
  512. if (collision_mesh->intersect_ray(ray_from, ray_dir, rpos, rnorm)) {
  513. r_pos = gt.xform(rpos);
  514. r_normal = gt.basis.xform(rnorm).normalized();
  515. return true;
  516. }
  517. }
  518. return false;
  519. }
  520. void EditorSpatialGizmo::create() {
  521. ERR_FAIL_COND(!spatial_node);
  522. ERR_FAIL_COND(valid);
  523. valid = true;
  524. for (int i = 0; i < instances.size(); i++) {
  525. instances.write[i].create_instance(spatial_node, hidden);
  526. }
  527. transform();
  528. }
  529. void EditorSpatialGizmo::transform() {
  530. ERR_FAIL_COND(!spatial_node);
  531. ERR_FAIL_COND(!valid);
  532. for (int i = 0; i < instances.size(); i++) {
  533. VS::get_singleton()->instance_set_transform(instances[i].instance, spatial_node->get_global_transform());
  534. }
  535. }
  536. void EditorSpatialGizmo::free() {
  537. ERR_FAIL_COND(!spatial_node);
  538. ERR_FAIL_COND(!valid);
  539. for (int i = 0; i < instances.size(); i++) {
  540. if (instances[i].instance.is_valid())
  541. VS::get_singleton()->free(instances[i].instance);
  542. instances.write[i].instance = RID();
  543. }
  544. clear();
  545. valid = false;
  546. }
  547. void EditorSpatialGizmo::set_hidden(bool p_hidden) {
  548. hidden = p_hidden;
  549. int layer = hidden ? 0 : 1 << SpatialEditorViewport::GIZMO_EDIT_LAYER;
  550. for (int i = 0; i < instances.size(); ++i) {
  551. VS::get_singleton()->instance_set_layer_mask(instances[i].instance, layer);
  552. }
  553. }
  554. void EditorSpatialGizmo::set_plugin(EditorSpatialGizmoPlugin *p_plugin) {
  555. gizmo_plugin = p_plugin;
  556. }
  557. void EditorSpatialGizmo::_bind_methods() {
  558. ClassDB::bind_method(D_METHOD("add_lines", "lines", "material", "billboard", "modulate"), &EditorSpatialGizmo::add_lines, DEFVAL(false), DEFVAL(Color(1, 1, 1)));
  559. ClassDB::bind_method(D_METHOD("add_mesh", "mesh", "billboard", "skeleton", "material"), &EditorSpatialGizmo::add_mesh, DEFVAL(false), DEFVAL(Ref<SkinReference>()), DEFVAL(Variant()));
  560. ClassDB::bind_method(D_METHOD("add_collision_segments", "segments"), &EditorSpatialGizmo::add_collision_segments);
  561. ClassDB::bind_method(D_METHOD("add_collision_triangles", "triangles"), &EditorSpatialGizmo::add_collision_triangles);
  562. ClassDB::bind_method(D_METHOD("add_unscaled_billboard", "material", "default_scale", "modulate"), &EditorSpatialGizmo::add_unscaled_billboard, DEFVAL(1), DEFVAL(Color(1, 1, 1)));
  563. ClassDB::bind_method(D_METHOD("add_handles", "handles", "material", "billboard", "secondary"), &EditorSpatialGizmo::add_handles, DEFVAL(false), DEFVAL(false));
  564. ClassDB::bind_method(D_METHOD("set_spatial_node", "node"), &EditorSpatialGizmo::_set_spatial_node);
  565. ClassDB::bind_method(D_METHOD("get_spatial_node"), &EditorSpatialGizmo::get_spatial_node);
  566. ClassDB::bind_method(D_METHOD("get_plugin"), &EditorSpatialGizmo::get_plugin);
  567. ClassDB::bind_method(D_METHOD("clear"), &EditorSpatialGizmo::clear);
  568. ClassDB::bind_method(D_METHOD("set_hidden", "hidden"), &EditorSpatialGizmo::set_hidden);
  569. BIND_VMETHOD(MethodInfo("redraw"));
  570. BIND_VMETHOD(MethodInfo(Variant::STRING, "get_handle_name", PropertyInfo(Variant::INT, "index")));
  571. BIND_VMETHOD(MethodInfo(Variant::BOOL, "is_handle_highlighted", PropertyInfo(Variant::INT, "index")));
  572. MethodInfo hvget(Variant::NIL, "get_handle_value", PropertyInfo(Variant::INT, "index"));
  573. hvget.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  574. BIND_VMETHOD(hvget);
  575. BIND_VMETHOD(MethodInfo("set_handle", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::OBJECT, "camera", PROPERTY_HINT_RESOURCE_TYPE, "Camera"), PropertyInfo(Variant::VECTOR2, "point")));
  576. MethodInfo cm = MethodInfo("commit_handle", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::NIL, "restore"), PropertyInfo(Variant::BOOL, "cancel"));
  577. cm.default_arguments.push_back(false);
  578. BIND_VMETHOD(cm);
  579. }
  580. EditorSpatialGizmo::EditorSpatialGizmo() {
  581. valid = false;
  582. billboard_handle = false;
  583. hidden = false;
  584. base = NULL;
  585. selected = false;
  586. instanced = false;
  587. spatial_node = NULL;
  588. gizmo_plugin = NULL;
  589. selectable_icon_size = -1.0f;
  590. }
  591. EditorSpatialGizmo::~EditorSpatialGizmo() {
  592. if (gizmo_plugin != NULL) gizmo_plugin->unregister_gizmo(this);
  593. clear();
  594. }
  595. Vector3 EditorSpatialGizmo::get_handle_pos(int p_idx) const {
  596. ERR_FAIL_INDEX_V(p_idx, handles.size(), Vector3());
  597. return handles[p_idx];
  598. }
  599. //// light gizmo
  600. LightSpatialGizmoPlugin::LightSpatialGizmoPlugin() {
  601. // Enable vertex colors for the materials below as the gizmo color depends on the light color.
  602. create_material("lines_primary", Color(1, 1, 1), false, false, true);
  603. create_material("lines_secondary", Color(1, 1, 1, 0.35), false, false, true);
  604. create_material("lines_billboard", Color(1, 1, 1), true, false, true);
  605. create_icon_material("light_directional_icon", SpatialEditor::get_singleton()->get_icon("GizmoDirectionalLight", "EditorIcons"));
  606. create_icon_material("light_omni_icon", SpatialEditor::get_singleton()->get_icon("GizmoLight", "EditorIcons"));
  607. create_icon_material("light_spot_icon", SpatialEditor::get_singleton()->get_icon("GizmoSpotLight", "EditorIcons"));
  608. create_handle_material("handles");
  609. create_handle_material("handles_billboard", true);
  610. }
  611. bool LightSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  612. return Object::cast_to<Light>(p_spatial) != NULL;
  613. }
  614. String LightSpatialGizmoPlugin::get_name() const {
  615. return "Lights";
  616. }
  617. int LightSpatialGizmoPlugin::get_priority() const {
  618. return -1;
  619. }
  620. String LightSpatialGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  621. if (p_idx == 0)
  622. return "Radius";
  623. else
  624. return "Aperture";
  625. }
  626. Variant LightSpatialGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  627. Light *light = Object::cast_to<Light>(p_gizmo->get_spatial_node());
  628. if (p_idx == 0)
  629. return light->get_param(Light::PARAM_RANGE);
  630. if (p_idx == 1)
  631. return light->get_param(Light::PARAM_SPOT_ANGLE);
  632. return Variant();
  633. }
  634. 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) {
  635. //bleh, discrete is simpler
  636. static const int arc_test_points = 64;
  637. float min_d = 1e20;
  638. Vector3 min_p;
  639. for (int i = 0; i < arc_test_points; i++) {
  640. float a = i * Math_PI * 0.5 / arc_test_points;
  641. float an = (i + 1) * Math_PI * 0.5 / arc_test_points;
  642. Vector3 p = Vector3(Math::cos(a), 0, -Math::sin(a)) * p_arc_radius;
  643. Vector3 n = Vector3(Math::cos(an), 0, -Math::sin(an)) * p_arc_radius;
  644. Vector3 ra, rb;
  645. Geometry::get_closest_points_between_segments(p, n, p_from, p_to, ra, rb);
  646. float d = ra.distance_to(rb);
  647. if (d < min_d) {
  648. min_d = d;
  649. min_p = ra;
  650. }
  651. }
  652. //min_p = p_arc_xform.affine_inverse().xform(min_p);
  653. float a = (Math_PI * 0.5) - Vector2(min_p.x, -min_p.z).angle();
  654. return a * 180.0 / Math_PI;
  655. }
  656. void LightSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  657. Light *light = Object::cast_to<Light>(p_gizmo->get_spatial_node());
  658. Transform gt = light->get_global_transform();
  659. Transform gi = gt.affine_inverse();
  660. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  661. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  662. Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  663. if (p_idx == 0) {
  664. if (Object::cast_to<SpotLight>(light)) {
  665. Vector3 ra, rb;
  666. Geometry::get_closest_points_between_segments(Vector3(), Vector3(0, 0, -4096), s[0], s[1], ra, rb);
  667. float d = -ra.z;
  668. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  669. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  670. }
  671. if (d <= 0) // Equal is here for negative zero.
  672. d = 0;
  673. light->set_param(Light::PARAM_RANGE, d);
  674. } else if (Object::cast_to<OmniLight>(light)) {
  675. Plane cp = Plane(gt.origin, p_camera->get_transform().basis.get_axis(2));
  676. Vector3 inters;
  677. if (cp.intersects_ray(ray_from, ray_dir, &inters)) {
  678. float r = inters.distance_to(gt.origin);
  679. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  680. r = Math::stepify(r, SpatialEditor::get_singleton()->get_translate_snap());
  681. }
  682. light->set_param(Light::PARAM_RANGE, r);
  683. }
  684. }
  685. } else if (p_idx == 1) {
  686. float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], light->get_param(Light::PARAM_RANGE), gt);
  687. light->set_param(Light::PARAM_SPOT_ANGLE, CLAMP(a, 0.01, 89.99));
  688. }
  689. }
  690. void LightSpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  691. Light *light = Object::cast_to<Light>(p_gizmo->get_spatial_node());
  692. if (p_cancel) {
  693. light->set_param(p_idx == 0 ? Light::PARAM_RANGE : Light::PARAM_SPOT_ANGLE, p_restore);
  694. } else if (p_idx == 0) {
  695. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  696. ur->create_action(TTR("Change Light Radius"));
  697. ur->add_do_method(light, "set_param", Light::PARAM_RANGE, light->get_param(Light::PARAM_RANGE));
  698. ur->add_undo_method(light, "set_param", Light::PARAM_RANGE, p_restore);
  699. ur->commit_action();
  700. } else if (p_idx == 1) {
  701. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  702. ur->create_action(TTR("Change Light Radius"));
  703. ur->add_do_method(light, "set_param", Light::PARAM_SPOT_ANGLE, light->get_param(Light::PARAM_SPOT_ANGLE));
  704. ur->add_undo_method(light, "set_param", Light::PARAM_SPOT_ANGLE, p_restore);
  705. ur->commit_action();
  706. }
  707. }
  708. void LightSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  709. Light *light = Object::cast_to<Light>(p_gizmo->get_spatial_node());
  710. Color color = light->get_color();
  711. // Make the gizmo color as bright as possible for better visibility
  712. color.set_hsv(color.get_h(), color.get_s(), 1);
  713. p_gizmo->clear();
  714. if (Object::cast_to<DirectionalLight>(light)) {
  715. Ref<Material> material = get_material("lines_primary", p_gizmo);
  716. Ref<Material> icon = get_material("light_directional_icon", p_gizmo);
  717. const int arrow_points = 7;
  718. const float arrow_length = 1.5;
  719. Vector3 arrow[arrow_points] = {
  720. Vector3(0, 0, -1),
  721. Vector3(0, 0.8, 0),
  722. Vector3(0, 0.3, 0),
  723. Vector3(0, 0.3, arrow_length),
  724. Vector3(0, -0.3, arrow_length),
  725. Vector3(0, -0.3, 0),
  726. Vector3(0, -0.8, 0)
  727. };
  728. int arrow_sides = 2;
  729. Vector<Vector3> lines;
  730. for (int i = 0; i < arrow_sides; i++) {
  731. for (int j = 0; j < arrow_points; j++) {
  732. Basis ma(Vector3(0, 0, 1), Math_PI * i / arrow_sides);
  733. Vector3 v1 = arrow[j] - Vector3(0, 0, arrow_length);
  734. Vector3 v2 = arrow[(j + 1) % arrow_points] - Vector3(0, 0, arrow_length);
  735. lines.push_back(ma.xform(v1));
  736. lines.push_back(ma.xform(v2));
  737. }
  738. }
  739. p_gizmo->add_lines(lines, material, false, color);
  740. p_gizmo->add_unscaled_billboard(icon, 0.05, color);
  741. }
  742. if (Object::cast_to<OmniLight>(light)) {
  743. // Use both a billboard circle and 3 non-billboard circles for a better sphere-like representation
  744. const Ref<Material> lines_material = get_material("lines_secondary", p_gizmo);
  745. const Ref<Material> lines_billboard_material = get_material("lines_billboard", p_gizmo);
  746. const Ref<Material> icon = get_material("light_omni_icon", p_gizmo);
  747. OmniLight *on = Object::cast_to<OmniLight>(light);
  748. const float r = on->get_param(Light::PARAM_RANGE);
  749. Vector<Vector3> points;
  750. Vector<Vector3> points_billboard;
  751. for (int i = 0; i < 120; i++) {
  752. // Create a circle
  753. const float ra = Math::deg2rad((float)(i * 3));
  754. const float rb = Math::deg2rad((float)((i + 1) * 3));
  755. const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  756. const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  757. // Draw axis-aligned circles
  758. points.push_back(Vector3(a.x, 0, a.y));
  759. points.push_back(Vector3(b.x, 0, b.y));
  760. points.push_back(Vector3(0, a.x, a.y));
  761. points.push_back(Vector3(0, b.x, b.y));
  762. points.push_back(Vector3(a.x, a.y, 0));
  763. points.push_back(Vector3(b.x, b.y, 0));
  764. // Draw a billboarded circle
  765. points_billboard.push_back(Vector3(a.x, a.y, 0));
  766. points_billboard.push_back(Vector3(b.x, b.y, 0));
  767. }
  768. p_gizmo->add_lines(points, lines_material, true, color);
  769. p_gizmo->add_lines(points_billboard, lines_billboard_material, true, color);
  770. p_gizmo->add_unscaled_billboard(icon, 0.05, color);
  771. Vector<Vector3> handles;
  772. handles.push_back(Vector3(r, 0, 0));
  773. p_gizmo->add_handles(handles, get_material("handles_billboard"), true);
  774. }
  775. if (Object::cast_to<SpotLight>(light)) {
  776. const Ref<Material> material_primary = get_material("lines_primary", p_gizmo);
  777. const Ref<Material> material_secondary = get_material("lines_secondary", p_gizmo);
  778. const Ref<Material> icon = get_material("light_spot_icon", p_gizmo);
  779. Vector<Vector3> points_primary;
  780. Vector<Vector3> points_secondary;
  781. SpotLight *sl = Object::cast_to<SpotLight>(light);
  782. float r = sl->get_param(Light::PARAM_RANGE);
  783. float w = r * Math::sin(Math::deg2rad(sl->get_param(Light::PARAM_SPOT_ANGLE)));
  784. float d = r * Math::cos(Math::deg2rad(sl->get_param(Light::PARAM_SPOT_ANGLE)));
  785. for (int i = 0; i < 120; i++) {
  786. // Draw a circle
  787. const float ra = Math::deg2rad((float)(i * 3));
  788. const float rb = Math::deg2rad((float)((i + 1) * 3));
  789. const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;
  790. const Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;
  791. points_primary.push_back(Vector3(a.x, a.y, -d));
  792. points_primary.push_back(Vector3(b.x, b.y, -d));
  793. if (i % 15 == 0) {
  794. // Draw 8 lines from the cone origin to the sides of the circle
  795. points_secondary.push_back(Vector3(a.x, a.y, -d));
  796. points_secondary.push_back(Vector3());
  797. }
  798. }
  799. points_primary.push_back(Vector3(0, 0, -r));
  800. points_primary.push_back(Vector3());
  801. p_gizmo->add_lines(points_primary, material_primary, false, color);
  802. p_gizmo->add_lines(points_secondary, material_secondary, false, color);
  803. const float ra = 16 * Math_PI * 2.0 / 64.0;
  804. const Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;
  805. Vector<Vector3> handles;
  806. handles.push_back(Vector3(0, 0, -r));
  807. handles.push_back(Vector3(a.x, a.y, -d));
  808. p_gizmo->add_handles(handles, get_material("handles"));
  809. p_gizmo->add_unscaled_billboard(icon, 0.05, color);
  810. }
  811. }
  812. //////
  813. //// player gizmo
  814. AudioStreamPlayer3DSpatialGizmoPlugin::AudioStreamPlayer3DSpatialGizmoPlugin() {
  815. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/stream_player_3d", Color(0.4, 0.8, 1));
  816. create_icon_material("stream_player_3d_icon", SpatialEditor::get_singleton()->get_icon("GizmoSpatialSamplePlayer", "EditorIcons"));
  817. create_material("stream_player_3d_material_primary", gizmo_color);
  818. create_material("stream_player_3d_material_secondary", gizmo_color * Color(1, 1, 1, 0.35));
  819. create_handle_material("handles");
  820. }
  821. bool AudioStreamPlayer3DSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  822. return Object::cast_to<AudioStreamPlayer3D>(p_spatial) != NULL;
  823. }
  824. String AudioStreamPlayer3DSpatialGizmoPlugin::get_name() const {
  825. return "AudioStreamPlayer3D";
  826. }
  827. int AudioStreamPlayer3DSpatialGizmoPlugin::get_priority() const {
  828. return -1;
  829. }
  830. String AudioStreamPlayer3DSpatialGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  831. return "Emission Radius";
  832. }
  833. Variant AudioStreamPlayer3DSpatialGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  834. AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_spatial_node());
  835. return player->get_emission_angle();
  836. }
  837. void AudioStreamPlayer3DSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  838. AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_spatial_node());
  839. Transform gt = player->get_global_transform();
  840. Transform gi = gt.affine_inverse();
  841. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  842. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  843. Vector3 ray_to = ray_from + ray_dir * 4096;
  844. ray_from = gi.xform(ray_from);
  845. ray_to = gi.xform(ray_to);
  846. float closest_dist = 1e20;
  847. float closest_angle = 1e20;
  848. for (int i = 0; i < 180; i++) {
  849. float a = i * Math_PI / 180.0;
  850. float an = (i + 1) * Math_PI / 180.0;
  851. Vector3 from(Math::sin(a), 0, -Math::cos(a));
  852. Vector3 to(Math::sin(an), 0, -Math::cos(an));
  853. Vector3 r1, r2;
  854. Geometry::get_closest_points_between_segments(from, to, ray_from, ray_to, r1, r2);
  855. float d = r1.distance_to(r2);
  856. if (d < closest_dist) {
  857. closest_dist = d;
  858. closest_angle = i;
  859. }
  860. }
  861. if (closest_angle < 91) {
  862. player->set_emission_angle(closest_angle);
  863. }
  864. }
  865. void AudioStreamPlayer3DSpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  866. AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_spatial_node());
  867. if (p_cancel) {
  868. player->set_emission_angle(p_restore);
  869. } else {
  870. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  871. ur->create_action(TTR("Change AudioStreamPlayer3D Emission Angle"));
  872. ur->add_do_method(player, "set_emission_angle", player->get_emission_angle());
  873. ur->add_undo_method(player, "set_emission_angle", p_restore);
  874. ur->commit_action();
  875. }
  876. }
  877. void AudioStreamPlayer3DSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  878. const AudioStreamPlayer3D *player = Object::cast_to<AudioStreamPlayer3D>(p_gizmo->get_spatial_node());
  879. p_gizmo->clear();
  880. const Ref<Material> icon = get_material("stream_player_3d_icon", p_gizmo);
  881. if (player->is_emission_angle_enabled()) {
  882. const float pc = player->get_emission_angle();
  883. const float ofs = -Math::cos(Math::deg2rad(pc));
  884. const float radius = Math::sin(Math::deg2rad(pc));
  885. Vector<Vector3> points_primary;
  886. points_primary.resize(200);
  887. for (int i = 0; i < 100; i++) {
  888. const float a = i * 2.0 * Math_PI / 100.0;
  889. const float an = (i + 1) * 2.0 * Math_PI / 100.0;
  890. const Vector3 from(Math::sin(a) * radius, Math::cos(a) * radius, ofs);
  891. const Vector3 to(Math::sin(an) * radius, Math::cos(an) * radius, ofs);
  892. points_primary.write[i * 2 + 0] = from;
  893. points_primary.write[i * 2 + 1] = to;
  894. }
  895. const Ref<Material> material_primary = get_material("stream_player_3d_material_primary", p_gizmo);
  896. p_gizmo->add_lines(points_primary, material_primary);
  897. Vector<Vector3> points_secondary;
  898. points_secondary.resize(16);
  899. for (int i = 0; i < 8; i++) {
  900. const float a = i * 2.0 * Math_PI / 8.0;
  901. const Vector3 from(Math::sin(a) * radius, Math::cos(a) * radius, ofs);
  902. points_secondary.write[i * 2 + 0] = from;
  903. points_secondary.write[i * 2 + 1] = Vector3();
  904. }
  905. const Ref<Material> material_secondary = get_material("stream_player_3d_material_secondary", p_gizmo);
  906. p_gizmo->add_lines(points_secondary, material_secondary);
  907. Vector<Vector3> handles;
  908. const float ha = Math::deg2rad(player->get_emission_angle());
  909. handles.push_back(Vector3(Math::sin(ha), 0, -Math::cos(ha)));
  910. p_gizmo->add_handles(handles, get_material("handles"));
  911. }
  912. p_gizmo->add_unscaled_billboard(icon, 0.05);
  913. }
  914. //////
  915. CameraSpatialGizmoPlugin::CameraSpatialGizmoPlugin() {
  916. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/camera", Color(0.8, 0.4, 0.8));
  917. create_material("camera_material", gizmo_color);
  918. create_icon_material("camera_icon", SpatialEditor::get_singleton()->get_icon("GizmoCamera", "EditorIcons"));
  919. create_handle_material("handles");
  920. }
  921. bool CameraSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  922. return Object::cast_to<Camera>(p_spatial) != NULL;
  923. }
  924. String CameraSpatialGizmoPlugin::get_name() const {
  925. return "Camera";
  926. }
  927. int CameraSpatialGizmoPlugin::get_priority() const {
  928. return -1;
  929. }
  930. String CameraSpatialGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  931. Camera *camera = Object::cast_to<Camera>(p_gizmo->get_spatial_node());
  932. if (camera->get_projection() == Camera::PROJECTION_PERSPECTIVE) {
  933. return "FOV";
  934. } else {
  935. return "Size";
  936. }
  937. }
  938. Variant CameraSpatialGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  939. Camera *camera = Object::cast_to<Camera>(p_gizmo->get_spatial_node());
  940. if (camera->get_projection() == Camera::PROJECTION_PERSPECTIVE) {
  941. return camera->get_fov();
  942. } else {
  943. return camera->get_size();
  944. }
  945. }
  946. void CameraSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  947. Camera *camera = Object::cast_to<Camera>(p_gizmo->get_spatial_node());
  948. Transform gt = camera->get_global_transform();
  949. Transform gi = gt.affine_inverse();
  950. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  951. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  952. Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  953. if (camera->get_projection() == Camera::PROJECTION_PERSPECTIVE) {
  954. Transform gt2 = camera->get_global_transform();
  955. float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], 1.0, gt2);
  956. camera->set("fov", CLAMP(a * 2.0, 1, 179));
  957. } else {
  958. Vector3 ra, rb;
  959. Geometry::get_closest_points_between_segments(Vector3(0, 0, -1), Vector3(4096, 0, -1), s[0], s[1], ra, rb);
  960. float d = ra.x * 2.0;
  961. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  962. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  963. }
  964. d = CLAMP(d, 0.1, 16384);
  965. camera->set("size", d);
  966. }
  967. }
  968. void CameraSpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  969. Camera *camera = Object::cast_to<Camera>(p_gizmo->get_spatial_node());
  970. if (camera->get_projection() == Camera::PROJECTION_PERSPECTIVE) {
  971. if (p_cancel) {
  972. camera->set("fov", p_restore);
  973. } else {
  974. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  975. ur->create_action(TTR("Change Camera FOV"));
  976. ur->add_do_property(camera, "fov", camera->get_fov());
  977. ur->add_undo_property(camera, "fov", p_restore);
  978. ur->commit_action();
  979. }
  980. } else {
  981. if (p_cancel) {
  982. camera->set("size", p_restore);
  983. } else {
  984. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  985. ur->create_action(TTR("Change Camera Size"));
  986. ur->add_do_property(camera, "size", camera->get_size());
  987. ur->add_undo_property(camera, "size", p_restore);
  988. ur->commit_action();
  989. }
  990. }
  991. }
  992. void CameraSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  993. Camera *camera = Object::cast_to<Camera>(p_gizmo->get_spatial_node());
  994. p_gizmo->clear();
  995. Vector<Vector3> lines;
  996. Vector<Vector3> handles;
  997. Ref<Material> material = get_material("camera_material", p_gizmo);
  998. Ref<Material> icon = get_material("camera_icon", p_gizmo);
  999. #define ADD_TRIANGLE(m_a, m_b, m_c) \
  1000. { \
  1001. lines.push_back(m_a); \
  1002. lines.push_back(m_b); \
  1003. lines.push_back(m_b); \
  1004. lines.push_back(m_c); \
  1005. lines.push_back(m_c); \
  1006. lines.push_back(m_a); \
  1007. }
  1008. #define ADD_QUAD(m_a, m_b, m_c, m_d) \
  1009. { \
  1010. lines.push_back(m_a); \
  1011. lines.push_back(m_b); \
  1012. lines.push_back(m_b); \
  1013. lines.push_back(m_c); \
  1014. lines.push_back(m_c); \
  1015. lines.push_back(m_d); \
  1016. lines.push_back(m_d); \
  1017. lines.push_back(m_a); \
  1018. }
  1019. switch (camera->get_projection()) {
  1020. case Camera::PROJECTION_PERSPECTIVE: {
  1021. // The real FOV is halved for accurate representation
  1022. float fov = camera->get_fov() / 2.0;
  1023. Vector3 side = Vector3(Math::sin(Math::deg2rad(fov)), 0, -Math::cos(Math::deg2rad(fov)));
  1024. Vector3 nside = side;
  1025. nside.x = -nside.x;
  1026. Vector3 up = Vector3(0, side.x, 0);
  1027. ADD_TRIANGLE(Vector3(), side + up, side - up);
  1028. ADD_TRIANGLE(Vector3(), nside + up, nside - up);
  1029. ADD_TRIANGLE(Vector3(), side + up, nside + up);
  1030. ADD_TRIANGLE(Vector3(), side - up, nside - up);
  1031. handles.push_back(side);
  1032. side.x *= 0.25;
  1033. nside.x *= 0.25;
  1034. Vector3 tup(0, up.y * 3 / 2, side.z);
  1035. ADD_TRIANGLE(tup, side + up, nside + up);
  1036. } break;
  1037. case Camera::PROJECTION_ORTHOGONAL: {
  1038. float size = camera->get_size();
  1039. float hsize = size * 0.5;
  1040. Vector3 right(hsize, 0, 0);
  1041. Vector3 up(0, hsize, 0);
  1042. Vector3 back(0, 0, -1.0);
  1043. Vector3 front(0, 0, 0);
  1044. ADD_QUAD(-up - right, -up + right, up + right, up - right);
  1045. ADD_QUAD(-up - right + back, -up + right + back, up + right + back, up - right + back);
  1046. ADD_QUAD(up + right, up + right + back, up - right + back, up - right);
  1047. ADD_QUAD(-up + right, -up + right + back, -up - right + back, -up - right);
  1048. handles.push_back(right + back);
  1049. right.x *= 0.25;
  1050. Vector3 tup(0, up.y * 3 / 2, back.z);
  1051. ADD_TRIANGLE(tup, right + up + back, -right + up + back);
  1052. } break;
  1053. case Camera::PROJECTION_FRUSTUM: {
  1054. float hsize = camera->get_size() / 2.0;
  1055. Vector3 side = Vector3(hsize, 0, -camera->get_znear()).normalized();
  1056. Vector3 nside = side;
  1057. nside.x = -nside.x;
  1058. Vector3 up = Vector3(0, side.x, 0);
  1059. Vector3 offset = Vector3(camera->get_frustum_offset().x, camera->get_frustum_offset().y, 0.0);
  1060. ADD_TRIANGLE(Vector3(), side + up + offset, side - up + offset);
  1061. ADD_TRIANGLE(Vector3(), nside + up + offset, nside - up + offset);
  1062. ADD_TRIANGLE(Vector3(), side + up + offset, nside + up + offset);
  1063. ADD_TRIANGLE(Vector3(), side - up + offset, nside - up + offset);
  1064. side.x *= 0.25;
  1065. nside.x *= 0.25;
  1066. Vector3 tup(0, up.y * 3 / 2, side.z);
  1067. ADD_TRIANGLE(tup + offset, side + up + offset, nside + up + offset);
  1068. }
  1069. }
  1070. #undef ADD_TRIANGLE
  1071. #undef ADD_QUAD
  1072. p_gizmo->add_lines(lines, material);
  1073. p_gizmo->add_unscaled_billboard(icon, 0.05);
  1074. p_gizmo->add_handles(handles, get_material("handles"));
  1075. ClippedCamera *clipcam = Object::cast_to<ClippedCamera>(camera);
  1076. if (clipcam) {
  1077. Spatial *parent = Object::cast_to<Spatial>(camera->get_parent());
  1078. if (!parent) {
  1079. return;
  1080. }
  1081. Vector3 cam_normal = -camera->get_global_transform().basis.get_axis(Vector3::AXIS_Z).normalized();
  1082. Vector3 cam_x = camera->get_global_transform().basis.get_axis(Vector3::AXIS_X).normalized();
  1083. Vector3 cam_y = camera->get_global_transform().basis.get_axis(Vector3::AXIS_Y).normalized();
  1084. Vector3 cam_pos = camera->get_global_transform().origin;
  1085. Vector3 parent_pos = parent->get_global_transform().origin;
  1086. Plane parent_plane(parent_pos, cam_normal);
  1087. Vector3 ray_from = parent_plane.project(cam_pos);
  1088. lines.clear();
  1089. lines.push_back(ray_from + cam_x * 0.5 + cam_y * 0.5);
  1090. lines.push_back(ray_from + cam_x * 0.5 + cam_y * -0.5);
  1091. lines.push_back(ray_from + cam_x * 0.5 + cam_y * -0.5);
  1092. lines.push_back(ray_from + cam_x * -0.5 + cam_y * -0.5);
  1093. lines.push_back(ray_from + cam_x * -0.5 + cam_y * -0.5);
  1094. lines.push_back(ray_from + cam_x * -0.5 + cam_y * 0.5);
  1095. lines.push_back(ray_from + cam_x * -0.5 + cam_y * 0.5);
  1096. lines.push_back(ray_from + cam_x * 0.5 + cam_y * 0.5);
  1097. if (parent_plane.distance_to(cam_pos) < 0) {
  1098. lines.push_back(ray_from);
  1099. lines.push_back(cam_pos);
  1100. }
  1101. Transform local = camera->get_global_transform().affine_inverse();
  1102. for (int i = 0; i < lines.size(); i++) {
  1103. lines.write[i] = local.xform(lines[i]);
  1104. }
  1105. p_gizmo->add_lines(lines, material);
  1106. }
  1107. }
  1108. //////
  1109. MeshInstanceSpatialGizmoPlugin::MeshInstanceSpatialGizmoPlugin() {
  1110. }
  1111. bool MeshInstanceSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1112. return Object::cast_to<MeshInstance>(p_spatial) != NULL && Object::cast_to<SoftBody>(p_spatial) == NULL;
  1113. }
  1114. String MeshInstanceSpatialGizmoPlugin::get_name() const {
  1115. return "MeshInstance";
  1116. }
  1117. int MeshInstanceSpatialGizmoPlugin::get_priority() const {
  1118. return -1;
  1119. }
  1120. bool MeshInstanceSpatialGizmoPlugin::can_be_hidden() const {
  1121. return false;
  1122. }
  1123. void MeshInstanceSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1124. MeshInstance *mesh = Object::cast_to<MeshInstance>(p_gizmo->get_spatial_node());
  1125. p_gizmo->clear();
  1126. Ref<Mesh> m = mesh->get_mesh();
  1127. if (!m.is_valid())
  1128. return; //none
  1129. Ref<TriangleMesh> tm = m->generate_triangle_mesh();
  1130. if (tm.is_valid()) {
  1131. p_gizmo->add_collision_triangles(tm);
  1132. }
  1133. }
  1134. /////
  1135. Sprite3DSpatialGizmoPlugin::Sprite3DSpatialGizmoPlugin() {
  1136. }
  1137. bool Sprite3DSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1138. return Object::cast_to<Sprite3D>(p_spatial) != NULL;
  1139. }
  1140. String Sprite3DSpatialGizmoPlugin::get_name() const {
  1141. return "Sprite3D";
  1142. }
  1143. int Sprite3DSpatialGizmoPlugin::get_priority() const {
  1144. return -1;
  1145. }
  1146. bool Sprite3DSpatialGizmoPlugin::can_be_hidden() const {
  1147. return false;
  1148. }
  1149. void Sprite3DSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1150. Sprite3D *sprite = Object::cast_to<Sprite3D>(p_gizmo->get_spatial_node());
  1151. p_gizmo->clear();
  1152. Ref<TriangleMesh> tm = sprite->generate_triangle_mesh();
  1153. if (tm.is_valid()) {
  1154. p_gizmo->add_collision_triangles(tm);
  1155. }
  1156. }
  1157. ///
  1158. Position3DSpatialGizmoPlugin::Position3DSpatialGizmoPlugin() {
  1159. pos3d_mesh = Ref<ArrayMesh>(memnew(ArrayMesh));
  1160. cursor_points = Vector<Vector3>();
  1161. PoolVector<Color> cursor_colors;
  1162. float cs = 0.25;
  1163. cursor_points.push_back(Vector3(+cs, 0, 0));
  1164. cursor_points.push_back(Vector3(-cs, 0, 0));
  1165. cursor_points.push_back(Vector3(0, +cs, 0));
  1166. cursor_points.push_back(Vector3(0, -cs, 0));
  1167. cursor_points.push_back(Vector3(0, 0, +cs));
  1168. cursor_points.push_back(Vector3(0, 0, -cs));
  1169. cursor_colors.push_back(EditorNode::get_singleton()->get_gui_base()->get_color("axis_x_color", "Editor"));
  1170. cursor_colors.push_back(EditorNode::get_singleton()->get_gui_base()->get_color("axis_x_color", "Editor"));
  1171. cursor_colors.push_back(EditorNode::get_singleton()->get_gui_base()->get_color("axis_y_color", "Editor"));
  1172. cursor_colors.push_back(EditorNode::get_singleton()->get_gui_base()->get_color("axis_y_color", "Editor"));
  1173. cursor_colors.push_back(EditorNode::get_singleton()->get_gui_base()->get_color("axis_z_color", "Editor"));
  1174. cursor_colors.push_back(EditorNode::get_singleton()->get_gui_base()->get_color("axis_z_color", "Editor"));
  1175. Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
  1176. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  1177. mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  1178. mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  1179. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  1180. mat->set_line_width(3);
  1181. Array d;
  1182. d.resize(VS::ARRAY_MAX);
  1183. d[Mesh::ARRAY_VERTEX] = cursor_points;
  1184. d[Mesh::ARRAY_COLOR] = cursor_colors;
  1185. pos3d_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, d);
  1186. pos3d_mesh->surface_set_material(0, mat);
  1187. }
  1188. bool Position3DSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1189. return Object::cast_to<Position3D>(p_spatial) != NULL;
  1190. }
  1191. String Position3DSpatialGizmoPlugin::get_name() const {
  1192. return "Position3D";
  1193. }
  1194. int Position3DSpatialGizmoPlugin::get_priority() const {
  1195. return -1;
  1196. }
  1197. void Position3DSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1198. p_gizmo->clear();
  1199. p_gizmo->add_mesh(pos3d_mesh);
  1200. p_gizmo->add_collision_segments(cursor_points);
  1201. }
  1202. /////
  1203. SkeletonSpatialGizmoPlugin::SkeletonSpatialGizmoPlugin() {
  1204. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/skeleton", Color(1, 0.8, 0.4));
  1205. create_material("skeleton_material", gizmo_color);
  1206. }
  1207. bool SkeletonSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1208. return Object::cast_to<Skeleton>(p_spatial) != NULL;
  1209. }
  1210. String SkeletonSpatialGizmoPlugin::get_name() const {
  1211. return "Skeleton";
  1212. }
  1213. int SkeletonSpatialGizmoPlugin::get_priority() const {
  1214. return -1;
  1215. }
  1216. void SkeletonSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1217. Skeleton *skel = Object::cast_to<Skeleton>(p_gizmo->get_spatial_node());
  1218. p_gizmo->clear();
  1219. Ref<Material> material = get_material("skeleton_material", p_gizmo);
  1220. Ref<SurfaceTool> surface_tool(memnew(SurfaceTool));
  1221. surface_tool->begin(Mesh::PRIMITIVE_LINES);
  1222. surface_tool->set_material(material);
  1223. Vector<Transform> grests;
  1224. grests.resize(skel->get_bone_count());
  1225. Vector<int> bones;
  1226. Vector<float> weights;
  1227. bones.resize(4);
  1228. weights.resize(4);
  1229. for (int i = 0; i < 4; i++) {
  1230. bones.write[i] = 0;
  1231. weights.write[i] = 0;
  1232. }
  1233. weights.write[0] = 1;
  1234. AABB aabb;
  1235. Color bonecolor = Color(1.0, 0.4, 0.4, 0.3);
  1236. Color rootcolor = Color(0.4, 1.0, 0.4, 0.1);
  1237. for (int i_bone = 0; i_bone < skel->get_bone_count(); i_bone++) {
  1238. int i = skel->get_process_order(i_bone);
  1239. int parent = skel->get_bone_parent(i);
  1240. if (parent >= 0) {
  1241. grests.write[i] = grests[parent] * skel->get_bone_rest(i);
  1242. Vector3 v0 = grests[parent].origin;
  1243. Vector3 v1 = grests[i].origin;
  1244. Vector3 d = (v1 - v0).normalized();
  1245. float dist = v0.distance_to(v1);
  1246. //find closest axis
  1247. int closest = -1;
  1248. float closest_d = 0.0;
  1249. for (int j = 0; j < 3; j++) {
  1250. float dp = Math::abs(grests[parent].basis[j].normalized().dot(d));
  1251. if (j == 0 || dp > closest_d)
  1252. closest = j;
  1253. }
  1254. //find closest other
  1255. Vector3 first;
  1256. Vector3 points[4];
  1257. int pointidx = 0;
  1258. for (int j = 0; j < 3; j++) {
  1259. bones.write[0] = parent;
  1260. surface_tool->add_bones(bones);
  1261. surface_tool->add_weights(weights);
  1262. surface_tool->add_color(rootcolor);
  1263. surface_tool->add_vertex(v0 - grests[parent].basis[j].normalized() * dist * 0.05);
  1264. surface_tool->add_bones(bones);
  1265. surface_tool->add_weights(weights);
  1266. surface_tool->add_color(rootcolor);
  1267. surface_tool->add_vertex(v0 + grests[parent].basis[j].normalized() * dist * 0.05);
  1268. if (j == closest)
  1269. continue;
  1270. Vector3 axis;
  1271. if (first == Vector3()) {
  1272. axis = d.cross(d.cross(grests[parent].basis[j])).normalized();
  1273. first = axis;
  1274. } else {
  1275. axis = d.cross(first).normalized();
  1276. }
  1277. for (int k = 0; k < 2; k++) {
  1278. if (k == 1)
  1279. axis = -axis;
  1280. Vector3 point = v0 + d * dist * 0.2;
  1281. point += axis * dist * 0.1;
  1282. bones.write[0] = parent;
  1283. surface_tool->add_bones(bones);
  1284. surface_tool->add_weights(weights);
  1285. surface_tool->add_color(bonecolor);
  1286. surface_tool->add_vertex(v0);
  1287. surface_tool->add_bones(bones);
  1288. surface_tool->add_weights(weights);
  1289. surface_tool->add_color(bonecolor);
  1290. surface_tool->add_vertex(point);
  1291. bones.write[0] = parent;
  1292. surface_tool->add_bones(bones);
  1293. surface_tool->add_weights(weights);
  1294. surface_tool->add_color(bonecolor);
  1295. surface_tool->add_vertex(point);
  1296. bones.write[0] = i;
  1297. surface_tool->add_bones(bones);
  1298. surface_tool->add_weights(weights);
  1299. surface_tool->add_color(bonecolor);
  1300. surface_tool->add_vertex(v1);
  1301. points[pointidx++] = point;
  1302. }
  1303. }
  1304. SWAP(points[1], points[2]);
  1305. for (int j = 0; j < 4; j++) {
  1306. bones.write[0] = parent;
  1307. surface_tool->add_bones(bones);
  1308. surface_tool->add_weights(weights);
  1309. surface_tool->add_color(bonecolor);
  1310. surface_tool->add_vertex(points[j]);
  1311. surface_tool->add_bones(bones);
  1312. surface_tool->add_weights(weights);
  1313. surface_tool->add_color(bonecolor);
  1314. surface_tool->add_vertex(points[(j + 1) % 4]);
  1315. }
  1316. /*
  1317. bones[0]=parent;
  1318. surface_tool->add_bones(bones);
  1319. surface_tool->add_weights(weights);
  1320. surface_tool->add_color(Color(0.4,1,0.4,0.4));
  1321. surface_tool->add_vertex(v0);
  1322. bones[0]=i;
  1323. surface_tool->add_bones(bones);
  1324. surface_tool->add_weights(weights);
  1325. surface_tool->add_color(Color(0.4,1,0.4,0.4));
  1326. surface_tool->add_vertex(v1);
  1327. */
  1328. } else {
  1329. grests.write[i] = skel->get_bone_rest(i);
  1330. bones.write[0] = i;
  1331. }
  1332. /*
  1333. Transform t = grests[i];
  1334. t.orthonormalize();
  1335. for (int i=0;i<6;i++) {
  1336. Vector3 face_points[4];
  1337. for (int j=0;j<4;j++) {
  1338. float v[3];
  1339. v[0]=1.0;
  1340. v[1]=1-2*((j>>1)&1);
  1341. v[2]=v[1]*(1-2*(j&1));
  1342. for (int k=0;k<3;k++) {
  1343. if (i<3)
  1344. face_points[j][(i+k)%3]=v[k]*(i>=3?-1:1);
  1345. else
  1346. face_points[3-j][(i+k)%3]=v[k]*(i>=3?-1:1);
  1347. }
  1348. }
  1349. for(int j=0;j<4;j++) {
  1350. surface_tool->add_bones(bones);
  1351. surface_tool->add_weights(weights);
  1352. surface_tool->add_color(Color(1.0,0.4,0.4,0.4));
  1353. surface_tool->add_vertex(t.xform(face_points[j]*0.04));
  1354. surface_tool->add_bones(bones);
  1355. surface_tool->add_weights(weights);
  1356. surface_tool->add_color(Color(1.0,0.4,0.4,0.4));
  1357. surface_tool->add_vertex(t.xform(face_points[(j+1)%4]*0.04));
  1358. }
  1359. }
  1360. */
  1361. }
  1362. Ref<ArrayMesh> m = surface_tool->commit();
  1363. p_gizmo->add_mesh(m, false, skel->register_skin(Ref<Skin>()));
  1364. }
  1365. ////
  1366. PhysicalBoneSpatialGizmoPlugin::PhysicalBoneSpatialGizmoPlugin() {
  1367. create_material("joint_material", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/joint", Color(0.5, 0.8, 1)));
  1368. }
  1369. bool PhysicalBoneSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1370. return Object::cast_to<PhysicalBone>(p_spatial) != NULL;
  1371. }
  1372. String PhysicalBoneSpatialGizmoPlugin::get_name() const {
  1373. return "PhysicalBones";
  1374. }
  1375. int PhysicalBoneSpatialGizmoPlugin::get_priority() const {
  1376. return -1;
  1377. }
  1378. void PhysicalBoneSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1379. p_gizmo->clear();
  1380. PhysicalBone *physical_bone = Object::cast_to<PhysicalBone>(p_gizmo->get_spatial_node());
  1381. if (!physical_bone)
  1382. return;
  1383. Skeleton *sk(physical_bone->find_skeleton_parent());
  1384. if (!sk)
  1385. return;
  1386. PhysicalBone *pb(sk->get_physical_bone(physical_bone->get_bone_id()));
  1387. if (!pb)
  1388. return;
  1389. PhysicalBone *pbp(sk->get_physical_bone_parent(physical_bone->get_bone_id()));
  1390. if (!pbp)
  1391. return;
  1392. Vector<Vector3> points;
  1393. switch (physical_bone->get_joint_type()) {
  1394. case PhysicalBone::JOINT_TYPE_PIN: {
  1395. JointSpatialGizmoPlugin::CreatePinJointGizmo(physical_bone->get_joint_offset(), points);
  1396. } break;
  1397. case PhysicalBone::JOINT_TYPE_CONE: {
  1398. const PhysicalBone::ConeJointData *cjd(static_cast<const PhysicalBone::ConeJointData *>(physical_bone->get_joint_data()));
  1399. JointSpatialGizmoPlugin::CreateConeTwistJointGizmo(
  1400. physical_bone->get_joint_offset(),
  1401. physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
  1402. pb->get_global_transform(),
  1403. pbp->get_global_transform(),
  1404. cjd->swing_span,
  1405. cjd->twist_span,
  1406. &points,
  1407. &points);
  1408. } break;
  1409. case PhysicalBone::JOINT_TYPE_HINGE: {
  1410. const PhysicalBone::HingeJointData *hjd(static_cast<const PhysicalBone::HingeJointData *>(physical_bone->get_joint_data()));
  1411. JointSpatialGizmoPlugin::CreateHingeJointGizmo(
  1412. physical_bone->get_joint_offset(),
  1413. physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
  1414. pb->get_global_transform(),
  1415. pbp->get_global_transform(),
  1416. hjd->angular_limit_lower,
  1417. hjd->angular_limit_upper,
  1418. hjd->angular_limit_enabled,
  1419. points,
  1420. &points,
  1421. &points);
  1422. } break;
  1423. case PhysicalBone::JOINT_TYPE_SLIDER: {
  1424. const PhysicalBone::SliderJointData *sjd(static_cast<const PhysicalBone::SliderJointData *>(physical_bone->get_joint_data()));
  1425. JointSpatialGizmoPlugin::CreateSliderJointGizmo(
  1426. physical_bone->get_joint_offset(),
  1427. physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
  1428. pb->get_global_transform(),
  1429. pbp->get_global_transform(),
  1430. sjd->angular_limit_lower,
  1431. sjd->angular_limit_upper,
  1432. sjd->linear_limit_lower,
  1433. sjd->linear_limit_upper,
  1434. points,
  1435. &points,
  1436. &points);
  1437. } break;
  1438. case PhysicalBone::JOINT_TYPE_6DOF: {
  1439. const PhysicalBone::SixDOFJointData *sdofjd(static_cast<const PhysicalBone::SixDOFJointData *>(physical_bone->get_joint_data()));
  1440. JointSpatialGizmoPlugin::CreateGeneric6DOFJointGizmo(
  1441. physical_bone->get_joint_offset(),
  1442. physical_bone->get_global_transform() * physical_bone->get_joint_offset(),
  1443. pb->get_global_transform(),
  1444. pbp->get_global_transform(),
  1445. sdofjd->axis_data[0].angular_limit_lower,
  1446. sdofjd->axis_data[0].angular_limit_upper,
  1447. sdofjd->axis_data[0].linear_limit_lower,
  1448. sdofjd->axis_data[0].linear_limit_upper,
  1449. sdofjd->axis_data[0].angular_limit_enabled,
  1450. sdofjd->axis_data[0].linear_limit_enabled,
  1451. sdofjd->axis_data[1].angular_limit_lower,
  1452. sdofjd->axis_data[1].angular_limit_upper,
  1453. sdofjd->axis_data[1].linear_limit_lower,
  1454. sdofjd->axis_data[1].linear_limit_upper,
  1455. sdofjd->axis_data[1].angular_limit_enabled,
  1456. sdofjd->axis_data[1].linear_limit_enabled,
  1457. sdofjd->axis_data[2].angular_limit_lower,
  1458. sdofjd->axis_data[2].angular_limit_upper,
  1459. sdofjd->axis_data[2].linear_limit_lower,
  1460. sdofjd->axis_data[2].linear_limit_upper,
  1461. sdofjd->axis_data[2].angular_limit_enabled,
  1462. sdofjd->axis_data[2].linear_limit_enabled,
  1463. points,
  1464. &points,
  1465. &points);
  1466. } break;
  1467. default:
  1468. return;
  1469. }
  1470. Ref<Material> material = get_material("joint_material", p_gizmo);
  1471. p_gizmo->add_collision_segments(points);
  1472. p_gizmo->add_lines(points, material);
  1473. }
  1474. /////
  1475. RayCastSpatialGizmoPlugin::RayCastSpatialGizmoPlugin() {
  1476. const Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/shape", Color(0.5, 0.7, 1));
  1477. create_material("shape_material", gizmo_color);
  1478. const float gizmo_value = gizmo_color.get_v();
  1479. const Color gizmo_color_disabled = Color(gizmo_value, gizmo_value, gizmo_value, 0.65);
  1480. create_material("shape_material_disabled", gizmo_color_disabled);
  1481. }
  1482. bool RayCastSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1483. return Object::cast_to<RayCast>(p_spatial) != NULL;
  1484. }
  1485. String RayCastSpatialGizmoPlugin::get_name() const {
  1486. return "RayCast";
  1487. }
  1488. int RayCastSpatialGizmoPlugin::get_priority() const {
  1489. return -1;
  1490. }
  1491. void RayCastSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1492. RayCast *raycast = Object::cast_to<RayCast>(p_gizmo->get_spatial_node());
  1493. p_gizmo->clear();
  1494. Vector<Vector3> lines;
  1495. lines.push_back(Vector3());
  1496. lines.push_back(raycast->get_cast_to());
  1497. const Ref<SpatialMaterial> material =
  1498. get_material(raycast->is_enabled() ? "shape_material" : "shape_material_disabled", p_gizmo);
  1499. p_gizmo->add_lines(lines, material);
  1500. p_gizmo->add_collision_segments(lines);
  1501. }
  1502. /////
  1503. void SpringArmSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1504. SpringArm *spring_arm = Object::cast_to<SpringArm>(p_gizmo->get_spatial_node());
  1505. p_gizmo->clear();
  1506. Vector<Vector3> lines;
  1507. lines.push_back(Vector3());
  1508. lines.push_back(Vector3(0, 0, 1.0) * spring_arm->get_length());
  1509. Ref<SpatialMaterial> material = get_material("shape_material", p_gizmo);
  1510. p_gizmo->add_lines(lines, material);
  1511. p_gizmo->add_collision_segments(lines);
  1512. }
  1513. SpringArmSpatialGizmoPlugin::SpringArmSpatialGizmoPlugin() {
  1514. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/shape", Color(0.5, 0.7, 1));
  1515. create_material("shape_material", gizmo_color);
  1516. }
  1517. bool SpringArmSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1518. return Object::cast_to<SpringArm>(p_spatial) != NULL;
  1519. }
  1520. String SpringArmSpatialGizmoPlugin::get_name() const {
  1521. return "SpringArm";
  1522. }
  1523. int SpringArmSpatialGizmoPlugin::get_priority() const {
  1524. return -1;
  1525. }
  1526. /////
  1527. VehicleWheelSpatialGizmoPlugin::VehicleWheelSpatialGizmoPlugin() {
  1528. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/shape", Color(0.5, 0.7, 1));
  1529. create_material("shape_material", gizmo_color);
  1530. }
  1531. bool VehicleWheelSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1532. return Object::cast_to<VehicleWheel>(p_spatial) != NULL;
  1533. }
  1534. String VehicleWheelSpatialGizmoPlugin::get_name() const {
  1535. return "VehicleWheel";
  1536. }
  1537. int VehicleWheelSpatialGizmoPlugin::get_priority() const {
  1538. return -1;
  1539. }
  1540. void VehicleWheelSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1541. VehicleWheel *car_wheel = Object::cast_to<VehicleWheel>(p_gizmo->get_spatial_node());
  1542. p_gizmo->clear();
  1543. Vector<Vector3> points;
  1544. float r = car_wheel->get_radius();
  1545. const int skip = 10;
  1546. for (int i = 0; i <= 360; i += skip) {
  1547. float ra = Math::deg2rad((float)i);
  1548. float rb = Math::deg2rad((float)i + skip);
  1549. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  1550. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  1551. points.push_back(Vector3(0, a.x, a.y));
  1552. points.push_back(Vector3(0, b.x, b.y));
  1553. const int springsec = 4;
  1554. for (int j = 0; j < springsec; j++) {
  1555. float t = car_wheel->get_suspension_rest_length() * 5;
  1556. points.push_back(Vector3(a.x, i / 360.0 * t / springsec + j * (t / springsec), a.y) * 0.2);
  1557. points.push_back(Vector3(b.x, (i + skip) / 360.0 * t / springsec + j * (t / springsec), b.y) * 0.2);
  1558. }
  1559. }
  1560. //travel
  1561. points.push_back(Vector3(0, 0, 0));
  1562. points.push_back(Vector3(0, car_wheel->get_suspension_rest_length(), 0));
  1563. //axis
  1564. points.push_back(Vector3(r * 0.2, car_wheel->get_suspension_rest_length(), 0));
  1565. points.push_back(Vector3(-r * 0.2, car_wheel->get_suspension_rest_length(), 0));
  1566. //axis
  1567. points.push_back(Vector3(r * 0.2, 0, 0));
  1568. points.push_back(Vector3(-r * 0.2, 0, 0));
  1569. //forward line
  1570. points.push_back(Vector3(0, -r, 0));
  1571. points.push_back(Vector3(0, -r, r * 2));
  1572. points.push_back(Vector3(0, -r, r * 2));
  1573. points.push_back(Vector3(r * 2 * 0.2, -r, r * 2 * 0.8));
  1574. points.push_back(Vector3(0, -r, r * 2));
  1575. points.push_back(Vector3(-r * 2 * 0.2, -r, r * 2 * 0.8));
  1576. Ref<Material> material = get_material("shape_material", p_gizmo);
  1577. p_gizmo->add_lines(points, material);
  1578. p_gizmo->add_collision_segments(points);
  1579. }
  1580. ///////////
  1581. SoftBodySpatialGizmoPlugin::SoftBodySpatialGizmoPlugin() {
  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. create_handle_material("handles");
  1585. }
  1586. bool SoftBodySpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1587. return Object::cast_to<SoftBody>(p_spatial) != NULL;
  1588. }
  1589. String SoftBodySpatialGizmoPlugin::get_name() const {
  1590. return "SoftBody";
  1591. }
  1592. int SoftBodySpatialGizmoPlugin::get_priority() const {
  1593. return -1;
  1594. }
  1595. bool SoftBodySpatialGizmoPlugin::is_selectable_when_hidden() const {
  1596. return true;
  1597. }
  1598. void SoftBodySpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1599. SoftBody *soft_body = Object::cast_to<SoftBody>(p_gizmo->get_spatial_node());
  1600. p_gizmo->clear();
  1601. if (!soft_body || soft_body->get_mesh().is_null()) {
  1602. return;
  1603. }
  1604. // find mesh
  1605. Vector<Vector3> lines;
  1606. soft_body->get_mesh()->generate_debug_mesh_lines(lines);
  1607. if (!lines.size()) {
  1608. return;
  1609. }
  1610. Ref<TriangleMesh> tm = soft_body->get_mesh()->generate_triangle_mesh();
  1611. Vector<Vector3> points;
  1612. soft_body->get_mesh()->generate_debug_mesh_indices(points);
  1613. Ref<Material> material = get_material("shape_material", p_gizmo);
  1614. p_gizmo->add_lines(lines, material);
  1615. p_gizmo->add_handles(points, get_material("handles"));
  1616. p_gizmo->add_collision_triangles(tm);
  1617. }
  1618. String SoftBodySpatialGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  1619. return "SoftBody pin point";
  1620. }
  1621. Variant SoftBodySpatialGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  1622. SoftBody *soft_body = Object::cast_to<SoftBody>(p_gizmo->get_spatial_node());
  1623. return Variant(soft_body->is_point_pinned(p_idx));
  1624. }
  1625. void SoftBodySpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  1626. SoftBody *soft_body = Object::cast_to<SoftBody>(p_gizmo->get_spatial_node());
  1627. soft_body->pin_point_toggle(p_idx);
  1628. }
  1629. bool SoftBodySpatialGizmoPlugin::is_handle_highlighted(const EditorSpatialGizmo *p_gizmo, int idx) const {
  1630. SoftBody *soft_body = Object::cast_to<SoftBody>(p_gizmo->get_spatial_node());
  1631. return soft_body->is_point_pinned(idx);
  1632. }
  1633. ///////////
  1634. VisibilityNotifierGizmoPlugin::VisibilityNotifierGizmoPlugin() {
  1635. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/visibility_notifier", Color(0.8, 0.5, 0.7));
  1636. create_material("visibility_notifier_material", gizmo_color);
  1637. gizmo_color.a = 0.1;
  1638. create_material("visibility_notifier_solid_material", gizmo_color);
  1639. create_handle_material("handles");
  1640. }
  1641. bool VisibilityNotifierGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1642. return Object::cast_to<VisibilityNotifier>(p_spatial) != NULL;
  1643. }
  1644. String VisibilityNotifierGizmoPlugin::get_name() const {
  1645. return "VisibilityNotifier";
  1646. }
  1647. int VisibilityNotifierGizmoPlugin::get_priority() const {
  1648. return -1;
  1649. }
  1650. String VisibilityNotifierGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  1651. switch (p_idx) {
  1652. case 0: return "Size X";
  1653. case 1: return "Size Y";
  1654. case 2: return "Size Z";
  1655. case 3: return "Pos X";
  1656. case 4: return "Pos Y";
  1657. case 5: return "Pos Z";
  1658. }
  1659. return "";
  1660. }
  1661. Variant VisibilityNotifierGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  1662. VisibilityNotifier *notifier = Object::cast_to<VisibilityNotifier>(p_gizmo->get_spatial_node());
  1663. return notifier->get_aabb();
  1664. }
  1665. void VisibilityNotifierGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  1666. VisibilityNotifier *notifier = Object::cast_to<VisibilityNotifier>(p_gizmo->get_spatial_node());
  1667. Transform gt = notifier->get_global_transform();
  1668. Transform gi = gt.affine_inverse();
  1669. bool move = p_idx >= 3;
  1670. p_idx = p_idx % 3;
  1671. AABB aabb = notifier->get_aabb();
  1672. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1673. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1674. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  1675. Vector3 ofs = aabb.position + aabb.size * 0.5;
  1676. Vector3 axis;
  1677. axis[p_idx] = 1.0;
  1678. if (move) {
  1679. Vector3 ra, rb;
  1680. Geometry::get_closest_points_between_segments(ofs - axis * 4096, ofs + axis * 4096, sg[0], sg[1], ra, rb);
  1681. float d = ra[p_idx];
  1682. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  1683. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  1684. }
  1685. aabb.position[p_idx] = d - 1.0 - aabb.size[p_idx] * 0.5;
  1686. notifier->set_aabb(aabb);
  1687. } else {
  1688. Vector3 ra, rb;
  1689. Geometry::get_closest_points_between_segments(ofs, ofs + axis * 4096, sg[0], sg[1], ra, rb);
  1690. float d = ra[p_idx] - ofs[p_idx];
  1691. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  1692. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  1693. }
  1694. if (d < 0.001)
  1695. d = 0.001;
  1696. //resize
  1697. aabb.position[p_idx] = (aabb.position[p_idx] + aabb.size[p_idx] * 0.5) - d;
  1698. aabb.size[p_idx] = d * 2;
  1699. notifier->set_aabb(aabb);
  1700. }
  1701. }
  1702. void VisibilityNotifierGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  1703. VisibilityNotifier *notifier = Object::cast_to<VisibilityNotifier>(p_gizmo->get_spatial_node());
  1704. if (p_cancel) {
  1705. notifier->set_aabb(p_restore);
  1706. return;
  1707. }
  1708. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1709. ur->create_action(TTR("Change Notifier AABB"));
  1710. ur->add_do_method(notifier, "set_aabb", notifier->get_aabb());
  1711. ur->add_undo_method(notifier, "set_aabb", p_restore);
  1712. ur->commit_action();
  1713. }
  1714. void VisibilityNotifierGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1715. VisibilityNotifier *notifier = Object::cast_to<VisibilityNotifier>(p_gizmo->get_spatial_node());
  1716. p_gizmo->clear();
  1717. Vector<Vector3> lines;
  1718. AABB aabb = notifier->get_aabb();
  1719. for (int i = 0; i < 12; i++) {
  1720. Vector3 a, b;
  1721. aabb.get_edge(i, a, b);
  1722. lines.push_back(a);
  1723. lines.push_back(b);
  1724. }
  1725. Vector<Vector3> handles;
  1726. for (int i = 0; i < 3; i++) {
  1727. Vector3 ax;
  1728. ax[i] = aabb.position[i] + aabb.size[i];
  1729. ax[(i + 1) % 3] = aabb.position[(i + 1) % 3] + aabb.size[(i + 1) % 3] * 0.5;
  1730. ax[(i + 2) % 3] = aabb.position[(i + 2) % 3] + aabb.size[(i + 2) % 3] * 0.5;
  1731. handles.push_back(ax);
  1732. }
  1733. Vector3 center = aabb.position + aabb.size * 0.5;
  1734. for (int i = 0; i < 3; i++) {
  1735. Vector3 ax;
  1736. ax[i] = 1.0;
  1737. handles.push_back(center + ax);
  1738. lines.push_back(center);
  1739. lines.push_back(center + ax);
  1740. }
  1741. Ref<Material> material = get_material("visibility_notifier_material", p_gizmo);
  1742. p_gizmo->add_lines(lines, material);
  1743. p_gizmo->add_collision_segments(lines);
  1744. if (p_gizmo->is_selected()) {
  1745. Ref<Material> solid_material = get_material("visibility_notifier_solid_material", p_gizmo);
  1746. p_gizmo->add_solid_box(solid_material, aabb.get_size(), aabb.get_position() + aabb.get_size() / 2.0);
  1747. }
  1748. p_gizmo->add_handles(handles, get_material("handles"));
  1749. }
  1750. ////
  1751. CPUParticlesGizmoPlugin::CPUParticlesGizmoPlugin() {
  1752. create_icon_material("particles_icon", SpatialEditor::get_singleton()->get_icon("GizmoCPUParticles", "EditorIcons"));
  1753. }
  1754. bool CPUParticlesGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1755. return Object::cast_to<CPUParticles>(p_spatial) != NULL;
  1756. }
  1757. String CPUParticlesGizmoPlugin::get_name() const {
  1758. return "CPUParticles";
  1759. }
  1760. int CPUParticlesGizmoPlugin::get_priority() const {
  1761. return -1;
  1762. }
  1763. bool CPUParticlesGizmoPlugin::is_selectable_when_hidden() const {
  1764. return true;
  1765. }
  1766. void CPUParticlesGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1767. Ref<Material> icon = get_material("particles_icon", p_gizmo);
  1768. p_gizmo->add_unscaled_billboard(icon, 0.05);
  1769. }
  1770. ////
  1771. ParticlesGizmoPlugin::ParticlesGizmoPlugin() {
  1772. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/particles", Color(0.8, 0.7, 0.4));
  1773. create_material("particles_material", gizmo_color);
  1774. gizmo_color.a = 0.1;
  1775. create_material("particles_solid_material", gizmo_color);
  1776. create_icon_material("particles_icon", SpatialEditor::get_singleton()->get_icon("GizmoParticles", "EditorIcons"));
  1777. create_handle_material("handles");
  1778. }
  1779. bool ParticlesGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1780. return Object::cast_to<Particles>(p_spatial) != NULL;
  1781. }
  1782. String ParticlesGizmoPlugin::get_name() const {
  1783. return "Particles";
  1784. }
  1785. int ParticlesGizmoPlugin::get_priority() const {
  1786. return -1;
  1787. }
  1788. bool ParticlesGizmoPlugin::is_selectable_when_hidden() const {
  1789. return true;
  1790. }
  1791. String ParticlesGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  1792. switch (p_idx) {
  1793. case 0: return "Size X";
  1794. case 1: return "Size Y";
  1795. case 2: return "Size Z";
  1796. case 3: return "Pos X";
  1797. case 4: return "Pos Y";
  1798. case 5: return "Pos Z";
  1799. }
  1800. return "";
  1801. }
  1802. Variant ParticlesGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  1803. Particles *particles = Object::cast_to<Particles>(p_gizmo->get_spatial_node());
  1804. return particles->get_visibility_aabb();
  1805. }
  1806. void ParticlesGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  1807. Particles *particles = Object::cast_to<Particles>(p_gizmo->get_spatial_node());
  1808. Transform gt = particles->get_global_transform();
  1809. Transform gi = gt.affine_inverse();
  1810. bool move = p_idx >= 3;
  1811. p_idx = p_idx % 3;
  1812. AABB aabb = particles->get_visibility_aabb();
  1813. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1814. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1815. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  1816. Vector3 ofs = aabb.position + aabb.size * 0.5;
  1817. Vector3 axis;
  1818. axis[p_idx] = 1.0;
  1819. if (move) {
  1820. Vector3 ra, rb;
  1821. Geometry::get_closest_points_between_segments(ofs - axis * 4096, ofs + axis * 4096, sg[0], sg[1], ra, rb);
  1822. float d = ra[p_idx];
  1823. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  1824. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  1825. }
  1826. aabb.position[p_idx] = d - 1.0 - aabb.size[p_idx] * 0.5;
  1827. particles->set_visibility_aabb(aabb);
  1828. } else {
  1829. Vector3 ra, rb;
  1830. Geometry::get_closest_points_between_segments(ofs, ofs + axis * 4096, sg[0], sg[1], ra, rb);
  1831. float d = ra[p_idx] - ofs[p_idx];
  1832. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  1833. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  1834. }
  1835. if (d < 0.001)
  1836. d = 0.001;
  1837. //resize
  1838. aabb.position[p_idx] = (aabb.position[p_idx] + aabb.size[p_idx] * 0.5) - d;
  1839. aabb.size[p_idx] = d * 2;
  1840. particles->set_visibility_aabb(aabb);
  1841. }
  1842. }
  1843. void ParticlesGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  1844. Particles *particles = Object::cast_to<Particles>(p_gizmo->get_spatial_node());
  1845. if (p_cancel) {
  1846. particles->set_visibility_aabb(p_restore);
  1847. return;
  1848. }
  1849. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1850. ur->create_action(TTR("Change Particles AABB"));
  1851. ur->add_do_method(particles, "set_visibility_aabb", particles->get_visibility_aabb());
  1852. ur->add_undo_method(particles, "set_visibility_aabb", p_restore);
  1853. ur->commit_action();
  1854. }
  1855. void ParticlesGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1856. Particles *particles = Object::cast_to<Particles>(p_gizmo->get_spatial_node());
  1857. p_gizmo->clear();
  1858. Vector<Vector3> lines;
  1859. AABB aabb = particles->get_visibility_aabb();
  1860. for (int i = 0; i < 12; i++) {
  1861. Vector3 a, b;
  1862. aabb.get_edge(i, a, b);
  1863. lines.push_back(a);
  1864. lines.push_back(b);
  1865. }
  1866. Vector<Vector3> handles;
  1867. for (int i = 0; i < 3; i++) {
  1868. Vector3 ax;
  1869. ax[i] = aabb.position[i] + aabb.size[i];
  1870. ax[(i + 1) % 3] = aabb.position[(i + 1) % 3] + aabb.size[(i + 1) % 3] * 0.5;
  1871. ax[(i + 2) % 3] = aabb.position[(i + 2) % 3] + aabb.size[(i + 2) % 3] * 0.5;
  1872. handles.push_back(ax);
  1873. }
  1874. Vector3 center = aabb.position + aabb.size * 0.5;
  1875. for (int i = 0; i < 3; i++) {
  1876. Vector3 ax;
  1877. ax[i] = 1.0;
  1878. handles.push_back(center + ax);
  1879. lines.push_back(center);
  1880. lines.push_back(center + ax);
  1881. }
  1882. Ref<Material> material = get_material("particles_material", p_gizmo);
  1883. Ref<Material> icon = get_material("particles_icon", p_gizmo);
  1884. p_gizmo->add_lines(lines, material);
  1885. if (p_gizmo->is_selected()) {
  1886. Ref<Material> solid_material = get_material("particles_solid_material", p_gizmo);
  1887. p_gizmo->add_solid_box(solid_material, aabb.get_size(), aabb.get_position() + aabb.get_size() / 2.0);
  1888. }
  1889. p_gizmo->add_handles(handles, get_material("handles"));
  1890. p_gizmo->add_unscaled_billboard(icon, 0.05);
  1891. }
  1892. ////
  1893. ReflectionProbeGizmoPlugin::ReflectionProbeGizmoPlugin() {
  1894. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/reflection_probe", Color(0.6, 1, 0.5));
  1895. create_material("reflection_probe_material", gizmo_color);
  1896. gizmo_color.a = 0.5;
  1897. create_material("reflection_internal_material", gizmo_color);
  1898. gizmo_color.a = 0.1;
  1899. create_material("reflection_probe_solid_material", gizmo_color);
  1900. create_icon_material("reflection_probe_icon", SpatialEditor::get_singleton()->get_icon("GizmoReflectionProbe", "EditorIcons"));
  1901. create_handle_material("handles");
  1902. }
  1903. bool ReflectionProbeGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  1904. return Object::cast_to<ReflectionProbe>(p_spatial) != NULL;
  1905. }
  1906. String ReflectionProbeGizmoPlugin::get_name() const {
  1907. return "ReflectionProbe";
  1908. }
  1909. int ReflectionProbeGizmoPlugin::get_priority() const {
  1910. return -1;
  1911. }
  1912. String ReflectionProbeGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  1913. switch (p_idx) {
  1914. case 0: return "Extents X";
  1915. case 1: return "Extents Y";
  1916. case 2: return "Extents Z";
  1917. case 3: return "Origin X";
  1918. case 4: return "Origin Y";
  1919. case 5: return "Origin Z";
  1920. }
  1921. return "";
  1922. }
  1923. Variant ReflectionProbeGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  1924. ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_spatial_node());
  1925. return AABB(probe->get_extents(), probe->get_origin_offset());
  1926. }
  1927. void ReflectionProbeGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  1928. ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_spatial_node());
  1929. Transform gt = probe->get_global_transform();
  1930. Transform gi = gt.affine_inverse();
  1931. if (p_idx < 3) {
  1932. Vector3 extents = probe->get_extents();
  1933. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1934. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1935. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };
  1936. Vector3 axis;
  1937. axis[p_idx] = 1.0;
  1938. Vector3 ra, rb;
  1939. Geometry::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
  1940. float d = ra[p_idx];
  1941. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  1942. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  1943. }
  1944. if (d < 0.001)
  1945. d = 0.001;
  1946. extents[p_idx] = d;
  1947. probe->set_extents(extents);
  1948. } else {
  1949. p_idx -= 3;
  1950. Vector3 origin = probe->get_origin_offset();
  1951. origin[p_idx] = 0;
  1952. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1953. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1954. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };
  1955. Vector3 axis;
  1956. axis[p_idx] = 1.0;
  1957. Vector3 ra, rb;
  1958. Geometry::get_closest_points_between_segments(origin - axis * 16384, origin + axis * 16384, sg[0], sg[1], ra, rb);
  1959. // Adjust the actual position to account for the gizmo handle position
  1960. float d = ra[p_idx] + 0.25;
  1961. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  1962. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  1963. }
  1964. origin[p_idx] = d;
  1965. probe->set_origin_offset(origin);
  1966. }
  1967. }
  1968. void ReflectionProbeGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  1969. ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_spatial_node());
  1970. AABB restore = p_restore;
  1971. if (p_cancel) {
  1972. probe->set_extents(restore.position);
  1973. probe->set_origin_offset(restore.size);
  1974. return;
  1975. }
  1976. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1977. ur->create_action(TTR("Change Probe Extents"));
  1978. ur->add_do_method(probe, "set_extents", probe->get_extents());
  1979. ur->add_do_method(probe, "set_origin_offset", probe->get_origin_offset());
  1980. ur->add_undo_method(probe, "set_extents", restore.position);
  1981. ur->add_undo_method(probe, "set_origin_offset", restore.size);
  1982. ur->commit_action();
  1983. }
  1984. void ReflectionProbeGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  1985. ReflectionProbe *probe = Object::cast_to<ReflectionProbe>(p_gizmo->get_spatial_node());
  1986. p_gizmo->clear();
  1987. Vector<Vector3> lines;
  1988. Vector<Vector3> internal_lines;
  1989. Vector3 extents = probe->get_extents();
  1990. AABB aabb;
  1991. aabb.position = -extents;
  1992. aabb.size = extents * 2;
  1993. for (int i = 0; i < 12; i++) {
  1994. Vector3 a, b;
  1995. aabb.get_edge(i, a, b);
  1996. lines.push_back(a);
  1997. lines.push_back(b);
  1998. }
  1999. for (int i = 0; i < 8; i++) {
  2000. Vector3 ep = aabb.get_endpoint(i);
  2001. internal_lines.push_back(probe->get_origin_offset());
  2002. internal_lines.push_back(ep);
  2003. }
  2004. Vector<Vector3> handles;
  2005. for (int i = 0; i < 3; i++) {
  2006. Vector3 ax;
  2007. ax[i] = aabb.position[i] + aabb.size[i];
  2008. handles.push_back(ax);
  2009. }
  2010. for (int i = 0; i < 3; i++) {
  2011. Vector3 orig_handle = probe->get_origin_offset();
  2012. orig_handle[i] -= 0.25;
  2013. lines.push_back(orig_handle);
  2014. handles.push_back(orig_handle);
  2015. orig_handle[i] += 0.5;
  2016. lines.push_back(orig_handle);
  2017. }
  2018. Ref<Material> material = get_material("reflection_probe_material", p_gizmo);
  2019. Ref<Material> material_internal = get_material("reflection_internal_material", p_gizmo);
  2020. Ref<Material> icon = get_material("reflection_probe_icon", p_gizmo);
  2021. p_gizmo->add_lines(lines, material);
  2022. p_gizmo->add_lines(internal_lines, material_internal);
  2023. if (p_gizmo->is_selected()) {
  2024. Ref<Material> solid_material = get_material("reflection_probe_solid_material", p_gizmo);
  2025. p_gizmo->add_solid_box(solid_material, probe->get_extents() * 2.0);
  2026. }
  2027. p_gizmo->add_unscaled_billboard(icon, 0.05);
  2028. p_gizmo->add_handles(handles, get_material("handles"));
  2029. }
  2030. GIProbeGizmoPlugin::GIProbeGizmoPlugin() {
  2031. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/gi_probe", Color(0.5, 1, 0.6));
  2032. create_material("gi_probe_material", gizmo_color);
  2033. gizmo_color.a = 0.5;
  2034. create_material("gi_probe_internal_material", gizmo_color);
  2035. gizmo_color.a = 0.1;
  2036. create_material("gi_probe_solid_material", gizmo_color);
  2037. create_icon_material("gi_probe_icon", SpatialEditor::get_singleton()->get_icon("GizmoGIProbe", "EditorIcons"));
  2038. create_handle_material("handles");
  2039. }
  2040. bool GIProbeGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  2041. return Object::cast_to<GIProbe>(p_spatial) != NULL;
  2042. }
  2043. String GIProbeGizmoPlugin::get_name() const {
  2044. return "GIProbe";
  2045. }
  2046. int GIProbeGizmoPlugin::get_priority() const {
  2047. return -1;
  2048. }
  2049. String GIProbeGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  2050. switch (p_idx) {
  2051. case 0: return "Extents X";
  2052. case 1: return "Extents Y";
  2053. case 2: return "Extents Z";
  2054. }
  2055. return "";
  2056. }
  2057. Variant GIProbeGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  2058. GIProbe *probe = Object::cast_to<GIProbe>(p_gizmo->get_spatial_node());
  2059. return probe->get_extents();
  2060. }
  2061. void GIProbeGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  2062. GIProbe *probe = Object::cast_to<GIProbe>(p_gizmo->get_spatial_node());
  2063. Transform gt = probe->get_global_transform();
  2064. Transform gi = gt.affine_inverse();
  2065. Vector3 extents = probe->get_extents();
  2066. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  2067. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  2068. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };
  2069. Vector3 axis;
  2070. axis[p_idx] = 1.0;
  2071. Vector3 ra, rb;
  2072. Geometry::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
  2073. float d = ra[p_idx];
  2074. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  2075. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  2076. }
  2077. if (d < 0.001)
  2078. d = 0.001;
  2079. extents[p_idx] = d;
  2080. probe->set_extents(extents);
  2081. }
  2082. void GIProbeGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  2083. GIProbe *probe = Object::cast_to<GIProbe>(p_gizmo->get_spatial_node());
  2084. Vector3 restore = p_restore;
  2085. if (p_cancel) {
  2086. probe->set_extents(restore);
  2087. return;
  2088. }
  2089. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  2090. ur->create_action(TTR("Change Probe Extents"));
  2091. ur->add_do_method(probe, "set_extents", probe->get_extents());
  2092. ur->add_undo_method(probe, "set_extents", restore);
  2093. ur->commit_action();
  2094. }
  2095. void GIProbeGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  2096. GIProbe *probe = Object::cast_to<GIProbe>(p_gizmo->get_spatial_node());
  2097. Ref<Material> material = get_material("gi_probe_material", p_gizmo);
  2098. Ref<Material> icon = get_material("gi_probe_icon", p_gizmo);
  2099. Ref<Material> material_internal = get_material("gi_probe_internal_material", p_gizmo);
  2100. p_gizmo->clear();
  2101. Vector<Vector3> lines;
  2102. Vector3 extents = probe->get_extents();
  2103. static const int subdivs[GIProbe::SUBDIV_MAX] = { 64, 128, 256, 512 };
  2104. AABB aabb = AABB(-extents, extents * 2);
  2105. int subdiv = subdivs[probe->get_subdiv()];
  2106. float cell_size = aabb.get_longest_axis_size() / subdiv;
  2107. for (int i = 0; i < 12; i++) {
  2108. Vector3 a, b;
  2109. aabb.get_edge(i, a, b);
  2110. lines.push_back(a);
  2111. lines.push_back(b);
  2112. }
  2113. p_gizmo->add_lines(lines, material);
  2114. lines.clear();
  2115. for (int i = 1; i < subdiv; i++) {
  2116. for (int j = 0; j < 3; j++) {
  2117. if (cell_size * i > aabb.size[j]) {
  2118. continue;
  2119. }
  2120. Vector2 dir;
  2121. dir[j] = 1.0;
  2122. Vector2 ta, tb;
  2123. int j_n1 = (j + 1) % 3;
  2124. int j_n2 = (j + 2) % 3;
  2125. ta[j_n1] = 1.0;
  2126. tb[j_n2] = 1.0;
  2127. for (int k = 0; k < 4; k++) {
  2128. Vector3 from = aabb.position, to = aabb.position;
  2129. from[j] += cell_size * i;
  2130. to[j] += cell_size * i;
  2131. if (k & 1) {
  2132. to[j_n1] += aabb.size[j_n1];
  2133. } else {
  2134. to[j_n2] += aabb.size[j_n2];
  2135. }
  2136. if (k & 2) {
  2137. from[j_n1] += aabb.size[j_n1];
  2138. from[j_n2] += aabb.size[j_n2];
  2139. }
  2140. lines.push_back(from);
  2141. lines.push_back(to);
  2142. }
  2143. }
  2144. }
  2145. p_gizmo->add_lines(lines, material_internal);
  2146. Vector<Vector3> handles;
  2147. for (int i = 0; i < 3; i++) {
  2148. Vector3 ax;
  2149. ax[i] = aabb.position[i] + aabb.size[i];
  2150. handles.push_back(ax);
  2151. }
  2152. if (p_gizmo->is_selected()) {
  2153. Ref<Material> solid_material = get_material("gi_probe_solid_material", p_gizmo);
  2154. p_gizmo->add_solid_box(solid_material, aabb.get_size());
  2155. }
  2156. p_gizmo->add_unscaled_billboard(icon, 0.05);
  2157. p_gizmo->add_handles(handles, get_material("handles"));
  2158. }
  2159. ////
  2160. BakedIndirectLightGizmoPlugin::BakedIndirectLightGizmoPlugin() {
  2161. Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/baked_indirect_light", Color(0.5, 0.6, 1));
  2162. create_material("baked_indirect_light_material", gizmo_color);
  2163. gizmo_color.a = 0.1;
  2164. create_material("baked_indirect_light_internal_material", gizmo_color);
  2165. create_icon_material("baked_indirect_light_icon", SpatialEditor::get_singleton()->get_icon("GizmoBakedLightmap", "EditorIcons"));
  2166. create_handle_material("handles");
  2167. }
  2168. String BakedIndirectLightGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  2169. switch (p_idx) {
  2170. case 0: return "Extents X";
  2171. case 1: return "Extents Y";
  2172. case 2: return "Extents Z";
  2173. }
  2174. return "";
  2175. }
  2176. Variant BakedIndirectLightGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  2177. BakedLightmap *baker = Object::cast_to<BakedLightmap>(p_gizmo->get_spatial_node());
  2178. return baker->get_extents();
  2179. }
  2180. void BakedIndirectLightGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  2181. BakedLightmap *baker = Object::cast_to<BakedLightmap>(p_gizmo->get_spatial_node());
  2182. Transform gt = baker->get_global_transform();
  2183. Transform gi = gt.affine_inverse();
  2184. Vector3 extents = baker->get_extents();
  2185. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  2186. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  2187. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 16384) };
  2188. Vector3 axis;
  2189. axis[p_idx] = 1.0;
  2190. Vector3 ra, rb;
  2191. Geometry::get_closest_points_between_segments(Vector3(), axis * 16384, sg[0], sg[1], ra, rb);
  2192. float d = ra[p_idx];
  2193. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  2194. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  2195. }
  2196. if (d < 0.001)
  2197. d = 0.001;
  2198. extents[p_idx] = d;
  2199. baker->set_extents(extents);
  2200. }
  2201. void BakedIndirectLightGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  2202. BakedLightmap *baker = Object::cast_to<BakedLightmap>(p_gizmo->get_spatial_node());
  2203. Vector3 restore = p_restore;
  2204. if (p_cancel) {
  2205. baker->set_extents(restore);
  2206. return;
  2207. }
  2208. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  2209. ur->create_action(TTR("Change Probe Extents"));
  2210. ur->add_do_method(baker, "set_extents", baker->get_extents());
  2211. ur->add_undo_method(baker, "set_extents", restore);
  2212. ur->commit_action();
  2213. }
  2214. bool BakedIndirectLightGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  2215. return Object::cast_to<BakedLightmap>(p_spatial) != NULL;
  2216. }
  2217. String BakedIndirectLightGizmoPlugin::get_name() const {
  2218. return "BakedLightmap";
  2219. }
  2220. int BakedIndirectLightGizmoPlugin::get_priority() const {
  2221. return -1;
  2222. }
  2223. void BakedIndirectLightGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  2224. BakedLightmap *baker = Object::cast_to<BakedLightmap>(p_gizmo->get_spatial_node());
  2225. Ref<Material> material = get_material("baked_indirect_light_material", p_gizmo);
  2226. Ref<Material> icon = get_material("baked_indirect_light_icon", p_gizmo);
  2227. Ref<Material> material_internal = get_material("baked_indirect_light_internal_material", p_gizmo);
  2228. p_gizmo->clear();
  2229. Vector<Vector3> lines;
  2230. Vector3 extents = baker->get_extents();
  2231. AABB aabb = AABB(-extents, extents * 2);
  2232. for (int i = 0; i < 12; i++) {
  2233. Vector3 a, b;
  2234. aabb.get_edge(i, a, b);
  2235. lines.push_back(a);
  2236. lines.push_back(b);
  2237. }
  2238. p_gizmo->add_lines(lines, material);
  2239. Vector<Vector3> handles;
  2240. for (int i = 0; i < 3; i++) {
  2241. Vector3 ax;
  2242. ax[i] = aabb.position[i] + aabb.size[i];
  2243. handles.push_back(ax);
  2244. }
  2245. if (p_gizmo->is_selected()) {
  2246. p_gizmo->add_solid_box(material_internal, aabb.get_size());
  2247. }
  2248. p_gizmo->add_unscaled_billboard(icon, 0.05);
  2249. p_gizmo->add_handles(handles, get_material("handles"));
  2250. }
  2251. ////
  2252. CollisionShapeSpatialGizmoPlugin::CollisionShapeSpatialGizmoPlugin() {
  2253. const Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/shape", Color(0.5, 0.7, 1));
  2254. create_material("shape_material", gizmo_color);
  2255. const float gizmo_value = gizmo_color.get_v();
  2256. const Color gizmo_color_disabled = Color(gizmo_value, gizmo_value, gizmo_value, 0.65);
  2257. create_material("shape_material_disabled", gizmo_color_disabled);
  2258. create_handle_material("handles");
  2259. }
  2260. bool CollisionShapeSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  2261. return Object::cast_to<CollisionShape>(p_spatial) != NULL;
  2262. }
  2263. String CollisionShapeSpatialGizmoPlugin::get_name() const {
  2264. return "CollisionShape";
  2265. }
  2266. int CollisionShapeSpatialGizmoPlugin::get_priority() const {
  2267. return -1;
  2268. }
  2269. String CollisionShapeSpatialGizmoPlugin::get_handle_name(const EditorSpatialGizmo *p_gizmo, int p_idx) const {
  2270. const CollisionShape *cs = Object::cast_to<CollisionShape>(p_gizmo->get_spatial_node());
  2271. Ref<Shape> s = cs->get_shape();
  2272. if (s.is_null())
  2273. return "";
  2274. if (Object::cast_to<SphereShape>(*s)) {
  2275. return "Radius";
  2276. }
  2277. if (Object::cast_to<BoxShape>(*s)) {
  2278. return "Extents";
  2279. }
  2280. if (Object::cast_to<CapsuleShape>(*s)) {
  2281. return p_idx == 0 ? "Radius" : "Height";
  2282. }
  2283. if (Object::cast_to<CylinderShape>(*s)) {
  2284. return p_idx == 0 ? "Radius" : "Height";
  2285. }
  2286. if (Object::cast_to<RayShape>(*s)) {
  2287. return "Length";
  2288. }
  2289. return "";
  2290. }
  2291. Variant CollisionShapeSpatialGizmoPlugin::get_handle_value(EditorSpatialGizmo *p_gizmo, int p_idx) const {
  2292. CollisionShape *cs = Object::cast_to<CollisionShape>(p_gizmo->get_spatial_node());
  2293. Ref<Shape> s = cs->get_shape();
  2294. if (s.is_null())
  2295. return Variant();
  2296. if (Object::cast_to<SphereShape>(*s)) {
  2297. Ref<SphereShape> ss = s;
  2298. return ss->get_radius();
  2299. }
  2300. if (Object::cast_to<BoxShape>(*s)) {
  2301. Ref<BoxShape> bs = s;
  2302. return bs->get_extents();
  2303. }
  2304. if (Object::cast_to<CapsuleShape>(*s)) {
  2305. Ref<CapsuleShape> cs2 = s;
  2306. return p_idx == 0 ? cs2->get_radius() : cs2->get_height();
  2307. }
  2308. if (Object::cast_to<CylinderShape>(*s)) {
  2309. Ref<CylinderShape> cs2 = s;
  2310. return p_idx == 0 ? cs2->get_radius() : cs2->get_height();
  2311. }
  2312. if (Object::cast_to<RayShape>(*s)) {
  2313. Ref<RayShape> cs2 = s;
  2314. return cs2->get_length();
  2315. }
  2316. return Variant();
  2317. }
  2318. void CollisionShapeSpatialGizmoPlugin::set_handle(EditorSpatialGizmo *p_gizmo, int p_idx, Camera *p_camera, const Point2 &p_point) {
  2319. CollisionShape *cs = Object::cast_to<CollisionShape>(p_gizmo->get_spatial_node());
  2320. Ref<Shape> s = cs->get_shape();
  2321. if (s.is_null())
  2322. return;
  2323. Transform gt = cs->get_global_transform();
  2324. Transform gi = gt.affine_inverse();
  2325. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  2326. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  2327. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  2328. if (Object::cast_to<SphereShape>(*s)) {
  2329. Ref<SphereShape> ss = s;
  2330. Vector3 ra, rb;
  2331. Geometry::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
  2332. float d = ra.x;
  2333. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  2334. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  2335. }
  2336. if (d < 0.001)
  2337. d = 0.001;
  2338. ss->set_radius(d);
  2339. }
  2340. if (Object::cast_to<RayShape>(*s)) {
  2341. Ref<RayShape> rs = s;
  2342. Vector3 ra, rb;
  2343. Geometry::get_closest_points_between_segments(Vector3(), Vector3(0, 0, 4096), sg[0], sg[1], ra, rb);
  2344. float d = ra.z;
  2345. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  2346. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  2347. }
  2348. if (d < 0.001)
  2349. d = 0.001;
  2350. rs->set_length(d);
  2351. }
  2352. if (Object::cast_to<BoxShape>(*s)) {
  2353. Vector3 axis;
  2354. axis[p_idx] = 1.0;
  2355. Ref<BoxShape> bs = s;
  2356. Vector3 ra, rb;
  2357. Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  2358. float d = ra[p_idx];
  2359. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  2360. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  2361. }
  2362. if (d < 0.001)
  2363. d = 0.001;
  2364. Vector3 he = bs->get_extents();
  2365. he[p_idx] = d;
  2366. bs->set_extents(he);
  2367. }
  2368. if (Object::cast_to<CapsuleShape>(*s)) {
  2369. Vector3 axis;
  2370. axis[p_idx == 0 ? 0 : 2] = 1.0;
  2371. Ref<CapsuleShape> cs2 = s;
  2372. Vector3 ra, rb;
  2373. Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  2374. float d = axis.dot(ra);
  2375. if (p_idx == 1)
  2376. d -= cs2->get_radius();
  2377. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  2378. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  2379. }
  2380. if (d < 0.001)
  2381. d = 0.001;
  2382. if (p_idx == 0)
  2383. cs2->set_radius(d);
  2384. else if (p_idx == 1)
  2385. cs2->set_height(d * 2.0);
  2386. }
  2387. if (Object::cast_to<CylinderShape>(*s)) {
  2388. Vector3 axis;
  2389. axis[p_idx == 0 ? 0 : 1] = 1.0;
  2390. Ref<CylinderShape> cs2 = s;
  2391. Vector3 ra, rb;
  2392. Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  2393. float d = axis.dot(ra);
  2394. if (SpatialEditor::get_singleton()->is_snap_enabled()) {
  2395. d = Math::stepify(d, SpatialEditor::get_singleton()->get_translate_snap());
  2396. }
  2397. if (d < 0.001)
  2398. d = 0.001;
  2399. if (p_idx == 0)
  2400. cs2->set_radius(d);
  2401. else if (p_idx == 1)
  2402. cs2->set_height(d * 2.0);
  2403. }
  2404. }
  2405. void CollisionShapeSpatialGizmoPlugin::commit_handle(EditorSpatialGizmo *p_gizmo, int p_idx, const Variant &p_restore, bool p_cancel) {
  2406. CollisionShape *cs = Object::cast_to<CollisionShape>(p_gizmo->get_spatial_node());
  2407. Ref<Shape> s = cs->get_shape();
  2408. if (s.is_null())
  2409. return;
  2410. if (Object::cast_to<SphereShape>(*s)) {
  2411. Ref<SphereShape> ss = s;
  2412. if (p_cancel) {
  2413. ss->set_radius(p_restore);
  2414. return;
  2415. }
  2416. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  2417. ur->create_action(TTR("Change Sphere Shape Radius"));
  2418. ur->add_do_method(ss.ptr(), "set_radius", ss->get_radius());
  2419. ur->add_undo_method(ss.ptr(), "set_radius", p_restore);
  2420. ur->commit_action();
  2421. }
  2422. if (Object::cast_to<BoxShape>(*s)) {
  2423. Ref<BoxShape> ss = s;
  2424. if (p_cancel) {
  2425. ss->set_extents(p_restore);
  2426. return;
  2427. }
  2428. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  2429. ur->create_action(TTR("Change Box Shape Extents"));
  2430. ur->add_do_method(ss.ptr(), "set_extents", ss->get_extents());
  2431. ur->add_undo_method(ss.ptr(), "set_extents", p_restore);
  2432. ur->commit_action();
  2433. }
  2434. if (Object::cast_to<CapsuleShape>(*s)) {
  2435. Ref<CapsuleShape> ss = s;
  2436. if (p_cancel) {
  2437. if (p_idx == 0)
  2438. ss->set_radius(p_restore);
  2439. else
  2440. ss->set_height(p_restore);
  2441. return;
  2442. }
  2443. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  2444. if (p_idx == 0) {
  2445. ur->create_action(TTR("Change Capsule Shape Radius"));
  2446. ur->add_do_method(ss.ptr(), "set_radius", ss->get_radius());
  2447. ur->add_undo_method(ss.ptr(), "set_radius", p_restore);
  2448. } else {
  2449. ur->create_action(TTR("Change Capsule Shape Height"));
  2450. ur->add_do_method(ss.ptr(), "set_height", ss->get_height());
  2451. ur->add_undo_method(ss.ptr(), "set_height", p_restore);
  2452. }
  2453. ur->commit_action();
  2454. }
  2455. if (Object::cast_to<CylinderShape>(*s)) {
  2456. Ref<CylinderShape> ss = s;
  2457. if (p_cancel) {
  2458. if (p_idx == 0)
  2459. ss->set_radius(p_restore);
  2460. else
  2461. ss->set_height(p_restore);
  2462. return;
  2463. }
  2464. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  2465. if (p_idx == 0) {
  2466. ur->create_action(TTR("Change Cylinder Shape Radius"));
  2467. ur->add_do_method(ss.ptr(), "set_radius", ss->get_radius());
  2468. ur->add_undo_method(ss.ptr(), "set_radius", p_restore);
  2469. } else {
  2470. ur->create_action(
  2471. ///
  2472. ////////
  2473. TTR("Change Cylinder Shape Height"));
  2474. ur->add_do_method(ss.ptr(), "set_height", ss->get_height());
  2475. ur->add_undo_method(ss.ptr(), "set_height", p_restore);
  2476. }
  2477. ur->commit_action();
  2478. }
  2479. if (Object::cast_to<RayShape>(*s)) {
  2480. Ref<RayShape> ss = s;
  2481. if (p_cancel) {
  2482. ss->set_length(p_restore);
  2483. return;
  2484. }
  2485. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  2486. ur->create_action(TTR("Change Ray Shape Length"));
  2487. ur->add_do_method(ss.ptr(), "set_length", ss->get_length());
  2488. ur->add_undo_method(ss.ptr(), "set_length", p_restore);
  2489. ur->commit_action();
  2490. }
  2491. }
  2492. void CollisionShapeSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  2493. CollisionShape *cs = Object::cast_to<CollisionShape>(p_gizmo->get_spatial_node());
  2494. p_gizmo->clear();
  2495. Ref<Shape> s = cs->get_shape();
  2496. if (s.is_null())
  2497. return;
  2498. const Ref<Material> material =
  2499. get_material(!cs->is_disabled() ? "shape_material" : "shape_material_disabled", p_gizmo);
  2500. Ref<Material> handles_material = get_material("handles");
  2501. if (Object::cast_to<SphereShape>(*s)) {
  2502. Ref<SphereShape> sp = s;
  2503. float r = sp->get_radius();
  2504. Vector<Vector3> points;
  2505. for (int i = 0; i <= 360; i++) {
  2506. float ra = Math::deg2rad((float)i);
  2507. float rb = Math::deg2rad((float)i + 1);
  2508. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  2509. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  2510. points.push_back(Vector3(a.x, 0, a.y));
  2511. points.push_back(Vector3(b.x, 0, b.y));
  2512. points.push_back(Vector3(0, a.x, a.y));
  2513. points.push_back(Vector3(0, b.x, b.y));
  2514. points.push_back(Vector3(a.x, a.y, 0));
  2515. points.push_back(Vector3(b.x, b.y, 0));
  2516. }
  2517. Vector<Vector3> collision_segments;
  2518. for (int i = 0; i < 64; i++) {
  2519. float ra = i * Math_PI * 2.0 / 64.0;
  2520. float rb = (i + 1) * Math_PI * 2.0 / 64.0;
  2521. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  2522. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  2523. collision_segments.push_back(Vector3(a.x, 0, a.y));
  2524. collision_segments.push_back(Vector3(b.x, 0, b.y));
  2525. collision_segments.push_back(Vector3(0, a.x, a.y));
  2526. collision_segments.push_back(Vector3(0, b.x, b.y));
  2527. collision_segments.push_back(Vector3(a.x, a.y, 0));
  2528. collision_segments.push_back(Vector3(b.x, b.y, 0));
  2529. }
  2530. p_gizmo->add_lines(points, material);
  2531. p_gizmo->add_collision_segments(collision_segments);
  2532. Vector<Vector3> handles;
  2533. handles.push_back(Vector3(r, 0, 0));
  2534. p_gizmo->add_handles(handles, handles_material);
  2535. }
  2536. if (Object::cast_to<BoxShape>(*s)) {
  2537. Ref<BoxShape> bs = s;
  2538. Vector<Vector3> lines;
  2539. AABB aabb;
  2540. aabb.position = -bs->get_extents();
  2541. aabb.size = aabb.position * -2;
  2542. for (int i = 0; i < 12; i++) {
  2543. Vector3 a, b;
  2544. aabb.get_edge(i, a, b);
  2545. lines.push_back(a);
  2546. lines.push_back(b);
  2547. }
  2548. Vector<Vector3> handles;
  2549. for (int i = 0; i < 3; i++) {
  2550. Vector3 ax;
  2551. ax[i] = bs->get_extents()[i];
  2552. handles.push_back(ax);
  2553. }
  2554. p_gizmo->add_lines(lines, material);
  2555. p_gizmo->add_collision_segments(lines);
  2556. p_gizmo->add_handles(handles, handles_material);
  2557. }
  2558. if (Object::cast_to<CapsuleShape>(*s)) {
  2559. Ref<CapsuleShape> cs2 = s;
  2560. float radius = cs2->get_radius();
  2561. float height = cs2->get_height();
  2562. Vector<Vector3> points;
  2563. Vector3 d(0, 0, height * 0.5);
  2564. for (int i = 0; i < 360; i++) {
  2565. float ra = Math::deg2rad((float)i);
  2566. float rb = Math::deg2rad((float)i + 1);
  2567. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
  2568. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;
  2569. points.push_back(Vector3(a.x, a.y, 0) + d);
  2570. points.push_back(Vector3(b.x, b.y, 0) + d);
  2571. points.push_back(Vector3(a.x, a.y, 0) - d);
  2572. points.push_back(Vector3(b.x, b.y, 0) - d);
  2573. if (i % 90 == 0) {
  2574. points.push_back(Vector3(a.x, a.y, 0) + d);
  2575. points.push_back(Vector3(a.x, a.y, 0) - d);
  2576. }
  2577. Vector3 dud = i < 180 ? d : -d;
  2578. points.push_back(Vector3(0, a.y, a.x) + dud);
  2579. points.push_back(Vector3(0, b.y, b.x) + dud);
  2580. points.push_back(Vector3(a.y, 0, a.x) + dud);
  2581. points.push_back(Vector3(b.y, 0, b.x) + dud);
  2582. }
  2583. p_gizmo->add_lines(points, material);
  2584. Vector<Vector3> collision_segments;
  2585. for (int i = 0; i < 64; i++) {
  2586. float ra = i * Math_PI * 2.0 / 64.0;
  2587. float rb = (i + 1) * Math_PI * 2.0 / 64.0;
  2588. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
  2589. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;
  2590. collision_segments.push_back(Vector3(a.x, a.y, 0) + d);
  2591. collision_segments.push_back(Vector3(b.x, b.y, 0) + d);
  2592. collision_segments.push_back(Vector3(a.x, a.y, 0) - d);
  2593. collision_segments.push_back(Vector3(b.x, b.y, 0) - d);
  2594. if (i % 16 == 0) {
  2595. collision_segments.push_back(Vector3(a.x, a.y, 0) + d);
  2596. collision_segments.push_back(Vector3(a.x, a.y, 0) - d);
  2597. }
  2598. Vector3 dud = i < 32 ? d : -d;
  2599. collision_segments.push_back(Vector3(0, a.y, a.x) + dud);
  2600. collision_segments.push_back(Vector3(0, b.y, b.x) + dud);
  2601. collision_segments.push_back(Vector3(a.y, 0, a.x) + dud);
  2602. collision_segments.push_back(Vector3(b.y, 0, b.x) + dud);
  2603. }
  2604. p_gizmo->add_collision_segments(collision_segments);
  2605. Vector<Vector3> handles;
  2606. handles.push_back(Vector3(cs2->get_radius(), 0, 0));
  2607. handles.push_back(Vector3(0, 0, cs2->get_height() * 0.5 + cs2->get_radius()));
  2608. p_gizmo->add_handles(handles, handles_material);
  2609. }
  2610. if (Object::cast_to<CylinderShape>(*s)) {
  2611. Ref<CylinderShape> cs2 = s;
  2612. float radius = cs2->get_radius();
  2613. float height = cs2->get_height();
  2614. Vector<Vector3> points;
  2615. Vector3 d(0, height * 0.5, 0);
  2616. for (int i = 0; i < 360; i++) {
  2617. float ra = Math::deg2rad((float)i);
  2618. float rb = Math::deg2rad((float)i + 1);
  2619. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
  2620. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;
  2621. points.push_back(Vector3(a.x, 0, a.y) + d);
  2622. points.push_back(Vector3(b.x, 0, b.y) + d);
  2623. points.push_back(Vector3(a.x, 0, a.y) - d);
  2624. points.push_back(Vector3(b.x, 0, b.y) - d);
  2625. if (i % 90 == 0) {
  2626. points.push_back(Vector3(a.x, 0, a.y) + d);
  2627. points.push_back(Vector3(a.x, 0, a.y) - d);
  2628. }
  2629. }
  2630. p_gizmo->add_lines(points, material);
  2631. Vector<Vector3> collision_segments;
  2632. for (int i = 0; i < 64; i++) {
  2633. float ra = i * Math_PI * 2.0 / 64.0;
  2634. float rb = (i + 1) * Math_PI * 2.0 / 64.0;
  2635. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
  2636. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;
  2637. collision_segments.push_back(Vector3(a.x, 0, a.y) + d);
  2638. collision_segments.push_back(Vector3(b.x, 0, b.y) + d);
  2639. collision_segments.push_back(Vector3(a.x, 0, a.y) - d);
  2640. collision_segments.push_back(Vector3(b.x, 0, b.y) - d);
  2641. if (i % 16 == 0) {
  2642. collision_segments.push_back(Vector3(a.x, 0, a.y) + d);
  2643. collision_segments.push_back(Vector3(a.x, 0, a.y) - d);
  2644. }
  2645. }
  2646. p_gizmo->add_collision_segments(collision_segments);
  2647. Vector<Vector3> handles;
  2648. handles.push_back(Vector3(cs2->get_radius(), 0, 0));
  2649. handles.push_back(Vector3(0, cs2->get_height() * 0.5, 0));
  2650. p_gizmo->add_handles(handles, handles_material);
  2651. }
  2652. if (Object::cast_to<PlaneShape>(*s)) {
  2653. Ref<PlaneShape> ps = s;
  2654. Plane p = ps->get_plane();
  2655. Vector<Vector3> points;
  2656. Vector3 n1 = p.get_any_perpendicular_normal();
  2657. Vector3 n2 = p.normal.cross(n1).normalized();
  2658. Vector3 pface[4] = {
  2659. p.normal * p.d + n1 * 10.0 + n2 * 10.0,
  2660. p.normal * p.d + n1 * 10.0 + n2 * -10.0,
  2661. p.normal * p.d + n1 * -10.0 + n2 * -10.0,
  2662. p.normal * p.d + n1 * -10.0 + n2 * 10.0,
  2663. };
  2664. points.push_back(pface[0]);
  2665. points.push_back(pface[1]);
  2666. points.push_back(pface[1]);
  2667. points.push_back(pface[2]);
  2668. points.push_back(pface[2]);
  2669. points.push_back(pface[3]);
  2670. points.push_back(pface[3]);
  2671. points.push_back(pface[0]);
  2672. points.push_back(p.normal * p.d);
  2673. points.push_back(p.normal * p.d + p.normal * 3);
  2674. p_gizmo->add_lines(points, material);
  2675. p_gizmo->add_collision_segments(points);
  2676. }
  2677. if (Object::cast_to<ConvexPolygonShape>(*s)) {
  2678. PoolVector<Vector3> points = Object::cast_to<ConvexPolygonShape>(*s)->get_points();
  2679. if (points.size() > 3) {
  2680. Vector<Vector3> varr = Variant(points);
  2681. Geometry::MeshData md;
  2682. Error err = QuickHull::build(varr, md);
  2683. if (err == OK) {
  2684. Vector<Vector3> points2;
  2685. points2.resize(md.edges.size() * 2);
  2686. for (int i = 0; i < md.edges.size(); i++) {
  2687. points2.write[i * 2 + 0] = md.vertices[md.edges[i].a];
  2688. points2.write[i * 2 + 1] = md.vertices[md.edges[i].b];
  2689. }
  2690. p_gizmo->add_lines(points2, material);
  2691. p_gizmo->add_collision_segments(points2);
  2692. }
  2693. }
  2694. }
  2695. if (Object::cast_to<ConcavePolygonShape>(*s)) {
  2696. Ref<ConcavePolygonShape> cs2 = s;
  2697. Ref<ArrayMesh> mesh = cs2->get_debug_mesh();
  2698. p_gizmo->add_mesh(mesh, false, Ref<SkinReference>(), material);
  2699. p_gizmo->add_collision_segments(cs2->get_debug_mesh_lines());
  2700. }
  2701. if (Object::cast_to<RayShape>(*s)) {
  2702. Ref<RayShape> rs = s;
  2703. Vector<Vector3> points;
  2704. points.push_back(Vector3());
  2705. points.push_back(Vector3(0, 0, rs->get_length()));
  2706. p_gizmo->add_lines(points, material);
  2707. p_gizmo->add_collision_segments(points);
  2708. Vector<Vector3> handles;
  2709. handles.push_back(Vector3(0, 0, rs->get_length()));
  2710. p_gizmo->add_handles(handles, handles_material);
  2711. }
  2712. if (Object::cast_to<HeightMapShape>(*s)) {
  2713. Ref<HeightMapShape> hms = s;
  2714. Ref<ArrayMesh> mesh = hms->get_debug_mesh();
  2715. p_gizmo->add_mesh(mesh, false, Ref<SkinReference>(), material);
  2716. }
  2717. }
  2718. /////
  2719. CollisionPolygonSpatialGizmoPlugin::CollisionPolygonSpatialGizmoPlugin() {
  2720. const Color gizmo_color = EDITOR_DEF("editors/3d_gizmos/gizmo_colors/shape", Color(0.5, 0.7, 1));
  2721. create_material("shape_material", gizmo_color);
  2722. const float gizmo_value = gizmo_color.get_v();
  2723. const Color gizmo_color_disabled = Color(gizmo_value, gizmo_value, gizmo_value, 0.65);
  2724. create_material("shape_material_disabled", gizmo_color_disabled);
  2725. }
  2726. bool CollisionPolygonSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  2727. return Object::cast_to<CollisionPolygon>(p_spatial) != NULL;
  2728. }
  2729. String CollisionPolygonSpatialGizmoPlugin::get_name() const {
  2730. return "CollisionPolygon";
  2731. }
  2732. int CollisionPolygonSpatialGizmoPlugin::get_priority() const {
  2733. return -1;
  2734. }
  2735. void CollisionPolygonSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  2736. CollisionPolygon *polygon = Object::cast_to<CollisionPolygon>(p_gizmo->get_spatial_node());
  2737. p_gizmo->clear();
  2738. Vector<Vector2> points = polygon->get_polygon();
  2739. float depth = polygon->get_depth() * 0.5;
  2740. Vector<Vector3> lines;
  2741. for (int i = 0; i < points.size(); i++) {
  2742. int n = (i + 1) % points.size();
  2743. lines.push_back(Vector3(points[i].x, points[i].y, depth));
  2744. lines.push_back(Vector3(points[n].x, points[n].y, depth));
  2745. lines.push_back(Vector3(points[i].x, points[i].y, -depth));
  2746. lines.push_back(Vector3(points[n].x, points[n].y, -depth));
  2747. lines.push_back(Vector3(points[i].x, points[i].y, depth));
  2748. lines.push_back(Vector3(points[i].x, points[i].y, -depth));
  2749. }
  2750. const Ref<Material> material =
  2751. get_material(!polygon->is_disabled() ? "shape_material" : "shape_material_disabled", p_gizmo);
  2752. p_gizmo->add_lines(lines, material);
  2753. p_gizmo->add_collision_segments(lines);
  2754. }
  2755. ////
  2756. NavigationMeshSpatialGizmoPlugin::NavigationMeshSpatialGizmoPlugin() {
  2757. create_material("navigation_edge_material", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/navigation_edge", Color(0.5, 1, 1)));
  2758. create_material("navigation_edge_material_disabled", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/navigation_edge_disabled", Color(0.7, 0.7, 0.7)));
  2759. create_material("navigation_solid_material", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/navigation_solid", Color(0.5, 1, 1, 0.4)));
  2760. 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)));
  2761. }
  2762. bool NavigationMeshSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  2763. return Object::cast_to<NavigationMeshInstance>(p_spatial) != NULL;
  2764. }
  2765. String NavigationMeshSpatialGizmoPlugin::get_name() const {
  2766. return "NavigationMeshInstance";
  2767. }
  2768. int NavigationMeshSpatialGizmoPlugin::get_priority() const {
  2769. return -1;
  2770. }
  2771. void NavigationMeshSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  2772. NavigationMeshInstance *navmesh = Object::cast_to<NavigationMeshInstance>(p_gizmo->get_spatial_node());
  2773. Ref<Material> edge_material = get_material("navigation_edge_material", p_gizmo);
  2774. Ref<Material> edge_material_disabled = get_material("navigation_edge_material_disabled", p_gizmo);
  2775. Ref<Material> solid_material = get_material("navigation_solid_material", p_gizmo);
  2776. Ref<Material> solid_material_disabled = get_material("navigation_solid_material_disabled", p_gizmo);
  2777. p_gizmo->clear();
  2778. Ref<NavigationMesh> navmeshie = navmesh->get_navigation_mesh();
  2779. if (navmeshie.is_null())
  2780. return;
  2781. PoolVector<Vector3> vertices = navmeshie->get_vertices();
  2782. PoolVector<Vector3>::Read vr = vertices.read();
  2783. List<Face3> faces;
  2784. for (int i = 0; i < navmeshie->get_polygon_count(); i++) {
  2785. Vector<int> p = navmeshie->get_polygon(i);
  2786. for (int j = 2; j < p.size(); j++) {
  2787. Face3 f;
  2788. f.vertex[0] = vr[p[0]];
  2789. f.vertex[1] = vr[p[j - 1]];
  2790. f.vertex[2] = vr[p[j]];
  2791. faces.push_back(f);
  2792. }
  2793. }
  2794. if (faces.empty())
  2795. return;
  2796. Map<_EdgeKey, bool> edge_map;
  2797. PoolVector<Vector3> tmeshfaces;
  2798. tmeshfaces.resize(faces.size() * 3);
  2799. {
  2800. PoolVector<Vector3>::Write tw = tmeshfaces.write();
  2801. int tidx = 0;
  2802. for (List<Face3>::Element *E = faces.front(); E; E = E->next()) {
  2803. const Face3 &f = E->get();
  2804. for (int j = 0; j < 3; j++) {
  2805. tw[tidx++] = f.vertex[j];
  2806. _EdgeKey ek;
  2807. ek.from = f.vertex[j].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON));
  2808. ek.to = f.vertex[(j + 1) % 3].snapped(Vector3(CMP_EPSILON, CMP_EPSILON, CMP_EPSILON));
  2809. if (ek.from < ek.to)
  2810. SWAP(ek.from, ek.to);
  2811. Map<_EdgeKey, bool>::Element *F = edge_map.find(ek);
  2812. if (F) {
  2813. F->get() = false;
  2814. } else {
  2815. edge_map[ek] = true;
  2816. }
  2817. }
  2818. }
  2819. }
  2820. Vector<Vector3> lines;
  2821. for (Map<_EdgeKey, bool>::Element *E = edge_map.front(); E; E = E->next()) {
  2822. if (E->get()) {
  2823. lines.push_back(E->key().from);
  2824. lines.push_back(E->key().to);
  2825. }
  2826. }
  2827. Ref<TriangleMesh> tmesh = memnew(TriangleMesh);
  2828. tmesh->create(tmeshfaces);
  2829. if (lines.size())
  2830. p_gizmo->add_lines(lines, navmesh->is_enabled() ? edge_material : edge_material_disabled);
  2831. p_gizmo->add_collision_triangles(tmesh);
  2832. Ref<ArrayMesh> m = memnew(ArrayMesh);
  2833. Array a;
  2834. a.resize(Mesh::ARRAY_MAX);
  2835. a[0] = tmeshfaces;
  2836. m->add_surface_from_arrays(Mesh::PRIMITIVE_TRIANGLES, a);
  2837. m->surface_set_material(0, navmesh->is_enabled() ? solid_material : solid_material_disabled);
  2838. p_gizmo->add_mesh(m);
  2839. p_gizmo->add_collision_segments(lines);
  2840. }
  2841. //////
  2842. #define BODY_A_RADIUS 0.25
  2843. #define BODY_B_RADIUS 0.27
  2844. Basis JointGizmosDrawer::look_body(const Transform &p_joint_transform, const Transform &p_body_transform) {
  2845. const Vector3 &p_eye(p_joint_transform.origin);
  2846. const Vector3 &p_target(p_body_transform.origin);
  2847. Vector3 v_x, v_y, v_z;
  2848. // Look the body with X
  2849. v_x = p_target - p_eye;
  2850. v_x.normalize();
  2851. v_z = v_x.cross(Vector3(0, 1, 0));
  2852. v_z.normalize();
  2853. v_y = v_z.cross(v_x);
  2854. v_y.normalize();
  2855. Basis base;
  2856. base.set(v_x, v_y, v_z);
  2857. // Absorb current joint transform
  2858. base = p_joint_transform.basis.inverse() * base;
  2859. return base;
  2860. }
  2861. Basis JointGizmosDrawer::look_body_toward(Vector3::Axis p_axis, const Transform &joint_transform, const Transform &body_transform) {
  2862. switch (p_axis) {
  2863. case Vector3::AXIS_X:
  2864. return look_body_toward_x(joint_transform, body_transform);
  2865. case Vector3::AXIS_Y:
  2866. return look_body_toward_y(joint_transform, body_transform);
  2867. case Vector3::AXIS_Z:
  2868. return look_body_toward_z(joint_transform, body_transform);
  2869. default:
  2870. return Basis();
  2871. }
  2872. }
  2873. Basis JointGizmosDrawer::look_body_toward_x(const Transform &p_joint_transform, const Transform &p_body_transform) {
  2874. const Vector3 &p_eye(p_joint_transform.origin);
  2875. const Vector3 &p_target(p_body_transform.origin);
  2876. const Vector3 p_front(p_joint_transform.basis.get_axis(0));
  2877. Vector3 v_x, v_y, v_z;
  2878. // Look the body with X
  2879. v_x = p_target - p_eye;
  2880. v_x.normalize();
  2881. v_y = p_front.cross(v_x);
  2882. v_y.normalize();
  2883. v_z = v_y.cross(p_front);
  2884. v_z.normalize();
  2885. // Clamp X to FRONT axis
  2886. v_x = p_front;
  2887. v_x.normalize();
  2888. Basis base;
  2889. base.set(v_x, v_y, v_z);
  2890. // Absorb current joint transform
  2891. base = p_joint_transform.basis.inverse() * base;
  2892. return base;
  2893. }
  2894. Basis JointGizmosDrawer::look_body_toward_y(const Transform &p_joint_transform, const Transform &p_body_transform) {
  2895. const Vector3 &p_eye(p_joint_transform.origin);
  2896. const Vector3 &p_target(p_body_transform.origin);
  2897. const Vector3 p_up(p_joint_transform.basis.get_axis(1));
  2898. Vector3 v_x, v_y, v_z;
  2899. // Look the body with X
  2900. v_x = p_target - p_eye;
  2901. v_x.normalize();
  2902. v_z = v_x.cross(p_up);
  2903. v_z.normalize();
  2904. v_x = p_up.cross(v_z);
  2905. v_x.normalize();
  2906. // Clamp Y to UP axis
  2907. v_y = p_up;
  2908. v_y.normalize();
  2909. Basis base;
  2910. base.set(v_x, v_y, v_z);
  2911. // Absorb current joint transform
  2912. base = p_joint_transform.basis.inverse() * base;
  2913. return base;
  2914. }
  2915. Basis JointGizmosDrawer::look_body_toward_z(const Transform &p_joint_transform, const Transform &p_body_transform) {
  2916. const Vector3 &p_eye(p_joint_transform.origin);
  2917. const Vector3 &p_target(p_body_transform.origin);
  2918. const Vector3 p_lateral(p_joint_transform.basis.get_axis(2));
  2919. Vector3 v_x, v_y, v_z;
  2920. // Look the body with X
  2921. v_x = p_target - p_eye;
  2922. v_x.normalize();
  2923. v_z = p_lateral;
  2924. v_z.normalize();
  2925. v_y = v_z.cross(v_x);
  2926. v_y.normalize();
  2927. // Clamp X to Z axis
  2928. v_x = v_y.cross(v_z);
  2929. v_x.normalize();
  2930. Basis base;
  2931. base.set(v_x, v_y, v_z);
  2932. // Absorb current joint transform
  2933. base = p_joint_transform.basis.inverse() * base;
  2934. return base;
  2935. }
  2936. 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) {
  2937. if (p_limit_lower == p_limit_upper) {
  2938. r_points.push_back(p_offset.translated(Vector3()).origin);
  2939. r_points.push_back(p_offset.translated(p_base.xform(Vector3(0.5, 0, 0))).origin);
  2940. } else {
  2941. if (p_limit_lower > p_limit_upper) {
  2942. p_limit_lower = -Math_PI;
  2943. p_limit_upper = Math_PI;
  2944. }
  2945. const int points = 32;
  2946. for (int i = 0; i < points; i++) {
  2947. real_t s = p_limit_lower + i * (p_limit_upper - p_limit_lower) / points;
  2948. real_t n = p_limit_lower + (i + 1) * (p_limit_upper - p_limit_lower) / points;
  2949. Vector3 from;
  2950. Vector3 to;
  2951. switch (p_axis) {
  2952. case Vector3::AXIS_X:
  2953. if (p_inverse) {
  2954. from = p_base.xform(Vector3(0, Math::sin(s), Math::cos(s))) * p_radius;
  2955. to = p_base.xform(Vector3(0, Math::sin(n), Math::cos(n))) * p_radius;
  2956. } else {
  2957. from = p_base.xform(Vector3(0, -Math::sin(s), Math::cos(s))) * p_radius;
  2958. to = p_base.xform(Vector3(0, -Math::sin(n), Math::cos(n))) * p_radius;
  2959. }
  2960. break;
  2961. case Vector3::AXIS_Y:
  2962. if (p_inverse) {
  2963. from = p_base.xform(Vector3(Math::cos(s), 0, -Math::sin(s))) * p_radius;
  2964. to = p_base.xform(Vector3(Math::cos(n), 0, -Math::sin(n))) * p_radius;
  2965. } else {
  2966. from = p_base.xform(Vector3(Math::cos(s), 0, Math::sin(s))) * p_radius;
  2967. to = p_base.xform(Vector3(Math::cos(n), 0, Math::sin(n))) * p_radius;
  2968. }
  2969. break;
  2970. case Vector3::AXIS_Z:
  2971. from = p_base.xform(Vector3(Math::cos(s), Math::sin(s), 0)) * p_radius;
  2972. to = p_base.xform(Vector3(Math::cos(n), Math::sin(n), 0)) * p_radius;
  2973. break;
  2974. }
  2975. if (i == points - 1) {
  2976. r_points.push_back(p_offset.translated(to).origin);
  2977. r_points.push_back(p_offset.translated(Vector3()).origin);
  2978. }
  2979. if (i == 0) {
  2980. r_points.push_back(p_offset.translated(from).origin);
  2981. r_points.push_back(p_offset.translated(Vector3()).origin);
  2982. }
  2983. r_points.push_back(p_offset.translated(from).origin);
  2984. r_points.push_back(p_offset.translated(to).origin);
  2985. }
  2986. r_points.push_back(p_offset.translated(Vector3(0, p_radius * 1.5, 0)).origin);
  2987. r_points.push_back(p_offset.translated(Vector3()).origin);
  2988. }
  2989. }
  2990. void JointGizmosDrawer::draw_cone(const Transform &p_offset, const Basis &p_base, real_t p_swing, real_t p_twist, Vector<Vector3> &r_points) {
  2991. float r = 1.0;
  2992. float w = r * Math::sin(p_swing);
  2993. float d = r * Math::cos(p_swing);
  2994. //swing
  2995. for (int i = 0; i < 360; i += 10) {
  2996. float ra = Math::deg2rad((float)i);
  2997. float rb = Math::deg2rad((float)i + 10);
  2998. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;
  2999. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;
  3000. r_points.push_back(p_offset.translated(p_base.xform(Vector3(d, a.x, a.y))).origin);
  3001. r_points.push_back(p_offset.translated(p_base.xform(Vector3(d, b.x, b.y))).origin);
  3002. if (i % 90 == 0) {
  3003. r_points.push_back(p_offset.translated(p_base.xform(Vector3(d, a.x, a.y))).origin);
  3004. r_points.push_back(p_offset.translated(p_base.xform(Vector3())).origin);
  3005. }
  3006. }
  3007. r_points.push_back(p_offset.translated(p_base.xform(Vector3())).origin);
  3008. r_points.push_back(p_offset.translated(p_base.xform(Vector3(1, 0, 0))).origin);
  3009. /// Twist
  3010. float ts = Math::rad2deg(p_twist);
  3011. ts = MIN(ts, 720);
  3012. for (int i = 0; i < int(ts); i += 5) {
  3013. float ra = Math::deg2rad((float)i);
  3014. float rb = Math::deg2rad((float)i + 5);
  3015. float c = i / 720.0;
  3016. float cn = (i + 5) / 720.0;
  3017. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w * c;
  3018. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w * cn;
  3019. r_points.push_back(p_offset.translated(p_base.xform(Vector3(c, a.x, a.y))).origin);
  3020. r_points.push_back(p_offset.translated(p_base.xform(Vector3(cn, b.x, b.y))).origin);
  3021. }
  3022. }
  3023. ////
  3024. JointSpatialGizmoPlugin::JointSpatialGizmoPlugin() {
  3025. create_material("joint_material", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/joint", Color(0.5, 0.8, 1)));
  3026. create_material("joint_body_a_material", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/joint_body_a", Color(0.6, 0.8, 1)));
  3027. create_material("joint_body_b_material", EDITOR_DEF("editors/3d_gizmos/gizmo_colors/joint_body_b", Color(0.6, 0.9, 1)));
  3028. }
  3029. bool JointSpatialGizmoPlugin::has_gizmo(Spatial *p_spatial) {
  3030. return Object::cast_to<Joint>(p_spatial) != NULL;
  3031. }
  3032. String JointSpatialGizmoPlugin::get_name() const {
  3033. return "Joints";
  3034. }
  3035. int JointSpatialGizmoPlugin::get_priority() const {
  3036. return -1;
  3037. }
  3038. void JointSpatialGizmoPlugin::redraw(EditorSpatialGizmo *p_gizmo) {
  3039. Joint *joint = Object::cast_to<Joint>(p_gizmo->get_spatial_node());
  3040. p_gizmo->clear();
  3041. Spatial *node_body_a = NULL;
  3042. if (!joint->get_node_a().is_empty()) {
  3043. node_body_a = Object::cast_to<Spatial>(joint->get_node(joint->get_node_a()));
  3044. }
  3045. Spatial *node_body_b = NULL;
  3046. if (!joint->get_node_b().is_empty()) {
  3047. node_body_b = Object::cast_to<Spatial>(joint->get_node(joint->get_node_b()));
  3048. }
  3049. if (!node_body_a && !node_body_b) {
  3050. return;
  3051. }
  3052. Ref<Material> common_material = get_material("joint_material", p_gizmo);
  3053. Ref<Material> body_a_material = get_material("joint_body_a_material", p_gizmo);
  3054. Ref<Material> body_b_material = get_material("joint_body_b_material", p_gizmo);
  3055. Vector<Vector3> points;
  3056. Vector<Vector3> body_a_points;
  3057. Vector<Vector3> body_b_points;
  3058. if (Object::cast_to<PinJoint>(joint)) {
  3059. CreatePinJointGizmo(Transform(), points);
  3060. p_gizmo->add_collision_segments(points);
  3061. p_gizmo->add_lines(points, common_material);
  3062. }
  3063. HingeJoint *hinge = Object::cast_to<HingeJoint>(joint);
  3064. if (hinge) {
  3065. CreateHingeJointGizmo(
  3066. Transform(),
  3067. hinge->get_global_transform(),
  3068. node_body_a ? node_body_a->get_global_transform() : Transform(),
  3069. node_body_b ? node_body_b->get_global_transform() : Transform(),
  3070. hinge->get_param(HingeJoint::PARAM_LIMIT_LOWER),
  3071. hinge->get_param(HingeJoint::PARAM_LIMIT_UPPER),
  3072. hinge->get_flag(HingeJoint::FLAG_USE_LIMIT),
  3073. points,
  3074. node_body_a ? &body_a_points : NULL,
  3075. node_body_b ? &body_b_points : NULL);
  3076. p_gizmo->add_collision_segments(points);
  3077. p_gizmo->add_collision_segments(body_a_points);
  3078. p_gizmo->add_collision_segments(body_b_points);
  3079. p_gizmo->add_lines(points, common_material);
  3080. p_gizmo->add_lines(body_a_points, body_a_material);
  3081. p_gizmo->add_lines(body_b_points, body_b_material);
  3082. }
  3083. SliderJoint *slider = Object::cast_to<SliderJoint>(joint);
  3084. if (slider) {
  3085. CreateSliderJointGizmo(
  3086. Transform(),
  3087. slider->get_global_transform(),
  3088. node_body_a ? node_body_a->get_global_transform() : Transform(),
  3089. node_body_b ? node_body_b->get_global_transform() : Transform(),
  3090. slider->get_param(SliderJoint::PARAM_ANGULAR_LIMIT_LOWER),
  3091. slider->get_param(SliderJoint::PARAM_ANGULAR_LIMIT_UPPER),
  3092. slider->get_param(SliderJoint::PARAM_LINEAR_LIMIT_LOWER),
  3093. slider->get_param(SliderJoint::PARAM_LINEAR_LIMIT_UPPER),
  3094. points,
  3095. node_body_a ? &body_a_points : NULL,
  3096. node_body_b ? &body_b_points : NULL);
  3097. p_gizmo->add_collision_segments(points);
  3098. p_gizmo->add_collision_segments(body_a_points);
  3099. p_gizmo->add_collision_segments(body_b_points);
  3100. p_gizmo->add_lines(points, common_material);
  3101. p_gizmo->add_lines(body_a_points, body_a_material);
  3102. p_gizmo->add_lines(body_b_points, body_b_material);
  3103. }
  3104. ConeTwistJoint *cone = Object::cast_to<ConeTwistJoint>(joint);
  3105. if (cone) {
  3106. CreateConeTwistJointGizmo(
  3107. Transform(),
  3108. cone->get_global_transform(),
  3109. node_body_a ? node_body_a->get_global_transform() : Transform(),
  3110. node_body_b ? node_body_b->get_global_transform() : Transform(),
  3111. cone->get_param(ConeTwistJoint::PARAM_SWING_SPAN),
  3112. cone->get_param(ConeTwistJoint::PARAM_TWIST_SPAN),
  3113. node_body_a ? &body_a_points : NULL,
  3114. node_body_b ? &body_b_points : NULL);
  3115. p_gizmo->add_collision_segments(body_a_points);
  3116. p_gizmo->add_collision_segments(body_b_points);
  3117. p_gizmo->add_lines(body_a_points, body_a_material);
  3118. p_gizmo->add_lines(body_b_points, body_b_material);
  3119. }
  3120. Generic6DOFJoint *gen = Object::cast_to<Generic6DOFJoint>(joint);
  3121. if (gen) {
  3122. CreateGeneric6DOFJointGizmo(
  3123. Transform(),
  3124. gen->get_global_transform(),
  3125. node_body_a ? node_body_a->get_global_transform() : Transform(),
  3126. node_body_b ? node_body_b->get_global_transform() : Transform(),
  3127. gen->get_param_x(Generic6DOFJoint::PARAM_ANGULAR_LOWER_LIMIT),
  3128. gen->get_param_x(Generic6DOFJoint::PARAM_ANGULAR_UPPER_LIMIT),
  3129. gen->get_param_x(Generic6DOFJoint::PARAM_LINEAR_LOWER_LIMIT),
  3130. gen->get_param_x(Generic6DOFJoint::PARAM_LINEAR_UPPER_LIMIT),
  3131. gen->get_flag_x(Generic6DOFJoint::FLAG_ENABLE_ANGULAR_LIMIT),
  3132. gen->get_flag_x(Generic6DOFJoint::FLAG_ENABLE_LINEAR_LIMIT),
  3133. gen->get_param_y(Generic6DOFJoint::PARAM_ANGULAR_LOWER_LIMIT),
  3134. gen->get_param_y(Generic6DOFJoint::PARAM_ANGULAR_UPPER_LIMIT),
  3135. gen->get_param_y(Generic6DOFJoint::PARAM_LINEAR_LOWER_LIMIT),
  3136. gen->get_param_y(Generic6DOFJoint::PARAM_LINEAR_UPPER_LIMIT),
  3137. gen->get_flag_y(Generic6DOFJoint::FLAG_ENABLE_ANGULAR_LIMIT),
  3138. gen->get_flag_y(Generic6DOFJoint::FLAG_ENABLE_LINEAR_LIMIT),
  3139. gen->get_param_z(Generic6DOFJoint::PARAM_ANGULAR_LOWER_LIMIT),
  3140. gen->get_param_z(Generic6DOFJoint::PARAM_ANGULAR_UPPER_LIMIT),
  3141. gen->get_param_z(Generic6DOFJoint::PARAM_LINEAR_LOWER_LIMIT),
  3142. gen->get_param_z(Generic6DOFJoint::PARAM_LINEAR_UPPER_LIMIT),
  3143. gen->get_flag_z(Generic6DOFJoint::FLAG_ENABLE_ANGULAR_LIMIT),
  3144. gen->get_flag_z(Generic6DOFJoint::FLAG_ENABLE_LINEAR_LIMIT),
  3145. points,
  3146. node_body_a ? &body_a_points : NULL,
  3147. node_body_a ? &body_b_points : NULL);
  3148. p_gizmo->add_collision_segments(points);
  3149. p_gizmo->add_collision_segments(body_a_points);
  3150. p_gizmo->add_collision_segments(body_b_points);
  3151. p_gizmo->add_lines(points, common_material);
  3152. p_gizmo->add_lines(body_a_points, body_a_material);
  3153. p_gizmo->add_lines(body_b_points, body_b_material);
  3154. }
  3155. }
  3156. void JointSpatialGizmoPlugin::CreatePinJointGizmo(const Transform &p_offset, Vector<Vector3> &r_cursor_points) {
  3157. float cs = 0.25;
  3158. r_cursor_points.push_back(p_offset.translated(Vector3(+cs, 0, 0)).origin);
  3159. r_cursor_points.push_back(p_offset.translated(Vector3(-cs, 0, 0)).origin);
  3160. r_cursor_points.push_back(p_offset.translated(Vector3(0, +cs, 0)).origin);
  3161. r_cursor_points.push_back(p_offset.translated(Vector3(0, -cs, 0)).origin);
  3162. r_cursor_points.push_back(p_offset.translated(Vector3(0, 0, +cs)).origin);
  3163. r_cursor_points.push_back(p_offset.translated(Vector3(0, 0, -cs)).origin);
  3164. }
  3165. 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) {
  3166. r_common_points.push_back(p_offset.translated(Vector3(0, 0, 0.5)).origin);
  3167. r_common_points.push_back(p_offset.translated(Vector3(0, 0, -0.5)).origin);
  3168. if (!p_use_limit) {
  3169. p_limit_upper = -1;
  3170. p_limit_lower = 0;
  3171. }
  3172. if (r_body_a_points) {
  3173. JointGizmosDrawer::draw_circle(Vector3::AXIS_Z,
  3174. BODY_A_RADIUS,
  3175. p_offset,
  3176. JointGizmosDrawer::look_body_toward_z(p_trs_joint, p_trs_body_a),
  3177. p_limit_lower,
  3178. p_limit_upper,
  3179. *r_body_a_points);
  3180. }
  3181. if (r_body_b_points) {
  3182. JointGizmosDrawer::draw_circle(Vector3::AXIS_Z,
  3183. BODY_B_RADIUS,
  3184. p_offset,
  3185. JointGizmosDrawer::look_body_toward_z(p_trs_joint, p_trs_body_b),
  3186. p_limit_lower,
  3187. p_limit_upper,
  3188. *r_body_b_points);
  3189. }
  3190. }
  3191. 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) {
  3192. p_linear_limit_lower = -p_linear_limit_lower;
  3193. p_linear_limit_upper = -p_linear_limit_upper;
  3194. float cs = 0.25;
  3195. r_points.push_back(p_offset.translated(Vector3(0, 0, 0.5)).origin);
  3196. r_points.push_back(p_offset.translated(Vector3(0, 0, -0.5)).origin);
  3197. if (p_linear_limit_lower >= p_linear_limit_upper) {
  3198. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_upper, 0, 0)).origin);
  3199. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_lower, 0, 0)).origin);
  3200. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_upper, -cs, -cs)).origin);
  3201. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_upper, -cs, cs)).origin);
  3202. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_upper, -cs, cs)).origin);
  3203. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_upper, cs, cs)).origin);
  3204. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_upper, cs, cs)).origin);
  3205. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_upper, cs, -cs)).origin);
  3206. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_upper, cs, -cs)).origin);
  3207. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_upper, -cs, -cs)).origin);
  3208. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_lower, -cs, -cs)).origin);
  3209. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_lower, -cs, cs)).origin);
  3210. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_lower, -cs, cs)).origin);
  3211. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_lower, cs, cs)).origin);
  3212. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_lower, cs, cs)).origin);
  3213. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_lower, cs, -cs)).origin);
  3214. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_lower, cs, -cs)).origin);
  3215. r_points.push_back(p_offset.translated(Vector3(p_linear_limit_lower, -cs, -cs)).origin);
  3216. } else {
  3217. r_points.push_back(p_offset.translated(Vector3(+cs * 2, 0, 0)).origin);
  3218. r_points.push_back(p_offset.translated(Vector3(-cs * 2, 0, 0)).origin);
  3219. }
  3220. if (r_body_a_points)
  3221. JointGizmosDrawer::draw_circle(
  3222. Vector3::AXIS_X,
  3223. BODY_A_RADIUS,
  3224. p_offset,
  3225. JointGizmosDrawer::look_body_toward(Vector3::AXIS_X, p_trs_joint, p_trs_body_a),
  3226. p_angular_limit_lower,
  3227. p_angular_limit_upper,
  3228. *r_body_a_points);
  3229. if (r_body_b_points)
  3230. JointGizmosDrawer::draw_circle(
  3231. Vector3::AXIS_X,
  3232. BODY_B_RADIUS,
  3233. p_offset,
  3234. JointGizmosDrawer::look_body_toward(Vector3::AXIS_X, p_trs_joint, p_trs_body_b),
  3235. p_angular_limit_lower,
  3236. p_angular_limit_upper,
  3237. *r_body_b_points,
  3238. true);
  3239. }
  3240. 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) {
  3241. if (r_body_a_points)
  3242. JointGizmosDrawer::draw_cone(
  3243. p_offset,
  3244. JointGizmosDrawer::look_body(p_trs_joint, p_trs_body_a),
  3245. p_swing,
  3246. p_twist,
  3247. *r_body_a_points);
  3248. if (r_body_b_points)
  3249. JointGizmosDrawer::draw_cone(
  3250. p_offset,
  3251. JointGizmosDrawer::look_body(p_trs_joint, p_trs_body_b),
  3252. p_swing,
  3253. p_twist,
  3254. *r_body_b_points);
  3255. }
  3256. void JointSpatialGizmoPlugin::CreateGeneric6DOFJointGizmo(
  3257. const Transform &p_offset,
  3258. const Transform &p_trs_joint,
  3259. const Transform &p_trs_body_a,
  3260. const Transform &p_trs_body_b,
  3261. real_t p_angular_limit_lower_x,
  3262. real_t p_angular_limit_upper_x,
  3263. real_t p_linear_limit_lower_x,
  3264. real_t p_linear_limit_upper_x,
  3265. bool p_enable_angular_limit_x,
  3266. bool p_enable_linear_limit_x,
  3267. real_t p_angular_limit_lower_y,
  3268. real_t p_angular_limit_upper_y,
  3269. real_t p_linear_limit_lower_y,
  3270. real_t p_linear_limit_upper_y,
  3271. bool p_enable_angular_limit_y,
  3272. bool p_enable_linear_limit_y,
  3273. real_t p_angular_limit_lower_z,
  3274. real_t p_angular_limit_upper_z,
  3275. real_t p_linear_limit_lower_z,
  3276. real_t p_linear_limit_upper_z,
  3277. bool p_enable_angular_limit_z,
  3278. bool p_enable_linear_limit_z,
  3279. Vector<Vector3> &r_points,
  3280. Vector<Vector3> *r_body_a_points,
  3281. Vector<Vector3> *r_body_b_points) {
  3282. float cs = 0.25;
  3283. for (int ax = 0; ax < 3; ax++) {
  3284. float ll = 0;
  3285. float ul = 0;
  3286. float lll = 0;
  3287. float lul = 0;
  3288. int a1 = 0;
  3289. int a2 = 0;
  3290. int a3 = 0;
  3291. bool enable_ang = false;
  3292. bool enable_lin = false;
  3293. switch (ax) {
  3294. case 0:
  3295. ll = p_angular_limit_lower_x;
  3296. ul = p_angular_limit_upper_x;
  3297. lll = -p_linear_limit_lower_x;
  3298. lul = -p_linear_limit_upper_x;
  3299. enable_ang = p_enable_angular_limit_x;
  3300. enable_lin = p_enable_linear_limit_x;
  3301. a1 = 0;
  3302. a2 = 1;
  3303. a3 = 2;
  3304. break;
  3305. case 1:
  3306. ll = p_angular_limit_lower_y;
  3307. ul = p_angular_limit_upper_y;
  3308. lll = -p_linear_limit_lower_y;
  3309. lul = -p_linear_limit_upper_y;
  3310. enable_ang = p_enable_angular_limit_y;
  3311. enable_lin = p_enable_linear_limit_y;
  3312. a1 = 1;
  3313. a2 = 2;
  3314. a3 = 0;
  3315. break;
  3316. case 2:
  3317. ll = p_angular_limit_lower_z;
  3318. ul = p_angular_limit_upper_z;
  3319. lll = -p_linear_limit_lower_z;
  3320. lul = -p_linear_limit_upper_z;
  3321. enable_ang = p_enable_angular_limit_z;
  3322. enable_lin = p_enable_linear_limit_z;
  3323. a1 = 2;
  3324. a2 = 0;
  3325. a3 = 1;
  3326. break;
  3327. }
  3328. #define ADD_VTX(x, y, z) \
  3329. { \
  3330. Vector3 v; \
  3331. v[a1] = (x); \
  3332. v[a2] = (y); \
  3333. v[a3] = (z); \
  3334. r_points.push_back(p_offset.translated(v).origin); \
  3335. }
  3336. if (enable_lin && lll >= lul) {
  3337. ADD_VTX(lul, 0, 0);
  3338. ADD_VTX(lll, 0, 0);
  3339. ADD_VTX(lul, -cs, -cs);
  3340. ADD_VTX(lul, -cs, cs);
  3341. ADD_VTX(lul, -cs, cs);
  3342. ADD_VTX(lul, cs, cs);
  3343. ADD_VTX(lul, cs, cs);
  3344. ADD_VTX(lul, cs, -cs);
  3345. ADD_VTX(lul, cs, -cs);
  3346. ADD_VTX(lul, -cs, -cs);
  3347. ADD_VTX(lll, -cs, -cs);
  3348. ADD_VTX(lll, -cs, cs);
  3349. ADD_VTX(lll, -cs, cs);
  3350. ADD_VTX(lll, cs, cs);
  3351. ADD_VTX(lll, cs, cs);
  3352. ADD_VTX(lll, cs, -cs);
  3353. ADD_VTX(lll, cs, -cs);
  3354. ADD_VTX(lll, -cs, -cs);
  3355. } else {
  3356. ADD_VTX(+cs * 2, 0, 0);
  3357. ADD_VTX(-cs * 2, 0, 0);
  3358. }
  3359. if (!enable_ang) {
  3360. ll = 0;
  3361. ul = -1;
  3362. }
  3363. if (r_body_a_points)
  3364. JointGizmosDrawer::draw_circle(
  3365. static_cast<Vector3::Axis>(ax),
  3366. BODY_A_RADIUS,
  3367. p_offset,
  3368. JointGizmosDrawer::look_body_toward(static_cast<Vector3::Axis>(ax), p_trs_joint, p_trs_body_a),
  3369. ll,
  3370. ul,
  3371. *r_body_a_points,
  3372. true);
  3373. if (r_body_b_points)
  3374. JointGizmosDrawer::draw_circle(
  3375. static_cast<Vector3::Axis>(ax),
  3376. BODY_B_RADIUS,
  3377. p_offset,
  3378. JointGizmosDrawer::look_body_toward(static_cast<Vector3::Axis>(ax), p_trs_joint, p_trs_body_b),
  3379. ll,
  3380. ul,
  3381. *r_body_b_points);
  3382. }
  3383. #undef ADD_VTX
  3384. }