spatial_editor_gizmos.cpp 127 KB

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