2
0

spatial_editor_gizmos.cpp 135 KB

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