spatial_editor_gizmos.cpp 123 KB

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