spatial_editor_gizmos.cpp 157 KB

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