spatial_editor_plugin.cpp 180 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521
  1. /*************************************************************************/
  2. /* spatial_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 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_plugin.h"
  31. #include "camera_matrix.h"
  32. #include "core/os/input.h"
  33. #include "editor/editor_node.h"
  34. #include "editor/editor_settings.h"
  35. #include "editor/plugins/animation_player_editor_plugin.h"
  36. #include "editor/plugins/script_editor_plugin.h"
  37. #include "editor/script_editor_debugger.h"
  38. #include "editor/spatial_editor_gizmos.h"
  39. #include "os/keyboard.h"
  40. #include "print_string.h"
  41. #include "project_settings.h"
  42. #include "scene/3d/camera.h"
  43. #include "scene/3d/visual_instance.h"
  44. #include "scene/resources/packed_scene.h"
  45. #include "scene/resources/surface_tool.h"
  46. #include "sort.h"
  47. #define DISTANCE_DEFAULT 4
  48. #define GIZMO_ARROW_SIZE 0.35
  49. #define GIZMO_RING_HALF_WIDTH 0.1
  50. #define GIZMO_SCALE_DEFAULT 0.15
  51. #define GIZMO_PLANE_SIZE 0.2
  52. #define GIZMO_PLANE_DST 0.3
  53. #define GIZMO_CIRCLE_SIZE 1.1
  54. #define GIZMO_SCALE_OFFSET (GIZMO_CIRCLE_SIZE + 0.3)
  55. #define GIZMO_ARROW_OFFSET (GIZMO_CIRCLE_SIZE + 0.3)
  56. #define ZOOM_MIN_DISTANCE 0.001
  57. #define ZOOM_MULTIPLIER 1.08
  58. #define ZOOM_INDICATOR_DELAY_S 1.5
  59. #define FREELOOK_MIN_SPEED 0.01
  60. #define FREELOOK_SPEED_MULTIPLIER 1.08
  61. #define MIN_Z 0.01
  62. #define MAX_Z 10000
  63. #define MIN_FOV 0.01
  64. #define MAX_FOV 179
  65. #ifdef TOOLS_ENABLED
  66. #define get_global_gizmo_transform get_global_gizmo_transform
  67. #define get_local_gizmo_transform get_local_gizmo_transform
  68. #else
  69. #define get_global_gizmo_transform get_global_transform
  70. #define get_local_gizmo_transform get_transform
  71. #endif
  72. void SpatialEditorViewport::_update_camera(float p_interp_delta) {
  73. bool is_orthogonal = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL;
  74. Cursor old_camera_cursor = camera_cursor;
  75. camera_cursor = cursor;
  76. if (p_interp_delta > 0) {
  77. //-------
  78. // Perform smoothing
  79. if (is_freelook_active()) {
  80. // Higher inertia should increase "lag" (lerp with factor between 0 and 1)
  81. // Inertia of zero should produce instant movement (lerp with factor of 1) in this case it returns a really high value and gets clamped to 1.
  82. real_t inertia = EDITOR_GET("editors/3d/freelook/freelook_inertia");
  83. inertia = MAX(0.001, inertia);
  84. real_t factor = (1.0 / inertia) * p_interp_delta;
  85. // We interpolate a different point here, because in freelook mode the focus point (cursor.pos) orbits around eye_pos
  86. camera_cursor.eye_pos = old_camera_cursor.eye_pos.linear_interpolate(cursor.eye_pos, CLAMP(factor, 0, 1));
  87. float orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  88. orbit_inertia = MAX(0.0001, orbit_inertia);
  89. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  90. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  91. Vector3 forward = to_camera_transform(camera_cursor).basis.xform(Vector3(0, 0, -1));
  92. camera_cursor.pos = camera_cursor.eye_pos + forward * camera_cursor.distance;
  93. } else {
  94. //when not being manipulated, move softly
  95. float free_orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/orbit_inertia");
  96. float free_translation_inertia = EDITOR_GET("editors/3d/navigation_feel/translation_inertia");
  97. //when being manipulated, move more quickly
  98. float manip_orbit_inertia = EDITOR_GET("editors/3d/navigation_feel/manipulation_orbit_inertia");
  99. float manip_translation_inertia = EDITOR_GET("editors/3d/navigation_feel/manipulation_translation_inertia");
  100. float zoom_inertia = EDITOR_GET("editors/3d/navigation_feel/zoom_inertia");
  101. //determine if being manipulated
  102. bool manipulated = Input::get_singleton()->get_mouse_button_mask() & (2 | 4);
  103. manipulated |= Input::get_singleton()->is_key_pressed(KEY_SHIFT);
  104. manipulated |= Input::get_singleton()->is_key_pressed(KEY_ALT);
  105. manipulated |= Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  106. float orbit_inertia = MAX(0.00001, manipulated ? manip_orbit_inertia : free_orbit_inertia);
  107. float translation_inertia = MAX(0.0001, manipulated ? manip_translation_inertia : free_translation_inertia);
  108. zoom_inertia = MAX(0.0001, zoom_inertia);
  109. camera_cursor.x_rot = Math::lerp(old_camera_cursor.x_rot, cursor.x_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  110. camera_cursor.y_rot = Math::lerp(old_camera_cursor.y_rot, cursor.y_rot, MIN(1.f, p_interp_delta * (1 / orbit_inertia)));
  111. camera_cursor.pos = old_camera_cursor.pos.linear_interpolate(cursor.pos, MIN(1.f, p_interp_delta * (1 / translation_inertia)));
  112. camera_cursor.distance = Math::lerp(old_camera_cursor.distance, cursor.distance, MIN(1.f, p_interp_delta * (1 / zoom_inertia)));
  113. }
  114. }
  115. //-------
  116. // Apply camera transform
  117. float tolerance = 0.001;
  118. bool equal = true;
  119. if (Math::abs(old_camera_cursor.x_rot - camera_cursor.x_rot) > tolerance || Math::abs(old_camera_cursor.y_rot - camera_cursor.y_rot) > tolerance) {
  120. equal = false;
  121. }
  122. if (equal && old_camera_cursor.pos.distance_squared_to(camera_cursor.pos) > tolerance * tolerance) {
  123. equal = false;
  124. }
  125. if (equal && Math::abs(old_camera_cursor.distance - camera_cursor.distance) > tolerance) {
  126. equal = false;
  127. }
  128. if (!equal || p_interp_delta == 0 || is_freelook_active() || is_orthogonal != orthogonal) {
  129. camera->set_global_transform(to_camera_transform(camera_cursor));
  130. if (orthogonal)
  131. camera->set_orthogonal(2 * cursor.distance, 0.1, 8192);
  132. else
  133. camera->set_perspective(get_fov(), get_znear(), get_zfar());
  134. update_transform_gizmo_view();
  135. }
  136. }
  137. Transform SpatialEditorViewport::to_camera_transform(const Cursor &p_cursor) const {
  138. Transform camera_transform;
  139. camera_transform.translate(p_cursor.pos);
  140. camera_transform.basis.rotate(Vector3(1, 0, 0), -p_cursor.x_rot);
  141. camera_transform.basis.rotate(Vector3(0, 1, 0), -p_cursor.y_rot);
  142. if (orthogonal)
  143. camera_transform.translate(0, 0, 4096);
  144. else
  145. camera_transform.translate(0, 0, p_cursor.distance);
  146. return camera_transform;
  147. }
  148. String SpatialEditorGizmo::get_handle_name(int p_idx) const {
  149. if (get_script_instance() && get_script_instance()->has_method("get_handle_name"))
  150. return get_script_instance()->call("get_handle_name", p_idx);
  151. return "";
  152. }
  153. Variant SpatialEditorGizmo::get_handle_value(int p_idx) const {
  154. if (get_script_instance() && get_script_instance()->has_method("get_handle_value"))
  155. return get_script_instance()->call("get_handle_value", p_idx);
  156. return Variant();
  157. }
  158. void SpatialEditorGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
  159. if (get_script_instance() && get_script_instance()->has_method("set_handle"))
  160. get_script_instance()->call("set_handle", p_idx, p_camera, p_point);
  161. }
  162. void SpatialEditorGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
  163. if (get_script_instance() && get_script_instance()->has_method("commit_handle"))
  164. get_script_instance()->call("commit_handle", p_idx, p_restore, p_cancel);
  165. }
  166. bool SpatialEditorGizmo::intersect_frustum(const Camera *p_camera, const Vector<Plane> &p_frustum) {
  167. return false;
  168. }
  169. bool SpatialEditorGizmo::intersect_ray(Camera *p_camera, const Point2 &p_point, Vector3 &r_pos, Vector3 &r_normal, int *r_gizmo_handle, bool p_sec_first) {
  170. return false;
  171. }
  172. SpatialEditorGizmo::SpatialEditorGizmo() {
  173. selected = false;
  174. }
  175. int SpatialEditorViewport::get_selected_count() const {
  176. Map<Node *, Object *> &selection = editor_selection->get_selection();
  177. int count = 0;
  178. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  179. Spatial *sp = Object::cast_to<Spatial>(E->key());
  180. if (!sp)
  181. continue;
  182. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  183. if (!se)
  184. continue;
  185. count++;
  186. }
  187. return count;
  188. }
  189. float SpatialEditorViewport::get_znear() const {
  190. return CLAMP(spatial_editor->get_znear(), MIN_Z, MAX_Z);
  191. }
  192. float SpatialEditorViewport::get_zfar() const {
  193. return CLAMP(spatial_editor->get_zfar(), MIN_Z, MAX_Z);
  194. }
  195. float SpatialEditorViewport::get_fov() const {
  196. return CLAMP(spatial_editor->get_fov(), MIN_FOV, MAX_FOV);
  197. }
  198. Transform SpatialEditorViewport::_get_camera_transform() const {
  199. return camera->get_global_transform();
  200. }
  201. Vector3 SpatialEditorViewport::_get_camera_position() const {
  202. return _get_camera_transform().origin;
  203. }
  204. Point2 SpatialEditorViewport::_point_to_screen(const Vector3 &p_point) {
  205. return camera->unproject_position(p_point) * viewport_container->get_stretch_shrink();
  206. }
  207. Vector3 SpatialEditorViewport::_get_ray_pos(const Vector2 &p_pos) const {
  208. return camera->project_ray_origin(p_pos / viewport_container->get_stretch_shrink());
  209. }
  210. Vector3 SpatialEditorViewport::_get_camera_normal() const {
  211. return -_get_camera_transform().basis.get_axis(2);
  212. }
  213. Vector3 SpatialEditorViewport::_get_ray(const Vector2 &p_pos) const {
  214. return camera->project_ray_normal(p_pos / viewport_container->get_stretch_shrink());
  215. }
  216. /*
  217. void SpatialEditorViewport::_clear_id(Spatial *p_node) {
  218. editor_selection->remove_node(p_node);
  219. }
  220. */
  221. void SpatialEditorViewport::_clear_selected() {
  222. editor_selection->clear();
  223. }
  224. void SpatialEditorViewport::_select_clicked(bool p_append, bool p_single) {
  225. if (!clicked)
  226. return;
  227. Spatial *sp = Object::cast_to<Spatial>(ObjectDB::get_instance(clicked));
  228. if (!sp)
  229. return;
  230. _select(sp, clicked_wants_append, true);
  231. }
  232. void SpatialEditorViewport::_select(Spatial *p_node, bool p_append, bool p_single) {
  233. if (!p_append) {
  234. editor_selection->clear();
  235. }
  236. if (editor_selection->is_selected(p_node)) {
  237. //erase
  238. editor_selection->remove_node(p_node);
  239. } else {
  240. editor_selection->add_node(p_node);
  241. }
  242. if (p_single) {
  243. if (Engine::get_singleton()->is_editor_hint())
  244. editor->call("edit_node", p_node);
  245. }
  246. }
  247. ObjectID SpatialEditorViewport::_select_ray(const Point2 &p_pos, bool p_append, bool &r_includes_current, int *r_gizmo_handle, bool p_alt_select) {
  248. if (r_gizmo_handle)
  249. *r_gizmo_handle = -1;
  250. Vector3 ray = _get_ray(p_pos);
  251. Vector3 pos = _get_ray_pos(p_pos);
  252. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(pos, ray, get_tree()->get_root()->get_world()->get_scenario());
  253. Set<Ref<SpatialEditorGizmo> > found_gizmos;
  254. Node *edited_scene = get_tree()->get_edited_scene_root();
  255. ObjectID closest = 0;
  256. Spatial *item = NULL;
  257. float closest_dist = 1e20;
  258. int selected_handle = -1;
  259. for (int i = 0; i < instances.size(); i++) {
  260. Spatial *spat = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  261. if (!spat)
  262. continue;
  263. Ref<SpatialEditorGizmo> seg = spat->get_gizmo();
  264. if ((!seg.is_valid()) || found_gizmos.has(seg)) {
  265. continue;
  266. }
  267. found_gizmos.insert(seg);
  268. Vector3 point;
  269. Vector3 normal;
  270. int handle = -1;
  271. bool inters = seg->intersect_ray(camera, p_pos, point, normal, &handle, p_alt_select);
  272. if (!inters)
  273. continue;
  274. float dist = pos.distance_to(point);
  275. if (dist < 0)
  276. continue;
  277. if (dist < closest_dist) {
  278. //make sure that whathever is selected is editable
  279. while (spat && spat != edited_scene && spat->get_owner() != edited_scene && !edited_scene->is_editable_instance(spat->get_owner())) {
  280. spat = Object::cast_to<Spatial>(spat->get_owner());
  281. }
  282. if (spat) {
  283. item = spat;
  284. closest = spat->get_instance_id();
  285. closest_dist = dist;
  286. selected_handle = handle;
  287. } else {
  288. ERR_PRINT("Bug?");
  289. }
  290. }
  291. }
  292. if (!item)
  293. return 0;
  294. if (!editor_selection->is_selected(item) || (r_gizmo_handle && selected_handle >= 0)) {
  295. if (r_gizmo_handle)
  296. *r_gizmo_handle = selected_handle;
  297. }
  298. return closest;
  299. }
  300. void SpatialEditorViewport::_find_items_at_pos(const Point2 &p_pos, bool &r_includes_current, Vector<_RayResult> &results, bool p_alt_select) {
  301. Vector3 ray = _get_ray(p_pos);
  302. Vector3 pos = _get_ray_pos(p_pos);
  303. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(pos, ray, get_tree()->get_root()->get_world()->get_scenario());
  304. Set<Ref<SpatialEditorGizmo> > found_gizmos;
  305. r_includes_current = false;
  306. for (int i = 0; i < instances.size(); i++) {
  307. Spatial *spat = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  308. if (!spat)
  309. continue;
  310. Ref<SpatialEditorGizmo> seg = spat->get_gizmo();
  311. if (!seg.is_valid())
  312. continue;
  313. if (found_gizmos.has(seg))
  314. continue;
  315. found_gizmos.insert(seg);
  316. Vector3 point;
  317. Vector3 normal;
  318. int handle = -1;
  319. bool inters = seg->intersect_ray(camera, p_pos, point, normal, NULL, p_alt_select);
  320. if (!inters)
  321. continue;
  322. float dist = pos.distance_to(point);
  323. if (dist < 0)
  324. continue;
  325. if (editor_selection->is_selected(spat))
  326. r_includes_current = true;
  327. _RayResult res;
  328. res.item = spat;
  329. res.depth = dist;
  330. res.handle = handle;
  331. results.push_back(res);
  332. }
  333. if (results.empty())
  334. return;
  335. results.sort();
  336. }
  337. Vector3 SpatialEditorViewport::_get_screen_to_space(const Vector3 &p_vector3) {
  338. CameraMatrix cm;
  339. if (orthogonal) {
  340. cm.set_orthogonal(camera->get_size(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  341. } else {
  342. cm.set_perspective(get_fov(), get_size().aspect(), get_znear() + p_vector3.z, get_zfar());
  343. }
  344. float screen_w, screen_h;
  345. cm.get_viewport_size(screen_w, screen_h);
  346. Transform camera_transform;
  347. camera_transform.translate(cursor.pos);
  348. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  349. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  350. camera_transform.translate(0, 0, cursor.distance);
  351. return camera_transform.xform(Vector3(((p_vector3.x / get_size().width) * 2.0 - 1.0) * screen_w, ((1.0 - (p_vector3.y / get_size().height)) * 2.0 - 1.0) * screen_h, -(get_znear() + p_vector3.z)));
  352. }
  353. void SpatialEditorViewport::_select_region() {
  354. if (cursor.region_begin == cursor.region_end)
  355. return; //nothing really
  356. float z_offset = MAX(0.0, 5.0 - get_znear());
  357. Vector3 box[4] = {
  358. Vector3(
  359. MIN(cursor.region_begin.x, cursor.region_end.x),
  360. MIN(cursor.region_begin.y, cursor.region_end.y),
  361. z_offset),
  362. Vector3(
  363. MAX(cursor.region_begin.x, cursor.region_end.x),
  364. MIN(cursor.region_begin.y, cursor.region_end.y),
  365. z_offset),
  366. Vector3(
  367. MAX(cursor.region_begin.x, cursor.region_end.x),
  368. MAX(cursor.region_begin.y, cursor.region_end.y),
  369. z_offset),
  370. Vector3(
  371. MIN(cursor.region_begin.x, cursor.region_end.x),
  372. MAX(cursor.region_begin.y, cursor.region_end.y),
  373. z_offset)
  374. };
  375. Vector<Plane> frustum;
  376. Vector3 cam_pos = _get_camera_position();
  377. for (int i = 0; i < 4; i++) {
  378. Vector3 a = _get_screen_to_space(box[i]);
  379. Vector3 b = _get_screen_to_space(box[(i + 1) % 4]);
  380. if (orthogonal) {
  381. frustum.push_back(Plane(a, (a - b).normalized()));
  382. } else {
  383. frustum.push_back(Plane(a, b, cam_pos));
  384. }
  385. }
  386. if (!orthogonal) {
  387. Plane near(cam_pos, -_get_camera_normal());
  388. near.d -= get_znear();
  389. frustum.push_back(near);
  390. Plane far = -near;
  391. far.d += get_zfar();
  392. frustum.push_back(far);
  393. }
  394. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_convex(frustum, get_tree()->get_root()->get_world()->get_scenario());
  395. Vector<Spatial *> selected;
  396. Node *edited_scene = get_tree()->get_edited_scene_root();
  397. for (int i = 0; i < instances.size(); i++) {
  398. Spatial *sp = Object::cast_to<Spatial>(ObjectDB::get_instance(instances[i]));
  399. if (!sp)
  400. continue;
  401. Spatial *root_sp = sp;
  402. while (root_sp && root_sp != edited_scene && root_sp->get_owner() != edited_scene && !edited_scene->is_editable_instance(root_sp->get_owner())) {
  403. root_sp = Object::cast_to<Spatial>(root_sp->get_owner());
  404. }
  405. if (selected.find(root_sp) != -1) continue;
  406. Ref<SpatialEditorGizmo> seg = sp->get_gizmo();
  407. if (!seg.is_valid())
  408. continue;
  409. if (seg->intersect_frustum(camera, frustum)) {
  410. selected.push_back(root_sp);
  411. }
  412. }
  413. bool single = selected.size() == 1;
  414. for (int i = 0; i < selected.size(); i++) {
  415. _select(selected[i], true, single);
  416. }
  417. }
  418. void SpatialEditorViewport::_update_name() {
  419. String ortho = orthogonal ? TTR("Orthogonal") : TTR("Perspective");
  420. if (name != "")
  421. view_menu->set_text("[ " + name + " " + ortho + " ]");
  422. else
  423. view_menu->set_text("[ " + ortho + " ]");
  424. view_menu->set_size(Vector2(0, 0)); // resets the button size
  425. }
  426. void SpatialEditorViewport::_compute_edit(const Point2 &p_point) {
  427. _edit.click_ray = _get_ray(Vector2(p_point.x, p_point.y));
  428. _edit.click_ray_pos = _get_ray_pos(Vector2(p_point.x, p_point.y));
  429. _edit.plane = TRANSFORM_VIEW;
  430. spatial_editor->update_transform_gizmo();
  431. _edit.center = spatial_editor->get_gizmo_transform().origin;
  432. List<Node *> &selection = editor_selection->get_selected_node_list();
  433. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  434. Spatial *sp = Object::cast_to<Spatial>(E->get());
  435. if (!sp)
  436. continue;
  437. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  438. if (!se)
  439. continue;
  440. se->original = se->sp->get_global_gizmo_transform();
  441. se->original_local = se->sp->get_local_gizmo_transform();
  442. }
  443. }
  444. static int _get_key_modifier_setting(const String &p_property) {
  445. switch (EditorSettings::get_singleton()->get(p_property).operator int()) {
  446. case 0: return 0;
  447. case 1: return KEY_SHIFT;
  448. case 2: return KEY_ALT;
  449. case 3: return KEY_META;
  450. case 4: return KEY_CONTROL;
  451. }
  452. return 0;
  453. }
  454. static int _get_key_modifier(Ref<InputEventWithModifiers> e) {
  455. if (e->get_shift())
  456. return KEY_SHIFT;
  457. if (e->get_alt())
  458. return KEY_ALT;
  459. if (e->get_control())
  460. return KEY_CONTROL;
  461. if (e->get_metakey())
  462. return KEY_META;
  463. return 0;
  464. }
  465. bool SpatialEditorViewport::_gizmo_select(const Vector2 &p_screenpos, bool p_highlight_only) {
  466. if (!spatial_editor->is_gizmo_visible())
  467. return false;
  468. if (get_selected_count() == 0) {
  469. if (p_highlight_only)
  470. spatial_editor->select_gizmo_highlight_axis(-1);
  471. return false;
  472. }
  473. Vector3 ray_pos = _get_ray_pos(Vector2(p_screenpos.x, p_screenpos.y));
  474. Vector3 ray = _get_ray(Vector2(p_screenpos.x, p_screenpos.y));
  475. Transform gt = spatial_editor->get_gizmo_transform();
  476. float gs = gizmo_scale;
  477. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE) {
  478. int col_axis = -1;
  479. float col_d = 1e20;
  480. for (int i = 0; i < 3; i++) {
  481. Vector3 grabber_pos = gt.origin + gt.basis.get_axis(i) * gs * (GIZMO_ARROW_OFFSET + (GIZMO_ARROW_SIZE * 0.5));
  482. float grabber_radius = gs * GIZMO_ARROW_SIZE;
  483. Vector3 r;
  484. if (Geometry::segment_intersects_sphere(ray_pos, ray_pos + ray * 10000.0, grabber_pos, grabber_radius, &r)) {
  485. float d = r.distance_to(ray_pos);
  486. if (d < col_d) {
  487. col_d = d;
  488. col_axis = i;
  489. }
  490. }
  491. }
  492. bool is_plane_translate = false;
  493. // plane select
  494. if (col_axis == -1) {
  495. col_d = 1e20;
  496. for (int i = 0; i < 3; i++) {
  497. Vector3 ivec2 = gt.basis.get_axis((i + 1) % 3).normalized();
  498. Vector3 ivec3 = gt.basis.get_axis((i + 2) % 3).normalized();
  499. Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gs * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST);
  500. Vector3 r;
  501. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  502. if (plane.intersects_ray(ray_pos, ray, &r)) {
  503. float dist = r.distance_to(grabber_pos);
  504. if (dist < (gs * GIZMO_PLANE_SIZE)) {
  505. float d = ray_pos.distance_to(r);
  506. if (d < col_d) {
  507. col_d = d;
  508. col_axis = i;
  509. is_plane_translate = true;
  510. }
  511. }
  512. }
  513. }
  514. }
  515. if (col_axis != -1) {
  516. if (p_highlight_only) {
  517. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_translate ? 6 : 0));
  518. } else {
  519. //handle plane translate
  520. _edit.mode = TRANSFORM_TRANSLATE;
  521. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  522. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_translate ? 3 : 0));
  523. }
  524. return true;
  525. }
  526. }
  527. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE) {
  528. int col_axis = -1;
  529. float col_d = 1e20;
  530. for (int i = 0; i < 3; i++) {
  531. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  532. Vector3 r;
  533. if (!plane.intersects_ray(ray_pos, ray, &r))
  534. continue;
  535. float dist = r.distance_to(gt.origin);
  536. if (dist > gs * (GIZMO_CIRCLE_SIZE - GIZMO_RING_HALF_WIDTH) && dist < gs * (GIZMO_CIRCLE_SIZE + GIZMO_RING_HALF_WIDTH)) {
  537. float d = ray_pos.distance_to(r);
  538. if (d < col_d) {
  539. col_d = d;
  540. col_axis = i;
  541. }
  542. }
  543. }
  544. if (col_axis != -1) {
  545. if (p_highlight_only) {
  546. spatial_editor->select_gizmo_highlight_axis(col_axis + 3);
  547. } else {
  548. //handle rotate
  549. _edit.mode = TRANSFORM_ROTATE;
  550. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  551. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis);
  552. }
  553. return true;
  554. }
  555. }
  556. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE) {
  557. int col_axis = -1;
  558. float col_d = 1e20;
  559. for (int i = 0; i < 3; i++) {
  560. Vector3 grabber_pos = gt.origin + gt.basis.get_axis(i) * gs * GIZMO_SCALE_OFFSET;
  561. float grabber_radius = gs * GIZMO_ARROW_SIZE;
  562. Vector3 r;
  563. if (Geometry::segment_intersects_sphere(ray_pos, ray_pos + ray * 10000.0, grabber_pos, grabber_radius, &r)) {
  564. float d = r.distance_to(ray_pos);
  565. if (d < col_d) {
  566. col_d = d;
  567. col_axis = i;
  568. }
  569. }
  570. }
  571. bool is_plane_scale = false;
  572. // plane select
  573. if (col_axis == -1) {
  574. col_d = 1e20;
  575. for (int i = 0; i < 3; i++) {
  576. Vector3 ivec2 = gt.basis.get_axis((i + 1) % 3).normalized();
  577. Vector3 ivec3 = gt.basis.get_axis((i + 2) % 3).normalized();
  578. Vector3 grabber_pos = gt.origin + (ivec2 + ivec3) * gs * (GIZMO_PLANE_SIZE + GIZMO_PLANE_DST);
  579. Vector3 r;
  580. Plane plane(gt.origin, gt.basis.get_axis(i).normalized());
  581. if (plane.intersects_ray(ray_pos, ray, &r)) {
  582. float dist = r.distance_to(grabber_pos);
  583. if (dist < (gs * GIZMO_PLANE_SIZE)) {
  584. float d = ray_pos.distance_to(r);
  585. if (d < col_d) {
  586. col_d = d;
  587. col_axis = i;
  588. is_plane_scale = true;
  589. }
  590. }
  591. }
  592. }
  593. }
  594. if (col_axis != -1) {
  595. if (p_highlight_only) {
  596. spatial_editor->select_gizmo_highlight_axis(col_axis + (is_plane_scale ? 12 : 9));
  597. } else {
  598. //handle scale
  599. _edit.mode = TRANSFORM_SCALE;
  600. _compute_edit(Point2(p_screenpos.x, p_screenpos.y));
  601. _edit.plane = TransformPlane(TRANSFORM_X_AXIS + col_axis + (is_plane_scale ? 3 : 0));
  602. }
  603. return true;
  604. }
  605. }
  606. if (p_highlight_only)
  607. spatial_editor->select_gizmo_highlight_axis(-1);
  608. return false;
  609. }
  610. void SpatialEditorViewport::_surface_mouse_enter() {
  611. if (!surface->has_focus() && (!get_focus_owner() || !get_focus_owner()->is_text_field()))
  612. surface->grab_focus();
  613. }
  614. void SpatialEditorViewport::_surface_mouse_exit() {
  615. _remove_preview();
  616. }
  617. void SpatialEditorViewport::_surface_focus_enter() {
  618. view_menu->set_disable_shortcuts(false);
  619. }
  620. void SpatialEditorViewport::_surface_focus_exit() {
  621. view_menu->set_disable_shortcuts(true);
  622. }
  623. void SpatialEditorViewport::_list_select(Ref<InputEventMouseButton> b) {
  624. _find_items_at_pos(b->get_position(), clicked_includes_current, selection_results, b->get_shift());
  625. Node *scene = editor->get_edited_scene();
  626. for (int i = 0; i < selection_results.size(); i++) {
  627. Spatial *item = selection_results[i].item;
  628. if (item != scene && item->get_owner() != scene && !scene->is_editable_instance(item->get_owner())) {
  629. //invalid result
  630. selection_results.remove(i);
  631. i--;
  632. }
  633. }
  634. clicked_wants_append = b->get_shift();
  635. if (selection_results.size() == 1) {
  636. clicked = selection_results[0].item->get_instance_id();
  637. selection_results.clear();
  638. if (clicked) {
  639. _select_clicked(clicked_wants_append, true);
  640. clicked = 0;
  641. }
  642. } else if (!selection_results.empty()) {
  643. NodePath root_path = get_tree()->get_edited_scene_root()->get_path();
  644. StringName root_name = root_path.get_name(root_path.get_name_count() - 1);
  645. for (int i = 0; i < selection_results.size(); i++) {
  646. Spatial *spat = selection_results[i].item;
  647. Ref<Texture> icon;
  648. if (spat->has_meta("_editor_icon"))
  649. icon = spat->get_meta("_editor_icon");
  650. else
  651. icon = get_icon(has_icon(spat->get_class(), "EditorIcons") ? spat->get_class() : String("Object"), "EditorIcons");
  652. String node_path = "/" + root_name + "/" + root_path.rel_path_to(spat->get_path());
  653. selection_menu->add_item(spat->get_name());
  654. selection_menu->set_item_icon(i, icon);
  655. selection_menu->set_item_metadata(i, node_path);
  656. selection_menu->set_item_tooltip(i, String(spat->get_name()) + "\nType: " + spat->get_class() + "\nPath: " + node_path);
  657. }
  658. selection_menu->set_global_position(b->get_global_position());
  659. selection_menu->popup();
  660. }
  661. }
  662. void SpatialEditorViewport::_sinput(const Ref<InputEvent> &p_event) {
  663. if (previewing)
  664. return; //do NONE
  665. {
  666. EditorNode *en = editor;
  667. EditorPluginList *force_input_forwarding_list = en->get_editor_plugins_force_input_forwarding();
  668. if (!force_input_forwarding_list->empty()) {
  669. bool discard = force_input_forwarding_list->forward_spatial_gui_input(camera, p_event, true);
  670. if (discard)
  671. return;
  672. }
  673. }
  674. {
  675. EditorNode *en = editor;
  676. EditorPluginList *over_plugin_list = en->get_editor_plugins_over();
  677. if (!over_plugin_list->empty()) {
  678. bool discard = over_plugin_list->forward_spatial_gui_input(camera, p_event, false);
  679. if (discard)
  680. return;
  681. }
  682. }
  683. Ref<InputEventMouseButton> b = p_event;
  684. if (b.is_valid()) {
  685. float zoom_factor = 1 + (ZOOM_MULTIPLIER - 1) * b->get_factor();
  686. switch (b->get_button_index()) {
  687. case BUTTON_WHEEL_UP: {
  688. if (is_freelook_active())
  689. scale_freelook_speed(zoom_factor);
  690. else
  691. scale_cursor_distance(1.0 / zoom_factor);
  692. } break;
  693. case BUTTON_WHEEL_DOWN: {
  694. if (is_freelook_active())
  695. scale_freelook_speed(1.0 / zoom_factor);
  696. else
  697. scale_cursor_distance(zoom_factor);
  698. } break;
  699. case BUTTON_RIGHT: {
  700. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  701. if (b->is_pressed() && _edit.gizmo.is_valid()) {
  702. //restore
  703. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_initial_value, true);
  704. _edit.gizmo = Ref<SpatialEditorGizmo>();
  705. }
  706. if (_edit.mode == TRANSFORM_NONE && b->is_pressed()) {
  707. if (b->get_alt()) {
  708. if (nav_scheme == NAVIGATION_MAYA)
  709. break;
  710. _list_select(b);
  711. return;
  712. }
  713. }
  714. if (_edit.mode != TRANSFORM_NONE && b->is_pressed()) {
  715. //cancel motion
  716. _edit.mode = TRANSFORM_NONE;
  717. List<Node *> &selection = editor_selection->get_selected_node_list();
  718. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  719. Spatial *sp = Object::cast_to<Spatial>(E->get());
  720. if (!sp)
  721. continue;
  722. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  723. if (!se)
  724. continue;
  725. sp->set_global_transform(se->original);
  726. }
  727. surface->update();
  728. set_message(TTR("Transform Aborted."), 3);
  729. }
  730. if (b->is_pressed()) {
  731. int mod = _get_key_modifier(b);
  732. if (mod == _get_key_modifier_setting("editors/3d/freelook/freelook_activation_modifier")) {
  733. set_freelook_active(true);
  734. }
  735. } else {
  736. set_freelook_active(false);
  737. }
  738. if (freelook_active && !surface->has_focus()) {
  739. // Focus usually doesn't trigger on right-click, but in case of freelook it should,
  740. // otherwise using keyboard navigation would misbehave
  741. surface->grab_focus();
  742. }
  743. } break;
  744. case BUTTON_MIDDLE: {
  745. if (b->is_pressed() && _edit.mode != TRANSFORM_NONE) {
  746. switch (_edit.plane) {
  747. case TRANSFORM_VIEW: {
  748. _edit.plane = TRANSFORM_X_AXIS;
  749. set_message(TTR("X-Axis Transform."), 2);
  750. name = "";
  751. _update_name();
  752. } break;
  753. case TRANSFORM_X_AXIS: {
  754. _edit.plane = TRANSFORM_Y_AXIS;
  755. set_message(TTR("Y-Axis Transform."), 2);
  756. } break;
  757. case TRANSFORM_Y_AXIS: {
  758. _edit.plane = TRANSFORM_Z_AXIS;
  759. set_message(TTR("Z-Axis Transform."), 2);
  760. } break;
  761. case TRANSFORM_Z_AXIS: {
  762. _edit.plane = TRANSFORM_VIEW;
  763. set_message(TTR("View Plane Transform."), 2);
  764. } break;
  765. }
  766. }
  767. } break;
  768. case BUTTON_LEFT: {
  769. if (b->is_pressed()) {
  770. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  771. if ((nav_scheme == NAVIGATION_MAYA || nav_scheme == NAVIGATION_MODO) && b->get_alt()) {
  772. break;
  773. }
  774. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_LIST_SELECT) {
  775. _list_select(b);
  776. break;
  777. }
  778. _edit.mouse_pos = b->get_position();
  779. _edit.snap = false;
  780. _edit.mode = TRANSFORM_NONE;
  781. //gizmo has priority over everything
  782. bool can_select_gizmos = true;
  783. {
  784. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  785. can_select_gizmos = view_menu->get_popup()->is_item_checked(idx);
  786. }
  787. if (can_select_gizmos && spatial_editor->get_selected()) {
  788. Ref<SpatialEditorGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  789. if (seg.is_valid()) {
  790. int handle = -1;
  791. Vector3 point;
  792. Vector3 normal;
  793. bool inters = seg->intersect_ray(camera, _edit.mouse_pos, point, normal, &handle, b->get_shift());
  794. if (inters && handle != -1) {
  795. _edit.gizmo = seg;
  796. _edit.gizmo_handle = handle;
  797. _edit.gizmo_initial_value = seg->get_handle_value(handle);
  798. break;
  799. }
  800. }
  801. }
  802. if (_gizmo_select(_edit.mouse_pos))
  803. break;
  804. clicked = 0;
  805. clicked_includes_current = false;
  806. if ((spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT && b->get_control()) || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE) {
  807. /* HANDLE ROTATION */
  808. if (get_selected_count() == 0)
  809. break; //bye
  810. //handle rotate
  811. _edit.mode = TRANSFORM_ROTATE;
  812. _compute_edit(b->get_position());
  813. break;
  814. }
  815. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE) {
  816. if (get_selected_count() == 0)
  817. break; //bye
  818. //handle translate
  819. _edit.mode = TRANSFORM_TRANSLATE;
  820. _compute_edit(b->get_position());
  821. break;
  822. }
  823. if (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE) {
  824. if (get_selected_count() == 0)
  825. break; //bye
  826. //handle scale
  827. _edit.mode = TRANSFORM_SCALE;
  828. _compute_edit(b->get_position());
  829. break;
  830. }
  831. // todo scale
  832. int gizmo_handle = -1;
  833. clicked = _select_ray(b->get_position(), b->get_shift(), clicked_includes_current, &gizmo_handle, b->get_shift());
  834. //clicking is always deferred to either move or release
  835. clicked_wants_append = b->get_shift();
  836. if (!clicked) {
  837. if (!clicked_wants_append)
  838. _clear_selected();
  839. //default to regionselect
  840. cursor.region_select = true;
  841. cursor.region_begin = b->get_position();
  842. cursor.region_end = b->get_position();
  843. }
  844. if (clicked && gizmo_handle >= 0) {
  845. Spatial *spa = Object::cast_to<Spatial>(ObjectDB::get_instance(clicked));
  846. if (spa) {
  847. Ref<SpatialEditorGizmo> seg = spa->get_gizmo();
  848. if (seg.is_valid()) {
  849. _edit.gizmo = seg;
  850. _edit.gizmo_handle = gizmo_handle;
  851. _edit.gizmo_initial_value = seg->get_handle_value(gizmo_handle);
  852. break;
  853. }
  854. }
  855. }
  856. surface->update();
  857. } else {
  858. if (_edit.gizmo.is_valid()) {
  859. _edit.gizmo->commit_handle(_edit.gizmo_handle, _edit.gizmo_initial_value, false);
  860. _edit.gizmo = Ref<SpatialEditorGizmo>();
  861. break;
  862. }
  863. if (clicked) {
  864. _select_clicked(clicked_wants_append, true);
  865. //clickd processing was deferred
  866. clicked = 0;
  867. }
  868. if (cursor.region_select) {
  869. if (!clicked_wants_append) _clear_selected();
  870. _select_region();
  871. cursor.region_select = false;
  872. surface->update();
  873. }
  874. if (_edit.mode != TRANSFORM_NONE) {
  875. static const char *_transform_name[4] = { "None", "Rotate", "Translate", "Scale" };
  876. undo_redo->create_action(_transform_name[_edit.mode]);
  877. List<Node *> &selection = editor_selection->get_selected_node_list();
  878. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  879. Spatial *sp = Object::cast_to<Spatial>(E->get());
  880. if (!sp)
  881. continue;
  882. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  883. if (!se)
  884. continue;
  885. undo_redo->add_do_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  886. undo_redo->add_undo_method(sp, "set_global_transform", se->original);
  887. }
  888. undo_redo->commit_action();
  889. _edit.mode = TRANSFORM_NONE;
  890. set_message("");
  891. }
  892. surface->update();
  893. }
  894. } break;
  895. }
  896. }
  897. Ref<InputEventMouseMotion> m = p_event;
  898. if (m.is_valid()) {
  899. _edit.mouse_pos = m->get_position();
  900. if (spatial_editor->get_selected()) {
  901. Ref<SpatialEditorGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  902. if (seg.is_valid()) {
  903. int selected_handle = -1;
  904. int handle = -1;
  905. Vector3 point;
  906. Vector3 normal;
  907. bool inters = seg->intersect_ray(camera, _edit.mouse_pos, point, normal, &handle, false);
  908. if (inters && handle != -1) {
  909. selected_handle = handle;
  910. }
  911. if (selected_handle != spatial_editor->get_over_gizmo_handle()) {
  912. spatial_editor->set_over_gizmo_handle(selected_handle);
  913. spatial_editor->get_selected()->update_gizmo();
  914. if (selected_handle != -1)
  915. spatial_editor->select_gizmo_highlight_axis(-1);
  916. }
  917. }
  918. }
  919. if (spatial_editor->get_over_gizmo_handle() == -1 && !(m->get_button_mask() & 1) && !_edit.gizmo.is_valid()) {
  920. _gizmo_select(_edit.mouse_pos, true);
  921. }
  922. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  923. NavigationMode nav_mode = NAVIGATION_NONE;
  924. if (_edit.gizmo.is_valid()) {
  925. _edit.gizmo->set_handle(_edit.gizmo_handle, camera, m->get_position());
  926. Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle);
  927. String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle);
  928. set_message(n + ": " + String(v));
  929. } else if (m->get_button_mask() & BUTTON_MASK_LEFT) {
  930. if (nav_scheme == NAVIGATION_MAYA && m->get_alt()) {
  931. nav_mode = NAVIGATION_ORBIT;
  932. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt() && m->get_shift()) {
  933. nav_mode = NAVIGATION_PAN;
  934. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt() && m->get_control()) {
  935. nav_mode = NAVIGATION_ZOOM;
  936. } else if (nav_scheme == NAVIGATION_MODO && m->get_alt()) {
  937. nav_mode = NAVIGATION_ORBIT;
  938. } else {
  939. if (clicked) {
  940. if (!clicked_includes_current) {
  941. _select_clicked(clicked_wants_append, true);
  942. //clickd processing was deferred
  943. }
  944. _compute_edit(_edit.mouse_pos);
  945. clicked = 0;
  946. _edit.mode = TRANSFORM_TRANSLATE;
  947. }
  948. if (cursor.region_select && nav_mode == NAVIGATION_NONE) {
  949. cursor.region_end = m->get_position();
  950. surface->update();
  951. return;
  952. }
  953. if (_edit.mode == TRANSFORM_NONE && nav_mode == NAVIGATION_NONE)
  954. return;
  955. Vector3 ray_pos = _get_ray_pos(m->get_position());
  956. Vector3 ray = _get_ray(m->get_position());
  957. switch (_edit.mode) {
  958. case TRANSFORM_SCALE: {
  959. Vector3 motion_mask;
  960. Plane plane;
  961. bool plane_mv = false;
  962. switch (_edit.plane) {
  963. case TRANSFORM_VIEW:
  964. motion_mask = Vector3(0, 0, 0);
  965. plane = Plane(_edit.center, _get_camera_normal());
  966. break;
  967. case TRANSFORM_X_AXIS:
  968. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0);
  969. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  970. break;
  971. case TRANSFORM_Y_AXIS:
  972. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(1);
  973. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  974. break;
  975. case TRANSFORM_Z_AXIS:
  976. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2);
  977. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  978. break;
  979. case TRANSFORM_YZ:
  980. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2) + spatial_editor->get_gizmo_transform().basis.get_axis(1);
  981. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  982. plane_mv = true;
  983. break;
  984. case TRANSFORM_XZ:
  985. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2) + spatial_editor->get_gizmo_transform().basis.get_axis(0);
  986. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  987. plane_mv = true;
  988. break;
  989. case TRANSFORM_XY:
  990. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0) + spatial_editor->get_gizmo_transform().basis.get_axis(1);
  991. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  992. plane_mv = true;
  993. break;
  994. }
  995. Vector3 intersection;
  996. if (!plane.intersects_ray(ray_pos, ray, &intersection))
  997. break;
  998. Vector3 click;
  999. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click))
  1000. break;
  1001. Vector3 motion = intersection - click;
  1002. if (_edit.plane != TRANSFORM_VIEW) {
  1003. if (!plane_mv) {
  1004. motion = motion_mask.dot(motion) * motion_mask;
  1005. } else {
  1006. // Alternative planar scaling mode
  1007. if (_get_key_modifier(m) != KEY_SHIFT) {
  1008. motion = motion_mask.dot(motion) * motion_mask;
  1009. }
  1010. }
  1011. } else {
  1012. float center_click_dist = click.distance_to(_edit.center);
  1013. float center_inters_dist = intersection.distance_to(_edit.center);
  1014. if (center_click_dist == 0)
  1015. break;
  1016. float scale = center_inters_dist - center_click_dist;
  1017. motion = Vector3(scale, scale, scale);
  1018. }
  1019. List<Node *> &selection = editor_selection->get_selected_node_list();
  1020. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); // Disable local transformation for TRANSFORM_VIEW
  1021. float snap = 0;
  1022. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1023. snap = spatial_editor->get_scale_snap() / 100;
  1024. Vector3 motion_snapped = motion;
  1025. motion_snapped.snap(Vector3(snap, snap, snap));
  1026. set_message(TTR("Scaling: ") + motion_snapped);
  1027. } else {
  1028. set_message(TTR("Scaling: ") + motion);
  1029. }
  1030. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1031. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1032. if (!sp) {
  1033. continue;
  1034. }
  1035. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1036. if (!se) {
  1037. continue;
  1038. }
  1039. if (sp->has_meta("_edit_lock_")) {
  1040. continue;
  1041. }
  1042. Transform original = se->original;
  1043. Transform original_local = se->original_local;
  1044. Transform base = Transform(Basis(), _edit.center);
  1045. Transform t;
  1046. Vector3 local_scale;
  1047. if (local_coords) {
  1048. Basis g = original.basis.orthonormalized();
  1049. Vector3 local_motion = g.inverse().xform(motion);
  1050. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1051. local_motion.snap(Vector3(snap, snap, snap));
  1052. }
  1053. local_scale = original_local.basis.get_scale() * (local_motion + Vector3(1, 1, 1));
  1054. // Prevent scaling to 0 it would break the gizmo
  1055. Basis check = original_local.basis;
  1056. check.scale(local_scale);
  1057. if (check.determinant() != 0) {
  1058. // Apply scale
  1059. sp->set_scale(local_scale);
  1060. }
  1061. } else {
  1062. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1063. motion.snap(Vector3(snap, snap, snap));
  1064. }
  1065. Transform r;
  1066. r.basis.scale(motion + Vector3(1, 1, 1));
  1067. t = base * (r * (base.inverse() * original));
  1068. // Apply scale
  1069. sp->set_global_transform(t);
  1070. }
  1071. }
  1072. surface->update();
  1073. } break;
  1074. case TRANSFORM_TRANSLATE: {
  1075. Vector3 motion_mask;
  1076. Plane plane;
  1077. bool plane_mv = false;
  1078. switch (_edit.plane) {
  1079. case TRANSFORM_VIEW:
  1080. plane = Plane(_edit.center, _get_camera_normal());
  1081. break;
  1082. case TRANSFORM_X_AXIS:
  1083. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(0);
  1084. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1085. break;
  1086. case TRANSFORM_Y_AXIS:
  1087. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(1);
  1088. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1089. break;
  1090. case TRANSFORM_Z_AXIS:
  1091. motion_mask = spatial_editor->get_gizmo_transform().basis.get_axis(2);
  1092. plane = Plane(_edit.center, motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  1093. break;
  1094. case TRANSFORM_YZ:
  1095. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  1096. plane_mv = true;
  1097. break;
  1098. case TRANSFORM_XZ:
  1099. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  1100. plane_mv = true;
  1101. break;
  1102. case TRANSFORM_XY:
  1103. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  1104. plane_mv = true;
  1105. break;
  1106. }
  1107. Vector3 intersection;
  1108. if (!plane.intersects_ray(ray_pos, ray, &intersection))
  1109. break;
  1110. Vector3 click;
  1111. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click))
  1112. break;
  1113. Vector3 motion = intersection - click;
  1114. if (_edit.plane != TRANSFORM_VIEW) {
  1115. if (!plane_mv) {
  1116. motion = motion_mask.dot(motion) * motion_mask;
  1117. }
  1118. }
  1119. List<Node *> &selection = editor_selection->get_selected_node_list();
  1120. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); // Disable local transformation for TRANSFORM_VIEW
  1121. float snap = 0;
  1122. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1123. snap = spatial_editor->get_translate_snap();
  1124. Vector3 motion_snapped = motion;
  1125. motion_snapped.snap(Vector3(snap, snap, snap));
  1126. set_message(TTR("Translating: ") + motion_snapped);
  1127. } else {
  1128. set_message(TTR("Translating: ") + motion);
  1129. }
  1130. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1131. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1132. if (!sp) {
  1133. continue;
  1134. }
  1135. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1136. if (!se) {
  1137. continue;
  1138. }
  1139. if (sp->has_meta("_edit_lock_")) {
  1140. continue;
  1141. }
  1142. Transform original = se->original;
  1143. Transform t;
  1144. if (local_coords) {
  1145. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1146. Basis g = original.basis.orthonormalized();
  1147. Vector3 local_motion = g.inverse().xform(motion);
  1148. local_motion.snap(Vector3(snap, snap, snap));
  1149. motion = g.xform(local_motion);
  1150. }
  1151. } else {
  1152. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1153. motion.snap(Vector3(snap, snap, snap));
  1154. }
  1155. }
  1156. // Apply translation
  1157. t = original;
  1158. t.origin += motion;
  1159. sp->set_global_transform(t);
  1160. }
  1161. surface->update();
  1162. } break;
  1163. case TRANSFORM_ROTATE: {
  1164. Plane plane;
  1165. Vector3 axis;
  1166. switch (_edit.plane) {
  1167. case TRANSFORM_VIEW:
  1168. plane = Plane(_edit.center, _get_camera_normal());
  1169. break;
  1170. case TRANSFORM_X_AXIS:
  1171. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(0));
  1172. axis = Vector3(1, 0, 0);
  1173. break;
  1174. case TRANSFORM_Y_AXIS:
  1175. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(1));
  1176. axis = Vector3(0, 1, 0);
  1177. break;
  1178. case TRANSFORM_Z_AXIS:
  1179. plane = Plane(_edit.center, spatial_editor->get_gizmo_transform().basis.get_axis(2));
  1180. axis = Vector3(0, 0, 1);
  1181. break;
  1182. }
  1183. Vector3 intersection;
  1184. if (!plane.intersects_ray(ray_pos, ray, &intersection))
  1185. break;
  1186. Vector3 click;
  1187. if (!plane.intersects_ray(_edit.click_ray_pos, _edit.click_ray, &click))
  1188. break;
  1189. Vector3 y_axis = (click - _edit.center).normalized();
  1190. Vector3 x_axis = plane.normal.cross(y_axis).normalized();
  1191. float angle = Math::atan2(x_axis.dot(intersection - _edit.center), y_axis.dot(intersection - _edit.center));
  1192. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  1193. float snap = spatial_editor->get_rotate_snap();
  1194. if (snap) {
  1195. angle = Math::rad2deg(angle) + snap * 0.5; //else it won't reach +180
  1196. angle -= Math::fmod(angle, snap);
  1197. set_message(vformat(TTR("Rotating %s degrees."), rtos(angle)));
  1198. angle = Math::deg2rad(angle);
  1199. } else
  1200. set_message(vformat(TTR("Rotating %s degrees."), rtos(Math::rad2deg(angle))));
  1201. } else {
  1202. set_message(vformat(TTR("Rotating %s degrees."), rtos(Math::rad2deg(angle))));
  1203. }
  1204. List<Node *> &selection = editor_selection->get_selected_node_list();
  1205. bool local_coords = (spatial_editor->are_local_coords_enabled() && _edit.plane != TRANSFORM_VIEW); // Disable local transformation for TRANSFORM_VIEW
  1206. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1207. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1208. if (!sp)
  1209. continue;
  1210. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1211. if (!se)
  1212. continue;
  1213. if (sp->has_meta("_edit_lock_")) {
  1214. continue;
  1215. }
  1216. Transform t;
  1217. if (local_coords) {
  1218. Transform original_local = se->original_local;
  1219. Basis rot = Basis(axis, angle);
  1220. t.basis = original_local.get_basis().orthonormalized() * rot;
  1221. t.origin = original_local.origin;
  1222. // Apply rotation
  1223. sp->set_transform(t);
  1224. sp->set_scale(original_local.basis.get_scale()); // re-apply original scale
  1225. } else {
  1226. Transform original = se->original;
  1227. Transform r;
  1228. Transform base = Transform(Basis(), _edit.center);
  1229. r.basis.rotate(plane.normal, angle);
  1230. t = base * r * base.inverse() * original;
  1231. // Apply rotation
  1232. sp->set_global_transform(t);
  1233. }
  1234. }
  1235. surface->update();
  1236. } break;
  1237. default: {}
  1238. }
  1239. }
  1240. } else if ((m->get_button_mask() & BUTTON_MASK_RIGHT) || freelook_active) {
  1241. if (nav_scheme == NAVIGATION_MAYA && m->get_alt()) {
  1242. nav_mode = NAVIGATION_ZOOM;
  1243. } else if (freelook_active) {
  1244. nav_mode = NAVIGATION_LOOK;
  1245. }
  1246. } else if (m->get_button_mask() & BUTTON_MASK_MIDDLE) {
  1247. if (nav_scheme == NAVIGATION_GODOT) {
  1248. int mod = _get_key_modifier(m);
  1249. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier"))
  1250. nav_mode = NAVIGATION_PAN;
  1251. else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier"))
  1252. nav_mode = NAVIGATION_ZOOM;
  1253. else if (mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier"))
  1254. nav_mode = NAVIGATION_ORBIT;
  1255. } else if (nav_scheme == NAVIGATION_MAYA) {
  1256. if (m->get_alt())
  1257. nav_mode = NAVIGATION_PAN;
  1258. }
  1259. } else if (EditorSettings::get_singleton()->get("editors/3d/navigation/emulate_3_button_mouse")) {
  1260. // Handle trackpad (no external mouse) use case
  1261. int mod = _get_key_modifier(m);
  1262. if (mod) {
  1263. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier"))
  1264. nav_mode = NAVIGATION_PAN;
  1265. else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier"))
  1266. nav_mode = NAVIGATION_ZOOM;
  1267. else if (mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier"))
  1268. nav_mode = NAVIGATION_ORBIT;
  1269. }
  1270. }
  1271. switch (nav_mode) {
  1272. case NAVIGATION_PAN: {
  1273. _nav_pan(m, _get_warped_mouse_motion(m));
  1274. } break;
  1275. case NAVIGATION_ZOOM: {
  1276. _nav_zoom(m, m->get_relative());
  1277. } break;
  1278. case NAVIGATION_ORBIT: {
  1279. _nav_orbit(m, _get_warped_mouse_motion(m));
  1280. } break;
  1281. case NAVIGATION_LOOK: {
  1282. _nav_look(m, _get_warped_mouse_motion(m));
  1283. } break;
  1284. default: {}
  1285. }
  1286. }
  1287. Ref<InputEventMagnifyGesture> magnify_gesture = p_event;
  1288. if (magnify_gesture.is_valid()) {
  1289. if (is_freelook_active())
  1290. scale_freelook_speed(magnify_gesture->get_factor());
  1291. else
  1292. scale_cursor_distance(1.0 / magnify_gesture->get_factor());
  1293. }
  1294. Ref<InputEventPanGesture> pan_gesture = p_event;
  1295. if (pan_gesture.is_valid()) {
  1296. NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1297. NavigationMode nav_mode = NAVIGATION_NONE;
  1298. if (nav_scheme == NAVIGATION_GODOT) {
  1299. int mod = _get_key_modifier(pan_gesture);
  1300. if (mod == _get_key_modifier_setting("editors/3d/navigation/pan_modifier"))
  1301. nav_mode = NAVIGATION_PAN;
  1302. else if (mod == _get_key_modifier_setting("editors/3d/navigation/zoom_modifier"))
  1303. nav_mode = NAVIGATION_ZOOM;
  1304. else if (mod == _get_key_modifier_setting("editors/3d/navigation/orbit_modifier"))
  1305. nav_mode = NAVIGATION_ORBIT;
  1306. } else if (nav_scheme == NAVIGATION_MAYA) {
  1307. if (pan_gesture->get_alt())
  1308. nav_mode = NAVIGATION_PAN;
  1309. }
  1310. switch (nav_mode) {
  1311. case NAVIGATION_PAN: {
  1312. _nav_pan(m, pan_gesture->get_delta());
  1313. } break;
  1314. case NAVIGATION_ZOOM: {
  1315. _nav_zoom(m, pan_gesture->get_delta());
  1316. } break;
  1317. case NAVIGATION_ORBIT: {
  1318. _nav_orbit(m, pan_gesture->get_delta());
  1319. } break;
  1320. case NAVIGATION_LOOK: {
  1321. _nav_look(m, pan_gesture->get_delta());
  1322. } break;
  1323. default: {}
  1324. }
  1325. }
  1326. Ref<InputEventKey> k = p_event;
  1327. if (k.is_valid()) {
  1328. if (!k->is_pressed())
  1329. return;
  1330. if (ED_IS_SHORTCUT("spatial_editor/snap", p_event)) {
  1331. if (_edit.mode != TRANSFORM_NONE) {
  1332. _edit.snap = true;
  1333. }
  1334. }
  1335. if (ED_IS_SHORTCUT("spatial_editor/bottom_view", p_event)) {
  1336. _menu_option(VIEW_BOTTOM);
  1337. }
  1338. if (ED_IS_SHORTCUT("spatial_editor/top_view", p_event)) {
  1339. _menu_option(VIEW_TOP);
  1340. }
  1341. if (ED_IS_SHORTCUT("spatial_editor/rear_view", p_event)) {
  1342. _menu_option(VIEW_REAR);
  1343. }
  1344. if (ED_IS_SHORTCUT("spatial_editor/front_view", p_event)) {
  1345. _menu_option(VIEW_FRONT);
  1346. }
  1347. if (ED_IS_SHORTCUT("spatial_editor/left_view", p_event)) {
  1348. _menu_option(VIEW_LEFT);
  1349. }
  1350. if (ED_IS_SHORTCUT("spatial_editor/right_view", p_event)) {
  1351. _menu_option(VIEW_RIGHT);
  1352. }
  1353. if (ED_IS_SHORTCUT("spatial_editor/focus_origin", p_event)) {
  1354. _menu_option(VIEW_CENTER_TO_ORIGIN);
  1355. }
  1356. if (ED_IS_SHORTCUT("spatial_editor/focus_selection", p_event)) {
  1357. _menu_option(VIEW_CENTER_TO_SELECTION);
  1358. }
  1359. if (ED_IS_SHORTCUT("spatial_editor/switch_perspective_orthogonal", p_event)) {
  1360. _menu_option(orthogonal ? VIEW_PERSPECTIVE : VIEW_ORTHOGONAL);
  1361. _update_name();
  1362. }
  1363. if (ED_IS_SHORTCUT("spatial_editor/align_selection_with_view", p_event)) {
  1364. _menu_option(VIEW_ALIGN_SELECTION_WITH_VIEW);
  1365. }
  1366. if (ED_IS_SHORTCUT("spatial_editor/insert_anim_key", p_event)) {
  1367. if (!get_selected_count() || _edit.mode != TRANSFORM_NONE)
  1368. return;
  1369. if (!AnimationPlayerEditor::singleton->get_track_editor()->has_keying()) {
  1370. set_message(TTR("Keying is disabled (no key inserted)."));
  1371. return;
  1372. }
  1373. List<Node *> &selection = editor_selection->get_selected_node_list();
  1374. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1375. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1376. if (!sp)
  1377. continue;
  1378. emit_signal("transform_key_request", sp, "", sp->get_transform());
  1379. }
  1380. set_message(TTR("Animation Key Inserted."));
  1381. }
  1382. if (ED_IS_SHORTCUT("spatial_editor/freelook_toggle", p_event)) {
  1383. set_freelook_active(!is_freelook_active());
  1384. } else if (k->get_scancode() == KEY_ESCAPE) {
  1385. set_freelook_active(false);
  1386. }
  1387. if (k->get_scancode() == KEY_SPACE) {
  1388. if (!k->is_pressed()) emit_signal("toggle_maximize_view", this);
  1389. }
  1390. }
  1391. // freelook uses most of the useful shortcuts, like save, so its ok
  1392. // to consider freelook active as end of the line for future events.
  1393. if (freelook_active)
  1394. accept_event();
  1395. }
  1396. void SpatialEditorViewport::_nav_pan(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1397. const NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1398. real_t pan_speed = 1 / 150.0;
  1399. int pan_speed_modifier = 10;
  1400. if (nav_scheme == NAVIGATION_MAYA && p_event->get_shift())
  1401. pan_speed *= pan_speed_modifier;
  1402. Transform camera_transform;
  1403. camera_transform.translate(cursor.pos);
  1404. camera_transform.basis.rotate(Vector3(1, 0, 0), -cursor.x_rot);
  1405. camera_transform.basis.rotate(Vector3(0, 1, 0), -cursor.y_rot);
  1406. Vector3 translation(-p_relative.x * pan_speed, p_relative.y * pan_speed, 0);
  1407. translation *= cursor.distance / DISTANCE_DEFAULT;
  1408. camera_transform.translate(translation);
  1409. cursor.pos = camera_transform.origin;
  1410. }
  1411. void SpatialEditorViewport::_nav_zoom(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1412. const NavigationScheme nav_scheme = (NavigationScheme)EditorSettings::get_singleton()->get("editors/3d/navigation/navigation_scheme").operator int();
  1413. real_t zoom_speed = 1 / 80.0;
  1414. int zoom_speed_modifier = 10;
  1415. if (nav_scheme == NAVIGATION_MAYA && p_event->get_shift())
  1416. zoom_speed *= zoom_speed_modifier;
  1417. NavigationZoomStyle zoom_style = (NavigationZoomStyle)EditorSettings::get_singleton()->get("editors/3d/navigation/zoom_style").operator int();
  1418. if (zoom_style == NAVIGATION_ZOOM_HORIZONTAL) {
  1419. if (p_relative.x > 0)
  1420. scale_cursor_distance(1 - p_relative.x * zoom_speed);
  1421. else if (p_relative.x < 0)
  1422. scale_cursor_distance(1.0 / (1 + p_relative.x * zoom_speed));
  1423. } else {
  1424. if (p_relative.y > 0)
  1425. scale_cursor_distance(1 + p_relative.y * zoom_speed);
  1426. else if (p_relative.y < 0)
  1427. scale_cursor_distance(1.0 / (1 - p_relative.y * zoom_speed));
  1428. }
  1429. }
  1430. void SpatialEditorViewport::_nav_orbit(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1431. if (lock_rotation) {
  1432. _nav_pan(p_event, p_relative);
  1433. return;
  1434. }
  1435. real_t degrees_per_pixel = EditorSettings::get_singleton()->get("editors/3d/navigation_feel/orbit_sensitivity");
  1436. real_t radians_per_pixel = Math::deg2rad(degrees_per_pixel);
  1437. bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y-axis");
  1438. if (invert_y_axis) {
  1439. cursor.x_rot -= p_relative.y * radians_per_pixel;
  1440. } else {
  1441. cursor.x_rot += p_relative.y * radians_per_pixel;
  1442. }
  1443. cursor.y_rot += p_relative.x * radians_per_pixel;
  1444. if (cursor.x_rot > Math_PI / 2.0)
  1445. cursor.x_rot = Math_PI / 2.0;
  1446. if (cursor.x_rot < -Math_PI / 2.0)
  1447. cursor.x_rot = -Math_PI / 2.0;
  1448. name = "";
  1449. _update_name();
  1450. }
  1451. void SpatialEditorViewport::_nav_look(Ref<InputEventWithModifiers> p_event, const Vector2 &p_relative) {
  1452. // Freelook only works properly in perspective.
  1453. // It technically works too in ortho, but it's awful for a user due to fov being near zero
  1454. if (!orthogonal) {
  1455. real_t degrees_per_pixel = EditorSettings::get_singleton()->get("editors/3d/navigation_feel/orbit_sensitivity");
  1456. real_t radians_per_pixel = Math::deg2rad(degrees_per_pixel);
  1457. bool invert_y_axis = EditorSettings::get_singleton()->get("editors/3d/navigation/invert_y-axis");
  1458. // Note: do NOT assume the camera has the "current" transform, because it is interpolated and may have "lag".
  1459. Transform prev_camera_transform = to_camera_transform(cursor);
  1460. if (invert_y_axis) {
  1461. cursor.x_rot -= p_relative.y * radians_per_pixel;
  1462. } else {
  1463. cursor.x_rot += p_relative.y * radians_per_pixel;
  1464. }
  1465. cursor.y_rot += p_relative.x * radians_per_pixel;
  1466. if (cursor.x_rot > Math_PI / 2.0)
  1467. cursor.x_rot = Math_PI / 2.0;
  1468. if (cursor.x_rot < -Math_PI / 2.0)
  1469. cursor.x_rot = -Math_PI / 2.0;
  1470. // Look is like the opposite of Orbit: the focus point rotates around the camera
  1471. Transform camera_transform = to_camera_transform(cursor);
  1472. Vector3 pos = camera_transform.xform(Vector3(0, 0, 0));
  1473. Vector3 prev_pos = prev_camera_transform.xform(Vector3(0, 0, 0));
  1474. Vector3 diff = prev_pos - pos;
  1475. cursor.pos += diff;
  1476. name = "";
  1477. _update_name();
  1478. }
  1479. }
  1480. void SpatialEditorViewport::set_freelook_active(bool active_now) {
  1481. if (!freelook_active && active_now) {
  1482. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  1483. cursor = camera_cursor;
  1484. // Make sure eye_pos is synced, because freelook referential is eye pos rather than orbit pos
  1485. Vector3 forward = to_camera_transform(cursor).basis.xform(Vector3(0, 0, -1));
  1486. cursor.eye_pos = cursor.pos - cursor.distance * forward;
  1487. // Also sync the camera cursor, otherwise switching to freelook will be trippy if inertia is active
  1488. camera_cursor.eye_pos = cursor.eye_pos;
  1489. if (EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_speed_zoom_link")) {
  1490. // Re-adjust freelook speed from the current zoom level
  1491. real_t base_speed = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_base_speed");
  1492. freelook_speed = base_speed * cursor.distance;
  1493. }
  1494. // Hide mouse like in an FPS (warping doesn't work)
  1495. OS::get_singleton()->set_mouse_mode(OS::MOUSE_MODE_CAPTURED);
  1496. } else if (freelook_active && !active_now) {
  1497. // Sync camera cursor to cursor to "cut" interpolation jumps due to changing referential
  1498. cursor = camera_cursor;
  1499. // Restore mouse
  1500. OS::get_singleton()->set_mouse_mode(OS::MOUSE_MODE_VISIBLE);
  1501. }
  1502. freelook_active = active_now;
  1503. }
  1504. void SpatialEditorViewport::scale_cursor_distance(real_t scale) {
  1505. // Prevents zero distance which would short-circuit any scaling
  1506. if (cursor.distance < ZOOM_MIN_DISTANCE)
  1507. cursor.distance = ZOOM_MIN_DISTANCE;
  1508. cursor.distance *= scale;
  1509. if (cursor.distance < ZOOM_MIN_DISTANCE)
  1510. cursor.distance = ZOOM_MIN_DISTANCE;
  1511. zoom_indicator_delay = ZOOM_INDICATOR_DELAY_S;
  1512. surface->update();
  1513. }
  1514. void SpatialEditorViewport::scale_freelook_speed(real_t scale) {
  1515. // Prevents zero distance which would short-circuit any scaling
  1516. if (freelook_speed < FREELOOK_MIN_SPEED)
  1517. freelook_speed = FREELOOK_MIN_SPEED;
  1518. freelook_speed *= scale;
  1519. if (freelook_speed < FREELOOK_MIN_SPEED)
  1520. freelook_speed = FREELOOK_MIN_SPEED;
  1521. zoom_indicator_delay = ZOOM_INDICATOR_DELAY_S;
  1522. surface->update();
  1523. }
  1524. Point2i SpatialEditorViewport::_get_warped_mouse_motion(const Ref<InputEventMouseMotion> &p_ev_mouse_motion) const {
  1525. Point2i relative;
  1526. if (bool(EDITOR_DEF("editors/3d/navigation/warped_mouse_panning", false))) {
  1527. relative = Input::get_singleton()->warp_mouse_motion(p_ev_mouse_motion, surface->get_global_rect());
  1528. } else {
  1529. relative = p_ev_mouse_motion->get_relative();
  1530. }
  1531. return relative;
  1532. }
  1533. static bool is_shortcut_pressed(const String &p_path) {
  1534. Ref<ShortCut> shortcut = ED_GET_SHORTCUT(p_path);
  1535. if (shortcut.is_null()) {
  1536. return false;
  1537. }
  1538. InputEventKey *k = Object::cast_to<InputEventKey>(shortcut->get_shortcut().ptr());
  1539. if (k == NULL) {
  1540. return false;
  1541. }
  1542. const Input &input = *Input::get_singleton();
  1543. int scancode = k->get_scancode();
  1544. return input.is_key_pressed(scancode);
  1545. }
  1546. void SpatialEditorViewport::_update_freelook(real_t delta) {
  1547. if (!is_freelook_active()) {
  1548. return;
  1549. }
  1550. Vector3 forward = camera->get_transform().basis.xform(Vector3(0, 0, -1));
  1551. Vector3 right = camera->get_transform().basis.xform(Vector3(1, 0, 0));
  1552. Vector3 up = camera->get_transform().basis.xform(Vector3(0, 1, 0));
  1553. Vector3 direction;
  1554. bool speed_modifier = false;
  1555. if (is_shortcut_pressed("spatial_editor/freelook_left")) {
  1556. direction -= right;
  1557. }
  1558. if (is_shortcut_pressed("spatial_editor/freelook_right")) {
  1559. direction += right;
  1560. }
  1561. if (is_shortcut_pressed("spatial_editor/freelook_forward")) {
  1562. direction += forward;
  1563. }
  1564. if (is_shortcut_pressed("spatial_editor/freelook_backwards")) {
  1565. direction -= forward;
  1566. }
  1567. if (is_shortcut_pressed("spatial_editor/freelook_up")) {
  1568. direction += up;
  1569. }
  1570. if (is_shortcut_pressed("spatial_editor/freelook_down")) {
  1571. direction -= up;
  1572. }
  1573. if (is_shortcut_pressed("spatial_editor/freelook_speed_modifier")) {
  1574. speed_modifier = true;
  1575. }
  1576. real_t speed = freelook_speed;
  1577. if (speed_modifier) {
  1578. real_t modifier_speed_factor = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_modifier_speed_factor");
  1579. speed *= modifier_speed_factor;
  1580. }
  1581. Vector3 motion = direction * speed * delta;
  1582. cursor.pos += motion;
  1583. cursor.eye_pos += motion;
  1584. }
  1585. void SpatialEditorViewport::set_message(String p_message, float p_time) {
  1586. message = p_message;
  1587. message_time = p_time;
  1588. }
  1589. void SpatialEditorViewport::_notification(int p_what) {
  1590. if (p_what == NOTIFICATION_VISIBILITY_CHANGED) {
  1591. bool visible = is_visible_in_tree();
  1592. set_process(visible);
  1593. if (visible)
  1594. _update_camera(0);
  1595. call_deferred("update_transform_gizmo_view");
  1596. }
  1597. if (p_what == NOTIFICATION_RESIZED) {
  1598. call_deferred("update_transform_gizmo_view");
  1599. }
  1600. if (p_what == NOTIFICATION_PROCESS) {
  1601. real_t delta = get_process_delta_time();
  1602. if (zoom_indicator_delay > 0) {
  1603. zoom_indicator_delay -= delta;
  1604. if (zoom_indicator_delay <= 0) {
  1605. surface->update();
  1606. }
  1607. }
  1608. _update_freelook(delta);
  1609. _update_camera(delta);
  1610. Map<Node *, Object *> &selection = editor_selection->get_selection();
  1611. bool changed = false;
  1612. bool exist = false;
  1613. for (Map<Node *, Object *>::Element *E = selection.front(); E; E = E->next()) {
  1614. Spatial *sp = Object::cast_to<Spatial>(E->key());
  1615. if (!sp)
  1616. continue;
  1617. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1618. if (!se)
  1619. continue;
  1620. VisualInstance *vi = Object::cast_to<VisualInstance>(sp);
  1621. se->aabb = vi ? vi->get_aabb() : AABB(Vector3(-0.2, -0.2, -0.2), Vector3(0.4, 0.4, 0.4));
  1622. Transform t = sp->get_global_gizmo_transform();
  1623. t.translate(se->aabb.position);
  1624. // apply AABB scaling before item's global transform
  1625. Basis aabb_s;
  1626. aabb_s.scale(se->aabb.size);
  1627. t.basis = t.basis * aabb_s;
  1628. exist = true;
  1629. if (se->last_xform == t)
  1630. continue;
  1631. changed = true;
  1632. se->last_xform = t;
  1633. VisualServer::get_singleton()->instance_set_transform(se->sbox_instance, t);
  1634. }
  1635. if (changed || (spatial_editor->is_gizmo_visible() && !exist)) {
  1636. spatial_editor->update_transform_gizmo();
  1637. }
  1638. if (message_time > 0) {
  1639. if (message != last_message) {
  1640. surface->update();
  1641. last_message = message;
  1642. }
  1643. message_time -= get_physics_process_delta_time();
  1644. if (message_time < 0)
  1645. surface->update();
  1646. }
  1647. //update shadow atlas if changed
  1648. int shadowmap_size = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/size");
  1649. int atlas_q0 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_0_subdiv");
  1650. int atlas_q1 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_1_subdiv");
  1651. int atlas_q2 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_2_subdiv");
  1652. int atlas_q3 = ProjectSettings::get_singleton()->get("rendering/quality/shadow_atlas/quadrant_3_subdiv");
  1653. viewport->set_shadow_atlas_size(shadowmap_size);
  1654. viewport->set_shadow_atlas_quadrant_subdiv(0, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q0));
  1655. viewport->set_shadow_atlas_quadrant_subdiv(1, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q1));
  1656. viewport->set_shadow_atlas_quadrant_subdiv(2, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q2));
  1657. viewport->set_shadow_atlas_quadrant_subdiv(3, Viewport::ShadowAtlasQuadrantSubdiv(atlas_q3));
  1658. bool shrink = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION));
  1659. if (shrink != viewport_container->get_stretch_shrink() > 1) {
  1660. viewport_container->set_stretch_shrink(shrink ? 2 : 1);
  1661. }
  1662. //update msaa if changed
  1663. int msaa_mode = ProjectSettings::get_singleton()->get("rendering/quality/filters/msaa");
  1664. viewport->set_msaa(Viewport::MSAA(msaa_mode));
  1665. bool hdr = ProjectSettings::get_singleton()->get("rendering/quality/depth/hdr");
  1666. viewport->set_hdr(hdr);
  1667. bool show_info = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  1668. info_label->set_visible(show_info);
  1669. if (show_info) {
  1670. String text;
  1671. text += TTR("Objects Drawn") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_OBJECTS_IN_FRAME)) + "\n";
  1672. text += TTR("Material Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_MATERIAL_CHANGES_IN_FRAME)) + "\n";
  1673. text += TTR("Shader Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_SHADER_CHANGES_IN_FRAME)) + "\n";
  1674. text += TTR("Surface Changes") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_SURFACE_CHANGES_IN_FRAME)) + "\n";
  1675. text += TTR("Draw Calls") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_DRAW_CALLS_IN_FRAME)) + "\n";
  1676. text += TTR("Vertices") + ": " + itos(viewport->get_render_info(Viewport::RENDER_INFO_VERTICES_IN_FRAME));
  1677. info_label->set_text(text);
  1678. }
  1679. // FPS Counter.
  1680. bool show_fps = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FPS));
  1681. fps_label->set_visible(show_fps);
  1682. if (show_fps) {
  1683. String text;
  1684. const float temp_fps = Engine::get_singleton()->get_frames_per_second();
  1685. text += TTR("FPS") + ": " + itos(temp_fps) + " (" + String::num(1000.0f / temp_fps, 2) + " ms)";
  1686. fps_label->set_text(text);
  1687. }
  1688. }
  1689. if (p_what == NOTIFICATION_ENTER_TREE) {
  1690. surface->connect("draw", this, "_draw");
  1691. surface->connect("gui_input", this, "_sinput");
  1692. surface->connect("mouse_entered", this, "_surface_mouse_enter");
  1693. surface->connect("mouse_exited", this, "_surface_mouse_exit");
  1694. surface->connect("focus_entered", this, "_surface_focus_enter");
  1695. surface->connect("focus_exited", this, "_surface_focus_exit");
  1696. info_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1697. fps_label->add_style_override("normal", editor->get_gui_base()->get_stylebox("Information3dViewport", "EditorStyles"));
  1698. preview_camera->set_icon(get_icon("Camera", "EditorIcons"));
  1699. _init_gizmo_instance(index);
  1700. }
  1701. if (p_what == NOTIFICATION_EXIT_TREE) {
  1702. _finish_gizmo_instances();
  1703. }
  1704. if (p_what == NOTIFICATION_MOUSE_ENTER) {
  1705. }
  1706. if (p_what == NOTIFICATION_DRAW) {
  1707. }
  1708. }
  1709. // TODO That should be part of the drawing API...
  1710. static void stroke_rect(CanvasItem &ci, Rect2 rect, Color color, real_t width = 1.0) {
  1711. // a---b
  1712. // | |
  1713. // c---d
  1714. Vector2 a(rect.position);
  1715. Vector2 b(rect.position.x + rect.size.x, rect.position.y);
  1716. Vector2 c(rect.position.x, rect.position.y + rect.size.y);
  1717. Vector2 d(rect.position + rect.size);
  1718. ci.draw_line(a, b, color, width);
  1719. ci.draw_line(b, d, color, width);
  1720. ci.draw_line(d, c, color, width);
  1721. ci.draw_line(c, a, color, width);
  1722. }
  1723. static void draw_indicator_bar(Control &surface, real_t fill, Ref<Texture> icon) {
  1724. // Adjust bar size from control height
  1725. Vector2 surface_size = surface.get_size();
  1726. real_t h = surface_size.y / 2.0;
  1727. real_t y = (surface_size.y - h) / 2.0;
  1728. Rect2 r(10, y, 6, h);
  1729. real_t sy = r.size.y * fill;
  1730. // Note: because this bar appears over the viewport, it has to stay readable for any background color
  1731. // Draw both neutral dark and bright colors to account this
  1732. surface.draw_rect(r, Color(1, 1, 1, 0.2));
  1733. surface.draw_rect(Rect2(r.position.x, r.position.y + r.size.y - sy, r.size.x, sy), Color(1, 1, 1, 0.6));
  1734. stroke_rect(surface, r.grow(1), Color(0, 0, 0, 0.7));
  1735. Vector2 icon_size = icon->get_size();
  1736. Vector2 icon_pos = Vector2(r.position.x - (icon_size.x - r.size.x) / 2, r.position.y + r.size.y + 2);
  1737. surface.draw_texture(icon, icon_pos);
  1738. }
  1739. void SpatialEditorViewport::_draw() {
  1740. EditorPluginList *over_plugin_list = EditorNode::get_singleton()->get_editor_plugins_over();
  1741. if (!over_plugin_list->empty()) {
  1742. over_plugin_list->forward_draw_over_viewport(surface);
  1743. }
  1744. EditorPluginList *force_over_plugin_list = editor->get_editor_plugins_force_over();
  1745. if (!force_over_plugin_list->empty()) {
  1746. force_over_plugin_list->forward_force_draw_over_viewport(surface);
  1747. }
  1748. if (surface->has_focus()) {
  1749. Size2 size = surface->get_size();
  1750. Rect2 r = Rect2(Point2(), size);
  1751. get_stylebox("Focus", "EditorStyles")->draw(surface->get_canvas_item(), r);
  1752. }
  1753. RID ci = surface->get_canvas_item();
  1754. if (cursor.region_select) {
  1755. VisualServer::get_singleton()->canvas_item_add_rect(ci, Rect2(cursor.region_begin, cursor.region_end - cursor.region_begin), Color(0.7, 0.7, 1.0, 0.3));
  1756. }
  1757. if (message_time > 0) {
  1758. Ref<Font> font = get_font("font", "Label");
  1759. Point2 msgpos = Point2(5, get_size().y - 20);
  1760. font->draw(ci, msgpos + Point2(1, 1), message, Color(0, 0, 0, 0.8));
  1761. font->draw(ci, msgpos + Point2(-1, -1), message, Color(0, 0, 0, 0.8));
  1762. font->draw(ci, msgpos, message, Color(1, 1, 1, 1));
  1763. }
  1764. if (_edit.mode == TRANSFORM_ROTATE) {
  1765. Point2 center = _point_to_screen(_edit.center);
  1766. VisualServer::get_singleton()->canvas_item_add_line(ci, _edit.mouse_pos, center, Color(0.4, 0.7, 1.0, 0.8));
  1767. }
  1768. if (previewing) {
  1769. Size2 ss = Size2(ProjectSettings::get_singleton()->get("display/window/size/width"), ProjectSettings::get_singleton()->get("display/window/size/height"));
  1770. float aspect = ss.aspect();
  1771. Size2 s = get_size();
  1772. Rect2 draw_rect;
  1773. switch (previewing->get_keep_aspect_mode()) {
  1774. case Camera::KEEP_WIDTH: {
  1775. draw_rect.size = Size2(s.width, s.width / aspect);
  1776. draw_rect.position.x = 0;
  1777. draw_rect.position.y = (s.height - draw_rect.size.y) * 0.5;
  1778. } break;
  1779. case Camera::KEEP_HEIGHT: {
  1780. draw_rect.size = Size2(s.height * aspect, s.height);
  1781. draw_rect.position.y = 0;
  1782. draw_rect.position.x = (s.width - draw_rect.size.x) * 0.5;
  1783. } break;
  1784. }
  1785. draw_rect = Rect2(Vector2(), s).clip(draw_rect);
  1786. stroke_rect(*surface, draw_rect, Color(0.6, 0.6, 0.1, 0.5), 2.0);
  1787. } else {
  1788. if (zoom_indicator_delay > 0.0) {
  1789. if (is_freelook_active()) {
  1790. // Show speed
  1791. real_t min_speed = FREELOOK_MIN_SPEED;
  1792. real_t max_speed = camera->get_zfar();
  1793. real_t scale_length = (max_speed - min_speed);
  1794. if (Math::abs(scale_length) > CMP_EPSILON) {
  1795. real_t logscale_t = 1.0 - Math::log(1 + freelook_speed - min_speed) / Math::log(1 + scale_length);
  1796. // There is no real maximum speed so that factor can become negative,
  1797. // Let's make it look asymptotic instead (will decrease slower and slower).
  1798. if (logscale_t < 0.25)
  1799. logscale_t = 0.25 * Math::exp(4.0 * logscale_t - 1.0);
  1800. draw_indicator_bar(*surface, 1.0 - logscale_t, get_icon("ViewportSpeed", "EditorIcons"));
  1801. }
  1802. } else {
  1803. // Show zoom
  1804. real_t min_distance = ZOOM_MIN_DISTANCE; // TODO Why not pick znear to limit zoom?
  1805. real_t max_distance = camera->get_zfar();
  1806. real_t scale_length = (max_distance - min_distance);
  1807. if (Math::abs(scale_length) > CMP_EPSILON) {
  1808. real_t logscale_t = 1.0 - Math::log(1 + cursor.distance - min_distance) / Math::log(1 + scale_length);
  1809. // There is no real maximum distance so that factor can become negative,
  1810. // Let's make it look asymptotic instead (will decrease slower and slower).
  1811. if (logscale_t < 0.25)
  1812. logscale_t = 0.25 * Math::exp(4.0 * logscale_t - 1.0);
  1813. draw_indicator_bar(*surface, logscale_t, get_icon("ViewportZoom", "EditorIcons"));
  1814. }
  1815. }
  1816. }
  1817. }
  1818. }
  1819. void SpatialEditorViewport::_menu_option(int p_option) {
  1820. switch (p_option) {
  1821. case VIEW_TOP: {
  1822. cursor.y_rot = 0;
  1823. cursor.x_rot = Math_PI / 2.0;
  1824. set_message(TTR("Top View."), 2);
  1825. name = TTR("Top");
  1826. _update_name();
  1827. } break;
  1828. case VIEW_BOTTOM: {
  1829. cursor.y_rot = 0;
  1830. cursor.x_rot = -Math_PI / 2.0;
  1831. set_message(TTR("Bottom View."), 2);
  1832. name = TTR("Bottom");
  1833. _update_name();
  1834. } break;
  1835. case VIEW_LEFT: {
  1836. cursor.x_rot = 0;
  1837. cursor.y_rot = Math_PI / 2.0;
  1838. set_message(TTR("Left View."), 2);
  1839. name = TTR("Left");
  1840. _update_name();
  1841. } break;
  1842. case VIEW_RIGHT: {
  1843. cursor.x_rot = 0;
  1844. cursor.y_rot = -Math_PI / 2.0;
  1845. set_message(TTR("Right View."), 2);
  1846. name = TTR("Right");
  1847. _update_name();
  1848. } break;
  1849. case VIEW_FRONT: {
  1850. cursor.x_rot = 0;
  1851. cursor.y_rot = 0;
  1852. set_message(TTR("Front View."), 2);
  1853. name = TTR("Front");
  1854. _update_name();
  1855. } break;
  1856. case VIEW_REAR: {
  1857. cursor.x_rot = 0;
  1858. cursor.y_rot = Math_PI;
  1859. set_message(TTR("Rear View."), 2);
  1860. name = TTR("Rear");
  1861. _update_name();
  1862. } break;
  1863. case VIEW_CENTER_TO_ORIGIN: {
  1864. cursor.pos = Vector3(0, 0, 0);
  1865. } break;
  1866. case VIEW_CENTER_TO_SELECTION: {
  1867. focus_selection();
  1868. } break;
  1869. case VIEW_ALIGN_SELECTION_WITH_VIEW: {
  1870. if (!get_selected_count())
  1871. break;
  1872. Transform camera_transform = camera->get_global_transform();
  1873. List<Node *> &selection = editor_selection->get_selected_node_list();
  1874. undo_redo->create_action(TTR("Align with view"));
  1875. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  1876. Spatial *sp = Object::cast_to<Spatial>(E->get());
  1877. if (!sp)
  1878. continue;
  1879. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1880. if (!se)
  1881. continue;
  1882. Transform xform = camera_transform;
  1883. xform.scale_basis(sp->get_scale());
  1884. undo_redo->add_do_method(sp, "set_global_transform", xform);
  1885. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  1886. }
  1887. undo_redo->commit_action();
  1888. } break;
  1889. case VIEW_ENVIRONMENT: {
  1890. int idx = view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT);
  1891. bool current = view_menu->get_popup()->is_item_checked(idx);
  1892. current = !current;
  1893. if (current) {
  1894. camera->set_environment(RES());
  1895. } else {
  1896. camera->set_environment(SpatialEditor::get_singleton()->get_viewport_environment());
  1897. }
  1898. view_menu->get_popup()->set_item_checked(idx, current);
  1899. } break;
  1900. case VIEW_PERSPECTIVE: {
  1901. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  1902. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), false);
  1903. orthogonal = false;
  1904. call_deferred("update_transform_gizmo_view");
  1905. _update_name();
  1906. } break;
  1907. case VIEW_ORTHOGONAL: {
  1908. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), false);
  1909. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), true);
  1910. orthogonal = true;
  1911. call_deferred("update_transform_gizmo_view");
  1912. _update_name();
  1913. } break;
  1914. case VIEW_LOCK_ROTATION: {
  1915. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  1916. bool current = view_menu->get_popup()->is_item_checked(idx);
  1917. lock_rotation = !current;
  1918. view_menu->get_popup()->set_item_checked(idx, !current);
  1919. if (lock_rotation) {
  1920. view_menu->set_icon(get_icon("Lock", "EditorIcons"));
  1921. } else {
  1922. view_menu->set_icon(Ref<Texture>());
  1923. }
  1924. } break;
  1925. case VIEW_AUDIO_LISTENER: {
  1926. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  1927. bool current = view_menu->get_popup()->is_item_checked(idx);
  1928. current = !current;
  1929. viewport->set_as_audio_listener(current);
  1930. view_menu->get_popup()->set_item_checked(idx, current);
  1931. } break;
  1932. case VIEW_AUDIO_DOPPLER: {
  1933. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  1934. bool current = view_menu->get_popup()->is_item_checked(idx);
  1935. current = !current;
  1936. camera->set_doppler_tracking(current ? Camera::DOPPLER_TRACKING_IDLE_STEP : Camera::DOPPLER_TRACKING_DISABLED);
  1937. view_menu->get_popup()->set_item_checked(idx, current);
  1938. } break;
  1939. case VIEW_GIZMOS: {
  1940. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  1941. bool current = view_menu->get_popup()->is_item_checked(idx);
  1942. current = !current;
  1943. if (current)
  1944. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + index)) | (1 << GIZMO_EDIT_LAYER) | (1 << GIZMO_GRID_LAYER));
  1945. else
  1946. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + index)) | (1 << GIZMO_GRID_LAYER));
  1947. view_menu->get_popup()->set_item_checked(idx, current);
  1948. } break;
  1949. case VIEW_HALF_RESOLUTION: {
  1950. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  1951. bool current = view_menu->get_popup()->is_item_checked(idx);
  1952. current = !current;
  1953. view_menu->get_popup()->set_item_checked(idx, current);
  1954. } break;
  1955. case VIEW_INFORMATION: {
  1956. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  1957. bool current = view_menu->get_popup()->is_item_checked(idx);
  1958. view_menu->get_popup()->set_item_checked(idx, !current);
  1959. } break;
  1960. case VIEW_FPS: {
  1961. int idx = view_menu->get_popup()->get_item_index(VIEW_FPS);
  1962. bool current = view_menu->get_popup()->is_item_checked(idx);
  1963. view_menu->get_popup()->set_item_checked(idx, !current);
  1964. if (current)
  1965. preview_camera->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  1966. else
  1967. preview_camera->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 15 * EDSCALE + fps_label->get_size().height);
  1968. } break;
  1969. case VIEW_DISPLAY_NORMAL: {
  1970. viewport->set_debug_draw(Viewport::DEBUG_DRAW_DISABLED);
  1971. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true);
  1972. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  1973. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  1974. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  1975. } break;
  1976. case VIEW_DISPLAY_WIREFRAME: {
  1977. viewport->set_debug_draw(Viewport::DEBUG_DRAW_WIREFRAME);
  1978. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  1979. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), true);
  1980. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  1981. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  1982. } break;
  1983. case VIEW_DISPLAY_OVERDRAW: {
  1984. viewport->set_debug_draw(Viewport::DEBUG_DRAW_OVERDRAW);
  1985. VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_OVERDRAW);
  1986. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  1987. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  1988. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), true);
  1989. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), false);
  1990. } break;
  1991. case VIEW_DISPLAY_SHADELESS: {
  1992. viewport->set_debug_draw(Viewport::DEBUG_DRAW_UNSHADED);
  1993. VisualServer::get_singleton()->scenario_set_debug(get_tree()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_SHADELESS);
  1994. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), false);
  1995. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME), false);
  1996. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW), false);
  1997. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS), true);
  1998. } break;
  1999. }
  2000. }
  2001. void SpatialEditorViewport::_preview_exited_scene() {
  2002. preview_camera->set_pressed(false);
  2003. _toggle_camera_preview(false);
  2004. view_menu->show();
  2005. }
  2006. void SpatialEditorViewport::_init_gizmo_instance(int p_idx) {
  2007. uint32_t layer = 1 << (GIZMO_BASE_LAYER + p_idx);
  2008. for (int i = 0; i < 3; i++) {
  2009. move_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2010. VS::get_singleton()->instance_set_base(move_gizmo_instance[i], spatial_editor->get_move_gizmo(i)->get_rid());
  2011. VS::get_singleton()->instance_set_scenario(move_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2012. VS::get_singleton()->instance_set_visible(move_gizmo_instance[i], false);
  2013. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2014. VS::get_singleton()->instance_set_layer_mask(move_gizmo_instance[i], layer);
  2015. move_plane_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2016. VS::get_singleton()->instance_set_base(move_plane_gizmo_instance[i], spatial_editor->get_move_plane_gizmo(i)->get_rid());
  2017. VS::get_singleton()->instance_set_scenario(move_plane_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2018. VS::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], false);
  2019. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(move_plane_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2020. VS::get_singleton()->instance_set_layer_mask(move_plane_gizmo_instance[i], layer);
  2021. rotate_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2022. VS::get_singleton()->instance_set_base(rotate_gizmo_instance[i], spatial_editor->get_rotate_gizmo(i)->get_rid());
  2023. VS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2024. VS::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], false);
  2025. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(rotate_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2026. VS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[i], layer);
  2027. scale_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2028. VS::get_singleton()->instance_set_base(scale_gizmo_instance[i], spatial_editor->get_scale_gizmo(i)->get_rid());
  2029. VS::get_singleton()->instance_set_scenario(scale_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2030. VS::get_singleton()->instance_set_visible(scale_gizmo_instance[i], false);
  2031. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2032. VS::get_singleton()->instance_set_layer_mask(scale_gizmo_instance[i], layer);
  2033. scale_plane_gizmo_instance[i] = VS::get_singleton()->instance_create();
  2034. VS::get_singleton()->instance_set_base(scale_plane_gizmo_instance[i], spatial_editor->get_scale_plane_gizmo(i)->get_rid());
  2035. VS::get_singleton()->instance_set_scenario(scale_plane_gizmo_instance[i], get_tree()->get_root()->get_world()->get_scenario());
  2036. VS::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], false);
  2037. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(scale_plane_gizmo_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  2038. VS::get_singleton()->instance_set_layer_mask(scale_plane_gizmo_instance[i], layer);
  2039. }
  2040. }
  2041. void SpatialEditorViewport::_finish_gizmo_instances() {
  2042. for (int i = 0; i < 3; i++) {
  2043. VS::get_singleton()->free(move_gizmo_instance[i]);
  2044. VS::get_singleton()->free(move_plane_gizmo_instance[i]);
  2045. VS::get_singleton()->free(rotate_gizmo_instance[i]);
  2046. VS::get_singleton()->free(scale_gizmo_instance[i]);
  2047. VS::get_singleton()->free(scale_plane_gizmo_instance[i]);
  2048. }
  2049. }
  2050. void SpatialEditorViewport::_toggle_camera_preview(bool p_activate) {
  2051. ERR_FAIL_COND(p_activate && !preview);
  2052. ERR_FAIL_COND(!p_activate && !previewing);
  2053. if (!p_activate) {
  2054. previewing->disconnect("tree_exiting", this, "_preview_exited_scene");
  2055. previewing = NULL;
  2056. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), camera->get_camera()); //restore
  2057. if (!preview)
  2058. preview_camera->hide();
  2059. view_menu->show();
  2060. surface->update();
  2061. } else {
  2062. previewing = preview;
  2063. previewing->connect("tree_exiting", this, "_preview_exited_scene");
  2064. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), preview->get_camera()); //replace
  2065. view_menu->hide();
  2066. surface->update();
  2067. }
  2068. }
  2069. void SpatialEditorViewport::_selection_result_pressed(int p_result) {
  2070. if (selection_results.size() <= p_result)
  2071. return;
  2072. clicked = selection_results[p_result].item->get_instance_id();
  2073. if (clicked) {
  2074. _select_clicked(clicked_wants_append, true);
  2075. clicked = 0;
  2076. }
  2077. }
  2078. void SpatialEditorViewport::_selection_menu_hide() {
  2079. selection_results.clear();
  2080. selection_menu->clear();
  2081. selection_menu->set_size(Vector2(0, 0));
  2082. }
  2083. void SpatialEditorViewport::set_can_preview(Camera *p_preview) {
  2084. preview = p_preview;
  2085. if (!preview_camera->is_pressed())
  2086. preview_camera->set_visible(p_preview);
  2087. }
  2088. void SpatialEditorViewport::update_transform_gizmo_view() {
  2089. if (!is_visible_in_tree())
  2090. return;
  2091. Transform xform = spatial_editor->get_gizmo_transform();
  2092. Transform camera_xform = camera->get_transform();
  2093. Vector3 camz = -camera_xform.get_basis().get_axis(2).normalized();
  2094. Vector3 camy = -camera_xform.get_basis().get_axis(1).normalized();
  2095. Plane p(camera_xform.origin, camz);
  2096. float gizmo_d = Math::abs(p.distance_to(xform.origin));
  2097. float d0 = camera->unproject_position(camera_xform.origin + camz * gizmo_d).y;
  2098. float d1 = camera->unproject_position(camera_xform.origin + camz * gizmo_d + camy).y;
  2099. float dd = Math::abs(d0 - d1);
  2100. if (dd == 0)
  2101. dd = 0.0001;
  2102. float gsize = EditorSettings::get_singleton()->get("editors/3d/manipulator_gizmo_size");
  2103. gizmo_scale = (gsize / Math::abs(dd));
  2104. Vector3 scale = Vector3(1, 1, 1) * gizmo_scale;
  2105. xform.basis.scale(scale);
  2106. for (int i = 0; i < 3; i++) {
  2107. VisualServer::get_singleton()->instance_set_transform(move_gizmo_instance[i], xform);
  2108. VisualServer::get_singleton()->instance_set_visible(move_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE));
  2109. VisualServer::get_singleton()->instance_set_transform(move_plane_gizmo_instance[i], xform);
  2110. VisualServer::get_singleton()->instance_set_visible(move_plane_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_MOVE));
  2111. VisualServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[i], xform);
  2112. VisualServer::get_singleton()->instance_set_visible(rotate_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_ROTATE));
  2113. VisualServer::get_singleton()->instance_set_transform(scale_gizmo_instance[i], xform);
  2114. VisualServer::get_singleton()->instance_set_visible(scale_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE));
  2115. VisualServer::get_singleton()->instance_set_transform(scale_plane_gizmo_instance[i], xform);
  2116. VisualServer::get_singleton()->instance_set_visible(scale_plane_gizmo_instance[i], spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode() == SpatialEditor::TOOL_MODE_SCALE));
  2117. }
  2118. }
  2119. void SpatialEditorViewport::set_state(const Dictionary &p_state) {
  2120. if (p_state.has("position"))
  2121. cursor.pos = p_state["position"];
  2122. if (p_state.has("x_rotation"))
  2123. cursor.x_rot = p_state["x_rotation"];
  2124. if (p_state.has("y_rotation"))
  2125. cursor.y_rot = p_state["y_rotation"];
  2126. if (p_state.has("distance"))
  2127. cursor.distance = p_state["distance"];
  2128. if (p_state.has("use_orthogonal")) {
  2129. bool orth = p_state["use_orthogonal"];
  2130. if (orth)
  2131. _menu_option(VIEW_ORTHOGONAL);
  2132. else
  2133. _menu_option(VIEW_PERSPECTIVE);
  2134. }
  2135. if (p_state.has("display_mode")) {
  2136. int display = p_state["display_mode"];
  2137. int idx = view_menu->get_popup()->get_item_index(display);
  2138. if (!view_menu->get_popup()->is_item_checked(idx))
  2139. _menu_option(display);
  2140. }
  2141. if (p_state.has("lock_rotation")) {
  2142. lock_rotation = p_state["lock_rotation"];
  2143. int idx = view_menu->get_popup()->get_item_index(VIEW_LOCK_ROTATION);
  2144. view_menu->get_popup()->set_item_checked(idx, lock_rotation);
  2145. }
  2146. if (p_state.has("use_environment")) {
  2147. bool env = p_state["use_environment"];
  2148. if (env != camera->get_environment().is_valid())
  2149. _menu_option(VIEW_ENVIRONMENT);
  2150. }
  2151. if (p_state.has("listener")) {
  2152. bool listener = p_state["listener"];
  2153. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  2154. viewport->set_as_audio_listener(listener);
  2155. view_menu->get_popup()->set_item_checked(idx, listener);
  2156. }
  2157. if (p_state.has("doppler")) {
  2158. bool doppler = p_state["doppler"];
  2159. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER);
  2160. camera->set_doppler_tracking(doppler ? Camera::DOPPLER_TRACKING_IDLE_STEP : Camera::DOPPLER_TRACKING_DISABLED);
  2161. view_menu->get_popup()->set_item_checked(idx, doppler);
  2162. }
  2163. if (p_state.has("gizmos")) {
  2164. bool gizmos = p_state["gizmos"];
  2165. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  2166. if (view_menu->get_popup()->is_item_checked(idx) != gizmos)
  2167. _menu_option(VIEW_GIZMOS);
  2168. }
  2169. if (p_state.has("information")) {
  2170. bool information = p_state["information"];
  2171. int idx = view_menu->get_popup()->get_item_index(VIEW_INFORMATION);
  2172. if (view_menu->get_popup()->is_item_checked(idx) != information)
  2173. _menu_option(VIEW_INFORMATION);
  2174. }
  2175. if (p_state.has("fps")) {
  2176. bool fps = p_state["fps"];
  2177. int idx = view_menu->get_popup()->get_item_index(VIEW_FPS);
  2178. if (view_menu->get_popup()->is_item_checked(idx) != fps)
  2179. _menu_option(VIEW_FPS);
  2180. }
  2181. if (p_state.has("half_res")) {
  2182. bool half_res = p_state["half_res"];
  2183. int idx = view_menu->get_popup()->get_item_index(VIEW_HALF_RESOLUTION);
  2184. view_menu->get_popup()->set_item_checked(idx, half_res);
  2185. }
  2186. if (p_state.has("previewing")) {
  2187. Node *pv = EditorNode::get_singleton()->get_edited_scene()->get_node(p_state["previewing"]);
  2188. if (Object::cast_to<Camera>(pv)) {
  2189. previewing = Object::cast_to<Camera>(pv);
  2190. previewing->connect("tree_exiting", this, "_preview_exited_scene");
  2191. VS::get_singleton()->viewport_attach_camera(viewport->get_viewport_rid(), previewing->get_camera()); //replace
  2192. view_menu->hide();
  2193. surface->update();
  2194. preview_camera->set_pressed(true);
  2195. preview_camera->show();
  2196. }
  2197. }
  2198. }
  2199. Dictionary SpatialEditorViewport::get_state() const {
  2200. Dictionary d;
  2201. d["position"] = cursor.pos;
  2202. d["x_rotation"] = cursor.x_rot;
  2203. d["y_rotation"] = cursor.y_rot;
  2204. d["distance"] = cursor.distance;
  2205. d["use_environment"] = camera->get_environment().is_valid();
  2206. d["use_orthogonal"] = camera->get_projection() == Camera::PROJECTION_ORTHOGONAL;
  2207. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL)))
  2208. d["display_mode"] = VIEW_DISPLAY_NORMAL;
  2209. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_WIREFRAME)))
  2210. d["display_mode"] = VIEW_DISPLAY_WIREFRAME;
  2211. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_OVERDRAW)))
  2212. d["display_mode"] = VIEW_DISPLAY_OVERDRAW;
  2213. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_SHADELESS)))
  2214. d["display_mode"] = VIEW_DISPLAY_SHADELESS;
  2215. d["listener"] = viewport->is_audio_listener();
  2216. d["doppler"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_DOPPLER));
  2217. d["gizmos"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS));
  2218. d["information"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_INFORMATION));
  2219. d["fps"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(VIEW_FPS));
  2220. d["half_res"] = viewport_container->get_stretch_shrink() > 1;
  2221. if (previewing)
  2222. d["previewing"] = EditorNode::get_singleton()->get_edited_scene()->get_path_to(previewing);
  2223. if (lock_rotation)
  2224. d["lock_rotation"] = lock_rotation;
  2225. return d;
  2226. }
  2227. void SpatialEditorViewport::_bind_methods() {
  2228. ClassDB::bind_method(D_METHOD("_draw"), &SpatialEditorViewport::_draw);
  2229. ClassDB::bind_method(D_METHOD("_surface_mouse_enter"), &SpatialEditorViewport::_surface_mouse_enter);
  2230. ClassDB::bind_method(D_METHOD("_surface_mouse_exit"), &SpatialEditorViewport::_surface_mouse_exit);
  2231. ClassDB::bind_method(D_METHOD("_surface_focus_enter"), &SpatialEditorViewport::_surface_focus_enter);
  2232. ClassDB::bind_method(D_METHOD("_surface_focus_exit"), &SpatialEditorViewport::_surface_focus_exit);
  2233. ClassDB::bind_method(D_METHOD("_sinput"), &SpatialEditorViewport::_sinput);
  2234. ClassDB::bind_method(D_METHOD("_menu_option"), &SpatialEditorViewport::_menu_option);
  2235. ClassDB::bind_method(D_METHOD("_toggle_camera_preview"), &SpatialEditorViewport::_toggle_camera_preview);
  2236. ClassDB::bind_method(D_METHOD("_preview_exited_scene"), &SpatialEditorViewport::_preview_exited_scene);
  2237. ClassDB::bind_method(D_METHOD("update_transform_gizmo_view"), &SpatialEditorViewport::update_transform_gizmo_view);
  2238. ClassDB::bind_method(D_METHOD("_selection_result_pressed"), &SpatialEditorViewport::_selection_result_pressed);
  2239. ClassDB::bind_method(D_METHOD("_selection_menu_hide"), &SpatialEditorViewport::_selection_menu_hide);
  2240. ClassDB::bind_method(D_METHOD("can_drop_data_fw"), &SpatialEditorViewport::can_drop_data_fw);
  2241. ClassDB::bind_method(D_METHOD("drop_data_fw"), &SpatialEditorViewport::drop_data_fw);
  2242. ADD_SIGNAL(MethodInfo("toggle_maximize_view", PropertyInfo(Variant::OBJECT, "viewport")));
  2243. }
  2244. void SpatialEditorViewport::reset() {
  2245. orthogonal = false;
  2246. lock_rotation = false;
  2247. message_time = 0;
  2248. message = "";
  2249. last_message = "";
  2250. name = "";
  2251. cursor.x_rot = 0.5;
  2252. cursor.y_rot = 0.5;
  2253. cursor.distance = 4;
  2254. cursor.region_select = false;
  2255. cursor.pos = Vector3();
  2256. _update_name();
  2257. }
  2258. void SpatialEditorViewport::focus_selection() {
  2259. if (!get_selected_count())
  2260. return;
  2261. Vector3 center;
  2262. int count = 0;
  2263. List<Node *> &selection = editor_selection->get_selected_node_list();
  2264. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2265. Spatial *sp = Object::cast_to<Spatial>(E->get());
  2266. if (!sp)
  2267. continue;
  2268. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  2269. if (!se)
  2270. continue;
  2271. center += sp->get_global_gizmo_transform().origin;
  2272. count++;
  2273. }
  2274. if (count != 0) {
  2275. center /= float(count);
  2276. }
  2277. cursor.pos = center;
  2278. }
  2279. void SpatialEditorViewport::assign_pending_data_pointers(Spatial *p_preview_node, AABB *p_preview_bounds, AcceptDialog *p_accept) {
  2280. preview_node = p_preview_node;
  2281. preview_bounds = p_preview_bounds;
  2282. accept = p_accept;
  2283. }
  2284. Vector3 SpatialEditorViewport::_get_instance_position(const Point2 &p_pos) const {
  2285. const float MAX_DISTANCE = 10;
  2286. Vector3 world_ray = _get_ray(p_pos);
  2287. Vector3 world_pos = _get_ray_pos(p_pos);
  2288. Vector<ObjectID> instances = VisualServer::get_singleton()->instances_cull_ray(world_pos, world_ray, get_tree()->get_root()->get_world()->get_scenario());
  2289. Set<Ref<SpatialEditorGizmo> > found_gizmos;
  2290. float closest_dist = MAX_DISTANCE;
  2291. Vector3 point = world_pos + world_ray * MAX_DISTANCE;
  2292. Vector3 normal = Vector3(0.0, 0.0, 0.0);
  2293. for (int i = 0; i < instances.size(); i++) {
  2294. MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(ObjectDB::get_instance(instances[i]));
  2295. if (!mesh_instance)
  2296. continue;
  2297. Ref<SpatialEditorGizmo> seg = mesh_instance->get_gizmo();
  2298. if ((!seg.is_valid()) || found_gizmos.has(seg)) {
  2299. continue;
  2300. }
  2301. found_gizmos.insert(seg);
  2302. Vector3 hit_point;
  2303. Vector3 hit_normal;
  2304. bool inters = seg->intersect_ray(camera, p_pos, hit_point, hit_normal, NULL, false);
  2305. if (!inters)
  2306. continue;
  2307. float dist = world_pos.distance_to(hit_point);
  2308. if (dist < 0)
  2309. continue;
  2310. if (dist < closest_dist) {
  2311. closest_dist = dist;
  2312. point = hit_point;
  2313. normal = hit_normal;
  2314. }
  2315. }
  2316. Vector3 offset = Vector3();
  2317. for (int i = 0; i < 3; i++) {
  2318. if (normal[i] > 0.0)
  2319. offset[i] = (preview_bounds->get_size()[i] - (preview_bounds->get_size()[i] + preview_bounds->get_position()[i]));
  2320. else if (normal[i] < 0.0)
  2321. offset[i] = -(preview_bounds->get_size()[i] + preview_bounds->get_position()[i]);
  2322. }
  2323. return point + offset;
  2324. }
  2325. AABB SpatialEditorViewport::_calculate_spatial_bounds(const Spatial *p_parent, const AABB p_bounds) {
  2326. AABB bounds = p_bounds;
  2327. for (int i = 0; i < p_parent->get_child_count(); i++) {
  2328. Spatial *child = Object::cast_to<Spatial>(p_parent->get_child(i));
  2329. if (child) {
  2330. MeshInstance *mesh_instance = Object::cast_to<MeshInstance>(child);
  2331. if (mesh_instance) {
  2332. AABB mesh_instance_bounds = mesh_instance->get_aabb();
  2333. mesh_instance_bounds.position += mesh_instance->get_global_gizmo_transform().origin - p_parent->get_global_gizmo_transform().origin;
  2334. bounds.merge_with(mesh_instance_bounds);
  2335. }
  2336. bounds = _calculate_spatial_bounds(child, bounds);
  2337. }
  2338. }
  2339. return bounds;
  2340. }
  2341. void SpatialEditorViewport::_create_preview(const Vector<String> &files) const {
  2342. for (int i = 0; i < files.size(); i++) {
  2343. String path = files[i];
  2344. RES res = ResourceLoader::load(path);
  2345. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  2346. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  2347. if (mesh != NULL || scene != NULL) {
  2348. if (mesh != NULL) {
  2349. MeshInstance *mesh_instance = memnew(MeshInstance);
  2350. mesh_instance->set_mesh(mesh);
  2351. preview_node->add_child(mesh_instance);
  2352. } else {
  2353. if (scene.is_valid()) {
  2354. Node *instance = scene->instance();
  2355. if (instance) {
  2356. preview_node->add_child(instance);
  2357. }
  2358. }
  2359. }
  2360. editor->get_scene_root()->add_child(preview_node);
  2361. }
  2362. }
  2363. *preview_bounds = _calculate_spatial_bounds(preview_node, AABB());
  2364. }
  2365. void SpatialEditorViewport::_remove_preview() {
  2366. if (preview_node->get_parent()) {
  2367. for (int i = preview_node->get_child_count() - 1; i >= 0; i--) {
  2368. Node *node = preview_node->get_child(i);
  2369. node->queue_delete();
  2370. preview_node->remove_child(node);
  2371. }
  2372. editor->get_scene_root()->remove_child(preview_node);
  2373. }
  2374. }
  2375. bool SpatialEditorViewport::_cyclical_dependency_exists(const String &p_target_scene_path, Node *p_desired_node) {
  2376. if (p_desired_node->get_filename() == p_target_scene_path) {
  2377. return true;
  2378. }
  2379. int childCount = p_desired_node->get_child_count();
  2380. for (int i = 0; i < childCount; i++) {
  2381. Node *child = p_desired_node->get_child(i);
  2382. if (_cyclical_dependency_exists(p_target_scene_path, child)) {
  2383. return true;
  2384. }
  2385. }
  2386. return false;
  2387. }
  2388. bool SpatialEditorViewport::_create_instance(Node *parent, String &path, const Point2 &p_point) {
  2389. RES res = ResourceLoader::load(path);
  2390. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  2391. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  2392. Node *instanced_scene = NULL;
  2393. if (mesh != NULL || scene != NULL) {
  2394. if (mesh != NULL) {
  2395. MeshInstance *mesh_instance = memnew(MeshInstance);
  2396. mesh_instance->set_mesh(mesh);
  2397. mesh_instance->set_name(mesh->get_name());
  2398. instanced_scene = mesh_instance;
  2399. } else {
  2400. if (!scene.is_valid()) { // invalid scene
  2401. return false;
  2402. } else {
  2403. instanced_scene = scene->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  2404. }
  2405. }
  2406. }
  2407. if (instanced_scene == NULL) {
  2408. return false;
  2409. }
  2410. if (editor->get_edited_scene()->get_filename() != "") { // cyclical instancing
  2411. if (_cyclical_dependency_exists(editor->get_edited_scene()->get_filename(), instanced_scene)) {
  2412. memdelete(instanced_scene);
  2413. return false;
  2414. }
  2415. }
  2416. if (scene != NULL) {
  2417. instanced_scene->set_filename(ProjectSettings::get_singleton()->localize_path(path));
  2418. }
  2419. editor_data->get_undo_redo().add_do_method(parent, "add_child", instanced_scene);
  2420. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_owner", editor->get_edited_scene());
  2421. editor_data->get_undo_redo().add_do_reference(instanced_scene);
  2422. editor_data->get_undo_redo().add_undo_method(parent, "remove_child", instanced_scene);
  2423. String new_name = parent->validate_child_name(instanced_scene);
  2424. ScriptEditorDebugger *sed = ScriptEditor::get_singleton()->get_debugger();
  2425. editor_data->get_undo_redo().add_do_method(sed, "live_debug_instance_node", editor->get_edited_scene()->get_path_to(parent), path, new_name);
  2426. editor_data->get_undo_redo().add_undo_method(sed, "live_debug_remove_node", NodePath(String(editor->get_edited_scene()->get_path_to(parent)) + "/" + new_name));
  2427. Transform global_transform;
  2428. Spatial *parent_spatial = Object::cast_to<Spatial>(parent);
  2429. if (parent_spatial)
  2430. global_transform = parent_spatial->get_global_gizmo_transform();
  2431. global_transform.origin = spatial_editor->snap_point(_get_instance_position(p_point));
  2432. editor_data->get_undo_redo().add_do_method(instanced_scene, "set_global_transform", global_transform);
  2433. return true;
  2434. }
  2435. void SpatialEditorViewport::_perform_drop_data() {
  2436. _remove_preview();
  2437. Vector<String> error_files;
  2438. editor_data->get_undo_redo().create_action(TTR("Create Node"));
  2439. for (int i = 0; i < selected_files.size(); i++) {
  2440. String path = selected_files[i];
  2441. RES res = ResourceLoader::load(path);
  2442. if (res.is_null()) {
  2443. continue;
  2444. }
  2445. Ref<PackedScene> scene = Ref<PackedScene>(Object::cast_to<PackedScene>(*res));
  2446. Ref<Mesh> mesh = Ref<Mesh>(Object::cast_to<Mesh>(*res));
  2447. if (mesh != NULL || scene != NULL) {
  2448. bool success = _create_instance(target_node, path, drop_pos);
  2449. if (!success) {
  2450. error_files.push_back(path);
  2451. }
  2452. }
  2453. }
  2454. editor_data->get_undo_redo().commit_action();
  2455. if (error_files.size() > 0) {
  2456. String files_str;
  2457. for (int i = 0; i < error_files.size(); i++) {
  2458. files_str += error_files[i].get_file().get_basename() + ",";
  2459. }
  2460. files_str = files_str.substr(0, files_str.length() - 1);
  2461. accept->get_ok()->set_text(TTR("Ugh"));
  2462. accept->set_text(vformat(TTR("Error instancing scene from %s"), files_str.c_str()));
  2463. accept->popup_centered_minsize();
  2464. }
  2465. }
  2466. bool SpatialEditorViewport::can_drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) const {
  2467. bool can_instance = false;
  2468. if (!preview_node->is_inside_tree()) {
  2469. Dictionary d = p_data;
  2470. if (d.has("type") && (String(d["type"]) == "files")) {
  2471. Vector<String> files = d["files"];
  2472. List<String> scene_extensions;
  2473. ResourceLoader::get_recognized_extensions_for_type("PackedScene", &scene_extensions);
  2474. List<String> mesh_extensions;
  2475. ResourceLoader::get_recognized_extensions_for_type("Mesh", &mesh_extensions);
  2476. for (int i = 0; i < files.size(); i++) {
  2477. if (mesh_extensions.find(files[i].get_extension()) || scene_extensions.find(files[i].get_extension())) {
  2478. RES res = ResourceLoader::load(files[i]);
  2479. if (res.is_null()) {
  2480. continue;
  2481. }
  2482. String type = res->get_class();
  2483. if (type == "PackedScene") {
  2484. Ref<PackedScene> sdata = ResourceLoader::load(files[i]);
  2485. Node *instanced_scene = sdata->instance(PackedScene::GEN_EDIT_STATE_INSTANCE);
  2486. if (!instanced_scene) {
  2487. continue;
  2488. }
  2489. memdelete(instanced_scene);
  2490. } else if (type == "Mesh" || type == "ArrayMesh" || type == "PrimitiveMesh") {
  2491. Ref<Mesh> mesh = ResourceLoader::load(files[i]);
  2492. if (!mesh.is_valid()) {
  2493. continue;
  2494. }
  2495. } else {
  2496. continue;
  2497. }
  2498. can_instance = true;
  2499. break;
  2500. }
  2501. }
  2502. if (can_instance) {
  2503. _create_preview(files);
  2504. }
  2505. }
  2506. } else {
  2507. can_instance = true;
  2508. }
  2509. if (can_instance) {
  2510. Transform global_transform = Transform(Basis(), _get_instance_position(p_point));
  2511. preview_node->set_global_transform(global_transform);
  2512. }
  2513. return can_instance;
  2514. }
  2515. void SpatialEditorViewport::drop_data_fw(const Point2 &p_point, const Variant &p_data, Control *p_from) {
  2516. if (!can_drop_data_fw(p_point, p_data, p_from))
  2517. return;
  2518. bool is_shift = Input::get_singleton()->is_key_pressed(KEY_SHIFT);
  2519. selected_files.clear();
  2520. Dictionary d = p_data;
  2521. if (d.has("type") && String(d["type"]) == "files") {
  2522. selected_files = d["files"];
  2523. }
  2524. List<Node *> list = editor->get_editor_selection()->get_selected_node_list();
  2525. if (list.size() == 0) {
  2526. Node *root_node = editor->get_edited_scene();
  2527. if (root_node) {
  2528. list.push_back(root_node);
  2529. } else {
  2530. accept->get_ok()->set_text(TTR("OK :("));
  2531. accept->set_text(TTR("No parent to instance a child at."));
  2532. accept->popup_centered_minsize();
  2533. _remove_preview();
  2534. return;
  2535. }
  2536. }
  2537. if (list.size() != 1) {
  2538. accept->get_ok()->set_text(TTR("I see..."));
  2539. accept->set_text(TTR("This operation requires a single selected node."));
  2540. accept->popup_centered_minsize();
  2541. _remove_preview();
  2542. return;
  2543. }
  2544. target_node = list[0];
  2545. if (is_shift && target_node != editor->get_edited_scene()) {
  2546. target_node = target_node->get_parent();
  2547. }
  2548. drop_pos = p_point;
  2549. _perform_drop_data();
  2550. }
  2551. SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, EditorNode *p_editor, int p_index) {
  2552. _edit.mode = TRANSFORM_NONE;
  2553. _edit.plane = TRANSFORM_VIEW;
  2554. _edit.edited_gizmo = 0;
  2555. _edit.snap = 1;
  2556. _edit.gizmo_handle = 0;
  2557. index = p_index;
  2558. editor = p_editor;
  2559. editor_data = editor->get_scene_tree_dock()->get_editor_data();
  2560. editor_selection = editor->get_editor_selection();
  2561. undo_redo = editor->get_undo_redo();
  2562. clicked = 0;
  2563. clicked_includes_current = false;
  2564. orthogonal = false;
  2565. message_time = 0;
  2566. zoom_indicator_delay = 0.0;
  2567. spatial_editor = p_spatial_editor;
  2568. ViewportContainer *c = memnew(ViewportContainer);
  2569. viewport_container = c;
  2570. c->set_stretch(true);
  2571. add_child(c);
  2572. c->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2573. viewport = memnew(Viewport);
  2574. viewport->set_disable_input(true);
  2575. c->add_child(viewport);
  2576. surface = memnew(Control);
  2577. surface->set_drag_forwarding(this);
  2578. add_child(surface);
  2579. surface->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  2580. surface->set_clip_contents(true);
  2581. camera = memnew(Camera);
  2582. camera->set_disable_gizmo(true);
  2583. camera->set_cull_mask(((1 << 20) - 1) | (1 << (GIZMO_BASE_LAYER + p_index)) | (1 << GIZMO_EDIT_LAYER) | (1 << GIZMO_GRID_LAYER));
  2584. viewport->add_child(camera);
  2585. camera->make_current();
  2586. surface->set_focus_mode(FOCUS_ALL);
  2587. view_menu = memnew(MenuButton);
  2588. surface->add_child(view_menu);
  2589. view_menu->set_position(Point2(4, 4) * EDSCALE);
  2590. view_menu->set_self_modulate(Color(1, 1, 1, 0.5));
  2591. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/top_view"), VIEW_TOP);
  2592. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/bottom_view"), VIEW_BOTTOM);
  2593. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/left_view"), VIEW_LEFT);
  2594. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/right_view"), VIEW_RIGHT);
  2595. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/front_view"), VIEW_FRONT);
  2596. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/rear_view"), VIEW_REAR);
  2597. view_menu->get_popup()->add_separator();
  2598. view_menu->get_popup()->add_radio_check_item(TTR("Perspective") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_PERSPECTIVE);
  2599. view_menu->get_popup()->add_radio_check_item(TTR("Orthogonal") + " (" + ED_GET_SHORTCUT("spatial_editor/switch_perspective_orthogonal")->get_as_text() + ")", VIEW_ORTHOGONAL);
  2600. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true);
  2601. view_menu->get_popup()->add_separator();
  2602. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_lock_rotation", TTR("Lock View Rotation")), VIEW_LOCK_ROTATION);
  2603. view_menu->get_popup()->add_separator();
  2604. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_normal", TTR("Display Normal")), VIEW_DISPLAY_NORMAL);
  2605. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_wireframe", TTR("Display Wireframe")), VIEW_DISPLAY_WIREFRAME);
  2606. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_overdraw", TTR("Display Overdraw")), VIEW_DISPLAY_OVERDRAW);
  2607. view_menu->get_popup()->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/view_display_unshaded", TTR("Display Unshaded")), VIEW_DISPLAY_SHADELESS);
  2608. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_DISPLAY_NORMAL), true);
  2609. view_menu->get_popup()->add_separator();
  2610. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_environment", TTR("View Environment")), VIEW_ENVIRONMENT);
  2611. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_gizmos", TTR("View Gizmos")), VIEW_GIZMOS);
  2612. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_information", TTR("View Information")), VIEW_INFORMATION);
  2613. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_fps", TTR("View FPS")), VIEW_FPS);
  2614. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT), true);
  2615. view_menu->get_popup()->add_separator();
  2616. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_half_resolution", TTR("Half Resolution")), VIEW_HALF_RESOLUTION);
  2617. view_menu->get_popup()->add_separator();
  2618. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_listener", TTR("Audio Listener")), VIEW_AUDIO_LISTENER);
  2619. view_menu->get_popup()->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_audio_doppler", TTR("Doppler Enable")), VIEW_AUDIO_DOPPLER);
  2620. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_GIZMOS), true);
  2621. view_menu->get_popup()->add_separator();
  2622. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_origin"), VIEW_CENTER_TO_ORIGIN);
  2623. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/focus_selection"), VIEW_CENTER_TO_SELECTION);
  2624. view_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("spatial_editor/align_selection_with_view"), VIEW_ALIGN_SELECTION_WITH_VIEW);
  2625. view_menu->get_popup()->connect("id_pressed", this, "_menu_option");
  2626. view_menu->set_disable_shortcuts(true);
  2627. ED_SHORTCUT("spatial_editor/freelook_left", TTR("Freelook Left"), KEY_A);
  2628. ED_SHORTCUT("spatial_editor/freelook_right", TTR("Freelook Right"), KEY_D);
  2629. ED_SHORTCUT("spatial_editor/freelook_forward", TTR("Freelook Forward"), KEY_W);
  2630. ED_SHORTCUT("spatial_editor/freelook_backwards", TTR("Freelook Backwards"), KEY_S);
  2631. ED_SHORTCUT("spatial_editor/freelook_up", TTR("Freelook Up"), KEY_E);
  2632. ED_SHORTCUT("spatial_editor/freelook_down", TTR("Freelook Down"), KEY_Q);
  2633. ED_SHORTCUT("spatial_editor/freelook_speed_modifier", TTR("Freelook Speed Modifier"), KEY_SHIFT);
  2634. preview_camera = memnew(Button);
  2635. preview_camera->set_toggle_mode(true);
  2636. preview_camera->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -90 * EDSCALE);
  2637. preview_camera->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  2638. preview_camera->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -10 * EDSCALE);
  2639. preview_camera->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  2640. preview_camera->set_text(TTR("Preview"));
  2641. surface->add_child(preview_camera);
  2642. preview_camera->hide();
  2643. preview_camera->connect("toggled", this, "_toggle_camera_preview");
  2644. previewing = NULL;
  2645. gizmo_scale = 1.0;
  2646. preview_node = NULL;
  2647. info_label = memnew(Label);
  2648. info_label->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -90 * EDSCALE);
  2649. info_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_END, -90 * EDSCALE);
  2650. info_label->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -10 * EDSCALE);
  2651. info_label->set_anchor_and_margin(MARGIN_BOTTOM, ANCHOR_END, -10 * EDSCALE);
  2652. info_label->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  2653. info_label->set_v_grow_direction(GROW_DIRECTION_BEGIN);
  2654. surface->add_child(info_label);
  2655. info_label->hide();
  2656. // FPS Counter.
  2657. fps_label = memnew(Label);
  2658. fps_label->set_anchor_and_margin(MARGIN_LEFT, ANCHOR_END, -90 * EDSCALE);
  2659. fps_label->set_anchor_and_margin(MARGIN_TOP, ANCHOR_BEGIN, 10 * EDSCALE);
  2660. fps_label->set_anchor_and_margin(MARGIN_RIGHT, ANCHOR_END, -10 * EDSCALE);
  2661. fps_label->set_h_grow_direction(GROW_DIRECTION_BEGIN);
  2662. surface->add_child(fps_label);
  2663. fps_label->hide();
  2664. accept = NULL;
  2665. freelook_active = false;
  2666. freelook_speed = EditorSettings::get_singleton()->get("editors/3d/freelook/freelook_base_speed");
  2667. selection_menu = memnew(PopupMenu);
  2668. add_child(selection_menu);
  2669. selection_menu->set_custom_minimum_size(Size2(100, 0) * EDSCALE);
  2670. selection_menu->connect("id_pressed", this, "_selection_result_pressed");
  2671. selection_menu->connect("popup_hide", this, "_selection_menu_hide");
  2672. if (p_index == 0) {
  2673. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER), true);
  2674. viewport->set_as_audio_listener(true);
  2675. }
  2676. name = "";
  2677. _update_name();
  2678. EditorSettings::get_singleton()->connect("settings_changed", this, "update_transform_gizmo_view");
  2679. }
  2680. //////////////////////////////////////////////////////////////
  2681. void SpatialEditorViewportContainer::_gui_input(const Ref<InputEvent> &p_event) {
  2682. Ref<InputEventMouseButton> mb = p_event;
  2683. if (mb.is_valid() && mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  2684. Vector2 size = get_size();
  2685. int h_sep = get_constant("separation", "HSplitContainer");
  2686. int v_sep = get_constant("separation", "VSplitContainer");
  2687. int mid_w = size.width * ratio_h;
  2688. int mid_h = size.height * ratio_v;
  2689. dragging_h = mb->get_position().x > (mid_w - h_sep / 2) && mb->get_position().x < (mid_w + h_sep / 2);
  2690. dragging_v = mb->get_position().y > (mid_h - v_sep / 2) && mb->get_position().y < (mid_h + v_sep / 2);
  2691. drag_begin_pos = mb->get_position();
  2692. drag_begin_ratio.x = ratio_h;
  2693. drag_begin_ratio.y = ratio_v;
  2694. switch (view) {
  2695. case VIEW_USE_1_VIEWPORT: {
  2696. dragging_h = false;
  2697. dragging_v = false;
  2698. } break;
  2699. case VIEW_USE_2_VIEWPORTS: {
  2700. dragging_h = false;
  2701. } break;
  2702. case VIEW_USE_2_VIEWPORTS_ALT: {
  2703. dragging_v = false;
  2704. } break;
  2705. case VIEW_USE_3_VIEWPORTS: {
  2706. if (dragging_v)
  2707. dragging_h = false;
  2708. else
  2709. dragging_v = false;
  2710. } break;
  2711. case VIEW_USE_3_VIEWPORTS_ALT: {
  2712. if (dragging_h)
  2713. dragging_v = false;
  2714. else
  2715. dragging_h = false;
  2716. } break;
  2717. case VIEW_USE_4_VIEWPORTS: {
  2718. } break;
  2719. }
  2720. }
  2721. if (mb.is_valid() && !mb->is_pressed() && mb->get_button_index() == BUTTON_LEFT) {
  2722. dragging_h = false;
  2723. dragging_v = false;
  2724. }
  2725. Ref<InputEventMouseMotion> mm = p_event;
  2726. if (mm.is_valid() && (dragging_h || dragging_v)) {
  2727. if (dragging_h) {
  2728. float new_ratio = drag_begin_ratio.x + (mm->get_position().x - drag_begin_pos.x) / get_size().width;
  2729. new_ratio = CLAMP(new_ratio, 40 / get_size().width, (get_size().width - 40) / get_size().width);
  2730. ratio_h = new_ratio;
  2731. queue_sort();
  2732. update();
  2733. }
  2734. if (dragging_v) {
  2735. float new_ratio = drag_begin_ratio.y + (mm->get_position().y - drag_begin_pos.y) / get_size().height;
  2736. new_ratio = CLAMP(new_ratio, 40 / get_size().height, (get_size().height - 40) / get_size().height);
  2737. ratio_v = new_ratio;
  2738. queue_sort();
  2739. update();
  2740. }
  2741. }
  2742. }
  2743. void SpatialEditorViewportContainer::_notification(int p_what) {
  2744. if (p_what == NOTIFICATION_MOUSE_ENTER || p_what == NOTIFICATION_MOUSE_EXIT) {
  2745. mouseover = (p_what == NOTIFICATION_MOUSE_ENTER);
  2746. update();
  2747. }
  2748. if (p_what == NOTIFICATION_DRAW && mouseover) {
  2749. Ref<Texture> h_grabber = get_icon("grabber", "HSplitContainer");
  2750. Ref<Texture> v_grabber = get_icon("grabber", "VSplitContainer");
  2751. Vector2 size = get_size();
  2752. int h_sep = get_constant("separation", "HSplitContainer");
  2753. int v_sep = get_constant("separation", "VSplitContainer");
  2754. int mid_w = size.width * ratio_h;
  2755. int mid_h = size.height * ratio_v;
  2756. int size_left = mid_w - h_sep / 2;
  2757. int size_bottom = size.height - mid_h - v_sep / 2;
  2758. switch (view) {
  2759. case VIEW_USE_1_VIEWPORT: {
  2760. //nothing to show
  2761. } break;
  2762. case VIEW_USE_2_VIEWPORTS: {
  2763. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  2764. } break;
  2765. case VIEW_USE_2_VIEWPORTS_ALT: {
  2766. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  2767. } break;
  2768. case VIEW_USE_3_VIEWPORTS: {
  2769. draw_texture(v_grabber, Vector2((size.width - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  2770. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, mid_h + v_grabber->get_height() / 2 + (size_bottom - h_grabber->get_height()) / 2));
  2771. } break;
  2772. case VIEW_USE_3_VIEWPORTS_ALT: {
  2773. draw_texture(v_grabber, Vector2((size_left - v_grabber->get_width()) / 2, mid_h - v_grabber->get_height() / 2));
  2774. draw_texture(h_grabber, Vector2(mid_w - h_grabber->get_width() / 2, (size.height - h_grabber->get_height()) / 2));
  2775. } break;
  2776. case VIEW_USE_4_VIEWPORTS: {
  2777. Vector2 half(mid_w, mid_h);
  2778. draw_texture(v_grabber, half - v_grabber->get_size() / 2.0);
  2779. draw_texture(h_grabber, half - h_grabber->get_size() / 2.0);
  2780. } break;
  2781. }
  2782. }
  2783. if (p_what == NOTIFICATION_SORT_CHILDREN) {
  2784. SpatialEditorViewport *viewports[4];
  2785. int vc = 0;
  2786. for (int i = 0; i < get_child_count(); i++) {
  2787. viewports[vc] = Object::cast_to<SpatialEditorViewport>(get_child(i));
  2788. if (viewports[vc]) {
  2789. vc++;
  2790. }
  2791. }
  2792. ERR_FAIL_COND(vc != 4);
  2793. Size2 size = get_size();
  2794. if (size.x < 10 || size.y < 10) {
  2795. for (int i = 0; i < 4; i++) {
  2796. viewports[i]->hide();
  2797. }
  2798. return;
  2799. }
  2800. int h_sep = get_constant("separation", "HSplitContainer");
  2801. int v_sep = get_constant("separation", "VSplitContainer");
  2802. int mid_w = size.width * ratio_h;
  2803. int mid_h = size.height * ratio_v;
  2804. int size_left = mid_w - h_sep / 2;
  2805. int size_right = size.width - mid_w - h_sep / 2;
  2806. int size_top = mid_h - v_sep / 2;
  2807. int size_bottom = size.height - mid_h - v_sep / 2;
  2808. switch (view) {
  2809. case VIEW_USE_1_VIEWPORT: {
  2810. for (int i = 1; i < 4; i++) {
  2811. viewports[i]->hide();
  2812. }
  2813. fit_child_in_rect(viewports[0], Rect2(Vector2(), size));
  2814. } break;
  2815. case VIEW_USE_2_VIEWPORTS: {
  2816. for (int i = 1; i < 4; i++) {
  2817. if (i == 1 || i == 3)
  2818. viewports[i]->hide();
  2819. else
  2820. viewports[i]->show();
  2821. }
  2822. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  2823. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size.width, size_bottom)));
  2824. } break;
  2825. case VIEW_USE_2_VIEWPORTS_ALT: {
  2826. for (int i = 1; i < 4; i++) {
  2827. if (i == 1 || i == 3)
  2828. viewports[i]->hide();
  2829. else
  2830. viewports[i]->show();
  2831. }
  2832. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size.height)));
  2833. fit_child_in_rect(viewports[2], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  2834. } break;
  2835. case VIEW_USE_3_VIEWPORTS: {
  2836. for (int i = 1; i < 4; i++) {
  2837. if (i == 1)
  2838. viewports[i]->hide();
  2839. else
  2840. viewports[i]->show();
  2841. }
  2842. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size.width, size_top)));
  2843. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  2844. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  2845. } break;
  2846. case VIEW_USE_3_VIEWPORTS_ALT: {
  2847. for (int i = 1; i < 4; i++) {
  2848. if (i == 1)
  2849. viewports[i]->hide();
  2850. else
  2851. viewports[i]->show();
  2852. }
  2853. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  2854. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  2855. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size.height)));
  2856. } break;
  2857. case VIEW_USE_4_VIEWPORTS: {
  2858. for (int i = 1; i < 4; i++) {
  2859. viewports[i]->show();
  2860. }
  2861. fit_child_in_rect(viewports[0], Rect2(Vector2(), Vector2(size_left, size_top)));
  2862. fit_child_in_rect(viewports[1], Rect2(Vector2(mid_w + h_sep / 2, 0), Vector2(size_right, size_top)));
  2863. fit_child_in_rect(viewports[2], Rect2(Vector2(0, mid_h + v_sep / 2), Vector2(size_left, size_bottom)));
  2864. fit_child_in_rect(viewports[3], Rect2(Vector2(mid_w + h_sep / 2, mid_h + v_sep / 2), Vector2(size_right, size_bottom)));
  2865. } break;
  2866. }
  2867. }
  2868. }
  2869. void SpatialEditorViewportContainer::set_view(View p_view) {
  2870. view = p_view;
  2871. queue_sort();
  2872. }
  2873. SpatialEditorViewportContainer::View SpatialEditorViewportContainer::get_view() {
  2874. return view;
  2875. }
  2876. void SpatialEditorViewportContainer::_bind_methods() {
  2877. ClassDB::bind_method("_gui_input", &SpatialEditorViewportContainer::_gui_input);
  2878. }
  2879. SpatialEditorViewportContainer::SpatialEditorViewportContainer() {
  2880. view = VIEW_USE_1_VIEWPORT;
  2881. mouseover = false;
  2882. ratio_h = 0.5;
  2883. ratio_v = 0.5;
  2884. dragging_v = false;
  2885. dragging_h = false;
  2886. }
  2887. ///////////////////////////////////////////////////////////////////
  2888. SpatialEditor *SpatialEditor::singleton = NULL;
  2889. SpatialEditorSelectedItem::~SpatialEditorSelectedItem() {
  2890. if (sbox_instance.is_valid())
  2891. VisualServer::get_singleton()->free(sbox_instance);
  2892. }
  2893. void SpatialEditor::select_gizmo_highlight_axis(int p_axis) {
  2894. for (int i = 0; i < 3; i++) {
  2895. move_gizmo[i]->surface_set_material(0, i == p_axis ? gizmo_hl : gizmo_color[i]);
  2896. move_plane_gizmo[i]->surface_set_material(0, (i + 6) == p_axis ? gizmo_hl : plane_gizmo_color[i]);
  2897. rotate_gizmo[i]->surface_set_material(0, (i + 3) == p_axis ? gizmo_hl : gizmo_color[i]);
  2898. scale_gizmo[i]->surface_set_material(0, (i + 9) == p_axis ? gizmo_hl : gizmo_color[i]);
  2899. scale_plane_gizmo[i]->surface_set_material(0, (i + 12) == p_axis ? gizmo_hl : plane_gizmo_color[i]);
  2900. }
  2901. }
  2902. int SpatialEditor::get_skeleton_visibility_state() const {
  2903. return view_menu->get_popup()->get_item_state(view_menu->get_popup()->get_item_index(MENU_VISIBILITY_SKELETON));
  2904. }
  2905. void SpatialEditor::update_transform_gizmo() {
  2906. List<Node *> &selection = editor_selection->get_selected_node_list();
  2907. AABB center;
  2908. bool first = true;
  2909. Basis gizmo_basis;
  2910. bool local_gizmo_coords = are_local_coords_enabled();
  2911. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  2912. Spatial *sp = Object::cast_to<Spatial>(E->get());
  2913. if (!sp)
  2914. continue;
  2915. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  2916. if (!se)
  2917. continue;
  2918. Transform xf = se->sp->get_global_gizmo_transform();
  2919. if (first) {
  2920. center.position = xf.origin;
  2921. first = false;
  2922. if (local_gizmo_coords) {
  2923. gizmo_basis = xf.basis;
  2924. gizmo_basis.orthonormalize();
  2925. }
  2926. } else {
  2927. center.expand_to(xf.origin);
  2928. gizmo_basis = Basis();
  2929. }
  2930. }
  2931. Vector3 pcenter = center.position + center.size * 0.5;
  2932. gizmo.visible = !first;
  2933. gizmo.transform.origin = pcenter;
  2934. gizmo.transform.basis = gizmo_basis;
  2935. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  2936. viewports[i]->update_transform_gizmo_view();
  2937. }
  2938. }
  2939. void _update_all_gizmos(Node *p_node) {
  2940. for (int i = p_node->get_child_count() - 1; 0 <= i; --i) {
  2941. Spatial *spatial_node = Object::cast_to<Spatial>(p_node->get_child(i));
  2942. if (spatial_node) {
  2943. spatial_node->update_gizmo();
  2944. }
  2945. _update_all_gizmos(p_node->get_child(i));
  2946. }
  2947. }
  2948. void SpatialEditor::update_all_gizmos() {
  2949. _update_all_gizmos(SceneTree::get_singleton()->get_root());
  2950. }
  2951. Object *SpatialEditor::_get_editor_data(Object *p_what) {
  2952. Spatial *sp = Object::cast_to<Spatial>(p_what);
  2953. if (!sp)
  2954. return NULL;
  2955. SpatialEditorSelectedItem *si = memnew(SpatialEditorSelectedItem);
  2956. si->sp = sp;
  2957. si->sbox_instance = VisualServer::get_singleton()->instance_create2(selection_box->get_rid(), sp->get_world()->get_scenario());
  2958. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(si->sbox_instance, VS::SHADOW_CASTING_SETTING_OFF);
  2959. return si;
  2960. }
  2961. void SpatialEditor::_generate_selection_box() {
  2962. AABB aabb(Vector3(), Vector3(1, 1, 1));
  2963. aabb.grow_by(aabb.get_longest_axis_size() / 20.0);
  2964. Ref<SurfaceTool> st = memnew(SurfaceTool);
  2965. st->begin(Mesh::PRIMITIVE_LINES);
  2966. for (int i = 0; i < 12; i++) {
  2967. Vector3 a, b;
  2968. aabb.get_edge(i, a, b);
  2969. /*Vector<Vector3> points;
  2970. Vector<Color> colors;
  2971. points.push_back(a);
  2972. points.push_back(b);*/
  2973. st->add_color(Color(1.0, 1.0, 0.8, 0.8));
  2974. st->add_vertex(a);
  2975. st->add_color(Color(1.0, 1.0, 0.8, 0.4));
  2976. st->add_vertex(a.linear_interpolate(b, 0.2));
  2977. st->add_color(Color(1.0, 1.0, 0.8, 0.4));
  2978. st->add_vertex(a.linear_interpolate(b, 0.8));
  2979. st->add_color(Color(1.0, 1.0, 0.8, 0.8));
  2980. st->add_vertex(b);
  2981. }
  2982. Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
  2983. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  2984. mat->set_albedo(Color(1, 1, 1));
  2985. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  2986. mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  2987. mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  2988. st->set_material(mat);
  2989. selection_box = st->commit();
  2990. }
  2991. Dictionary SpatialEditor::get_state() const {
  2992. Dictionary d;
  2993. d["snap_enabled"] = snap_enabled;
  2994. d["translate_snap"] = get_translate_snap();
  2995. d["rotate_snap"] = get_rotate_snap();
  2996. d["scale_snap"] = get_scale_snap();
  2997. d["local_coords"] = tool_option_button[TOOL_OPT_LOCAL_COORDS]->is_pressed();
  2998. int vc = 0;
  2999. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT)))
  3000. vc = 1;
  3001. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS)))
  3002. vc = 2;
  3003. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS)))
  3004. vc = 3;
  3005. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS)))
  3006. vc = 4;
  3007. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT)))
  3008. vc = 5;
  3009. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT)))
  3010. vc = 6;
  3011. d["viewport_mode"] = vc;
  3012. Array vpdata;
  3013. for (int i = 0; i < 4; i++) {
  3014. vpdata.push_back(viewports[i]->get_state());
  3015. }
  3016. d["viewports"] = vpdata;
  3017. d["show_grid"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID));
  3018. d["show_origin"] = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN));
  3019. d["fov"] = get_fov();
  3020. d["znear"] = get_znear();
  3021. d["zfar"] = get_zfar();
  3022. return d;
  3023. }
  3024. void SpatialEditor::set_state(const Dictionary &p_state) {
  3025. Dictionary d = p_state;
  3026. if (d.has("snap_enabled")) {
  3027. snap_enabled = d["snap_enabled"];
  3028. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(d["snap_enabled"]);
  3029. }
  3030. if (d.has("translate_snap"))
  3031. snap_translate->set_text(d["translate_snap"]);
  3032. if (d.has("rotate_snap"))
  3033. snap_rotate->set_text(d["rotate_snap"]);
  3034. if (d.has("scale_snap"))
  3035. snap_scale->set_text(d["scale_snap"]);
  3036. if (d.has("local_coords")) {
  3037. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(d["local_coords"]);
  3038. update_transform_gizmo();
  3039. }
  3040. if (d.has("viewport_mode")) {
  3041. int vc = d["viewport_mode"];
  3042. if (vc == 1)
  3043. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  3044. else if (vc == 2)
  3045. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  3046. else if (vc == 3)
  3047. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  3048. else if (vc == 4)
  3049. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  3050. else if (vc == 5)
  3051. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  3052. else if (vc == 6)
  3053. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  3054. }
  3055. if (d.has("viewports")) {
  3056. Array vp = d["viewports"];
  3057. ERR_FAIL_COND(vp.size() > 4);
  3058. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  3059. viewports[i]->set_state(vp[i]);
  3060. }
  3061. }
  3062. if (d.has("zfar"))
  3063. settings_zfar->set_value(float(d["zfar"]));
  3064. if (d.has("znear"))
  3065. settings_znear->set_value(float(d["znear"]));
  3066. if (d.has("fov"))
  3067. settings_fov->set_value(float(d["fov"]));
  3068. if (d.has("show_grid")) {
  3069. bool use = d["show_grid"];
  3070. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID))) {
  3071. _menu_item_pressed(MENU_VIEW_GRID);
  3072. }
  3073. }
  3074. if (d.has("show_origin")) {
  3075. bool use = d["show_origin"];
  3076. if (use != view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN))) {
  3077. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), use);
  3078. VisualServer::get_singleton()->instance_set_visible(origin_instance, use);
  3079. }
  3080. }
  3081. }
  3082. void SpatialEditor::edit(Spatial *p_spatial) {
  3083. if (p_spatial != selected) {
  3084. if (selected) {
  3085. Ref<SpatialEditorGizmo> seg = selected->get_gizmo();
  3086. if (seg.is_valid()) {
  3087. seg->set_selected(false);
  3088. selected->update_gizmo();
  3089. }
  3090. }
  3091. selected = p_spatial;
  3092. over_gizmo_handle = -1;
  3093. if (selected) {
  3094. Ref<SpatialEditorGizmo> seg = selected->get_gizmo();
  3095. if (seg.is_valid()) {
  3096. seg->set_selected(true);
  3097. selected->update_gizmo();
  3098. }
  3099. }
  3100. }
  3101. }
  3102. void SpatialEditor::_xform_dialog_action() {
  3103. Transform t;
  3104. //translation
  3105. Vector3 scale;
  3106. Vector3 rotate;
  3107. Vector3 translate;
  3108. for (int i = 0; i < 3; i++) {
  3109. translate[i] = xform_translate[i]->get_text().to_double();
  3110. rotate[i] = Math::deg2rad(xform_rotate[i]->get_text().to_double());
  3111. scale[i] = xform_scale[i]->get_text().to_double();
  3112. }
  3113. t.basis.scale(scale);
  3114. t.basis.rotate(rotate);
  3115. t.origin = translate;
  3116. undo_redo->create_action(TTR("XForm Dialog"));
  3117. List<Node *> &selection = editor_selection->get_selected_node_list();
  3118. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3119. Spatial *sp = Object::cast_to<Spatial>(E->get());
  3120. if (!sp)
  3121. continue;
  3122. SpatialEditorSelectedItem *se = editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  3123. if (!se)
  3124. continue;
  3125. bool post = xform_type->get_selected() > 0;
  3126. Transform tr = sp->get_global_gizmo_transform();
  3127. if (post)
  3128. tr = tr * t;
  3129. else {
  3130. tr.basis = t.basis * tr.basis;
  3131. tr.origin += t.origin;
  3132. }
  3133. undo_redo->add_do_method(sp, "set_global_transform", tr);
  3134. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_gizmo_transform());
  3135. }
  3136. undo_redo->commit_action();
  3137. }
  3138. void SpatialEditor::_menu_item_toggled(bool pressed, int p_option) {
  3139. switch (p_option) {
  3140. case MENU_TOOL_LOCAL_COORDS: {
  3141. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_pressed(pressed);
  3142. update_transform_gizmo();
  3143. } break;
  3144. case MENU_TOOL_USE_SNAP: {
  3145. tool_option_button[TOOL_OPT_USE_SNAP]->set_pressed(pressed);
  3146. snap_enabled = pressed;
  3147. } break;
  3148. }
  3149. }
  3150. void SpatialEditor::_menu_item_pressed(int p_option) {
  3151. switch (p_option) {
  3152. case MENU_TOOL_SELECT:
  3153. case MENU_TOOL_MOVE:
  3154. case MENU_TOOL_ROTATE:
  3155. case MENU_TOOL_SCALE:
  3156. case MENU_TOOL_LIST_SELECT: {
  3157. for (int i = 0; i < TOOL_MAX; i++)
  3158. tool_button[i]->set_pressed(i == p_option);
  3159. tool_mode = (ToolMode)p_option;
  3160. update_transform_gizmo();
  3161. } break;
  3162. case MENU_TRANSFORM_CONFIGURE_SNAP: {
  3163. snap_dialog->popup_centered(Size2(200, 180));
  3164. } break;
  3165. case MENU_TRANSFORM_DIALOG: {
  3166. for (int i = 0; i < 3; i++) {
  3167. xform_translate[i]->set_text("0");
  3168. xform_rotate[i]->set_text("0");
  3169. xform_scale[i]->set_text("1");
  3170. }
  3171. xform_dialog->popup_centered(Size2(320, 240) * EDSCALE);
  3172. } break;
  3173. case MENU_VIEW_USE_1_VIEWPORT: {
  3174. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_1_VIEWPORT);
  3175. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true);
  3176. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3177. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3178. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3179. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3180. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3181. } break;
  3182. case MENU_VIEW_USE_2_VIEWPORTS: {
  3183. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_2_VIEWPORTS);
  3184. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3185. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true);
  3186. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3187. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3188. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3189. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3190. } break;
  3191. case MENU_VIEW_USE_2_VIEWPORTS_ALT: {
  3192. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_2_VIEWPORTS_ALT);
  3193. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3194. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3195. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3196. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3197. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), true);
  3198. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3199. } break;
  3200. case MENU_VIEW_USE_3_VIEWPORTS: {
  3201. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_3_VIEWPORTS);
  3202. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3203. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3204. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), true);
  3205. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3206. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3207. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3208. } break;
  3209. case MENU_VIEW_USE_3_VIEWPORTS_ALT: {
  3210. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_3_VIEWPORTS_ALT);
  3211. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3212. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3213. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3214. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false);
  3215. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3216. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), true);
  3217. } break;
  3218. case MENU_VIEW_USE_4_VIEWPORTS: {
  3219. viewport_base->set_view(SpatialEditorViewportContainer::VIEW_USE_4_VIEWPORTS);
  3220. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false);
  3221. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false);
  3222. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false);
  3223. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), true);
  3224. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false);
  3225. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false);
  3226. } break;
  3227. case MENU_VIEW_ORIGIN: {
  3228. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  3229. is_checked = !is_checked;
  3230. VisualServer::get_singleton()->instance_set_visible(origin_instance, is_checked);
  3231. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), is_checked);
  3232. } break;
  3233. case MENU_VIEW_GRID: {
  3234. bool is_checked = view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(p_option));
  3235. grid_enabled = !is_checked;
  3236. for (int i = 0; i < 3; ++i) {
  3237. if (grid_enable[i]) {
  3238. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], grid_enabled);
  3239. grid_visible[i] = grid_enabled;
  3240. }
  3241. }
  3242. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(p_option), grid_enabled);
  3243. } break;
  3244. case MENU_VIEW_CAMERA_SETTINGS: {
  3245. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size() + Size2(50, 50));
  3246. } break;
  3247. case MENU_SNAP_TO_FLOOR: {
  3248. snap_selected_nodes_to_floor();
  3249. } break;
  3250. case MENU_LOCK_SELECTED: {
  3251. List<Node *> &selection = editor_selection->get_selected_node_list();
  3252. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3253. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  3254. if (!spatial || !spatial->is_visible_in_tree())
  3255. continue;
  3256. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root())
  3257. continue;
  3258. spatial->set_meta("_edit_lock_", true);
  3259. emit_signal("item_lock_status_changed");
  3260. }
  3261. _refresh_menu_icons();
  3262. } break;
  3263. case MENU_UNLOCK_SELECTED: {
  3264. List<Node *> &selection = editor_selection->get_selected_node_list();
  3265. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3266. Spatial *spatial = Object::cast_to<Spatial>(E->get());
  3267. if (!spatial || !spatial->is_visible_in_tree())
  3268. continue;
  3269. if (spatial->get_viewport() != EditorNode::get_singleton()->get_scene_root())
  3270. continue;
  3271. spatial->set_meta("_edit_lock_", Variant());
  3272. emit_signal("item_lock_status_changed");
  3273. }
  3274. _refresh_menu_icons();
  3275. } break;
  3276. case MENU_VISIBILITY_SKELETON: {
  3277. const int idx = view_menu->get_popup()->get_item_index(MENU_VISIBILITY_SKELETON);
  3278. view_menu->get_popup()->toggle_item_multistate(idx);
  3279. // Change icon
  3280. const int state = view_menu->get_popup()->get_item_state(idx);
  3281. switch (state) {
  3282. case 0:
  3283. view_menu->get_popup()->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_hidden"));
  3284. break;
  3285. case 1:
  3286. view_menu->get_popup()->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_visible"));
  3287. break;
  3288. case 2:
  3289. view_menu->get_popup()->set_item_icon(idx, view_menu->get_popup()->get_icon("visibility_xray"));
  3290. break;
  3291. }
  3292. update_all_gizmos();
  3293. } break;
  3294. }
  3295. }
  3296. void SpatialEditor::_init_indicators() {
  3297. {
  3298. indicator_mat.instance();
  3299. indicator_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3300. indicator_mat->set_flag(SpatialMaterial::FLAG_ALBEDO_FROM_VERTEX_COLOR, true);
  3301. indicator_mat->set_flag(SpatialMaterial::FLAG_SRGB_VERTEX_COLOR, true);
  3302. Vector<Color> origin_colors;
  3303. Vector<Vector3> origin_points;
  3304. for (int i = 0; i < 3; i++) {
  3305. Vector3 axis;
  3306. axis[i] = 1;
  3307. grid_enable[i] = false;
  3308. grid_visible[i] = false;
  3309. origin_colors.push_back(Color(axis.x, axis.y, axis.z));
  3310. origin_colors.push_back(Color(axis.x, axis.y, axis.z));
  3311. origin_points.push_back(axis * 4096);
  3312. origin_points.push_back(axis * -4096);
  3313. }
  3314. grid_enable[1] = true;
  3315. grid_visible[1] = true;
  3316. _init_grid();
  3317. origin = VisualServer::get_singleton()->mesh_create();
  3318. Array d;
  3319. d.resize(VS::ARRAY_MAX);
  3320. d[VisualServer::ARRAY_VERTEX] = origin_points;
  3321. d[VisualServer::ARRAY_COLOR] = origin_colors;
  3322. VisualServer::get_singleton()->mesh_add_surface_from_arrays(origin, VisualServer::PRIMITIVE_LINES, d);
  3323. VisualServer::get_singleton()->mesh_surface_set_material(origin, 0, indicator_mat->get_rid());
  3324. origin_instance = VisualServer::get_singleton()->instance_create2(origin, get_tree()->get_root()->get_world()->get_scenario());
  3325. VS::get_singleton()->instance_set_layer_mask(origin_instance, 1 << SpatialEditorViewport::GIZMO_GRID_LAYER);
  3326. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(origin_instance, VS::SHADOW_CASTING_SETTING_OFF);
  3327. grid_enabled = true;
  3328. last_grid_snap = 1;
  3329. }
  3330. {
  3331. //move gizmo
  3332. float gizmo_alph = EditorSettings::get_singleton()->get("editors/3d/manipulator_gizmo_opacity");
  3333. gizmo_hl = Ref<SpatialMaterial>(memnew(SpatialMaterial));
  3334. gizmo_hl->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3335. gizmo_hl->set_on_top_of_alpha();
  3336. gizmo_hl->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3337. gizmo_hl->set_albedo(Color(1, 1, 1, gizmo_alph + 0.2f));
  3338. gizmo_hl->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  3339. for (int i = 0; i < 3; i++) {
  3340. move_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3341. move_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3342. rotate_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3343. scale_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3344. scale_plane_gizmo[i] = Ref<ArrayMesh>(memnew(ArrayMesh));
  3345. Ref<SpatialMaterial> mat = memnew(SpatialMaterial);
  3346. mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3347. mat->set_on_top_of_alpha();
  3348. mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3349. Color col;
  3350. col[i] = 1.0;
  3351. col.a = gizmo_alph;
  3352. mat->set_albedo(col);
  3353. gizmo_color[i] = mat;
  3354. Vector3 ivec;
  3355. ivec[i] = 1;
  3356. Vector3 nivec;
  3357. nivec[(i + 1) % 3] = 1;
  3358. nivec[(i + 2) % 3] = 1;
  3359. Vector3 ivec2;
  3360. ivec2[(i + 1) % 3] = 1;
  3361. Vector3 ivec3;
  3362. ivec3[(i + 2) % 3] = 1;
  3363. //translate
  3364. {
  3365. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3366. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3367. // Arrow profile
  3368. const int arrow_points = 5;
  3369. Vector3 arrow[5] = {
  3370. nivec * 0.0 + ivec * 0.0,
  3371. nivec * 0.01 + ivec * 0.0,
  3372. nivec * 0.01 + ivec * GIZMO_ARROW_OFFSET,
  3373. nivec * 0.065 + ivec * GIZMO_ARROW_OFFSET,
  3374. nivec * 0.0 + ivec * (GIZMO_ARROW_OFFSET + GIZMO_ARROW_SIZE),
  3375. };
  3376. int arrow_sides = 6;
  3377. for (int k = 0; k < 6; k++) {
  3378. Basis ma(ivec, Math_PI * 2 * float(k) / arrow_sides);
  3379. Basis mb(ivec, Math_PI * 2 * float(k + 1) / arrow_sides);
  3380. for (int j = 0; j < arrow_points - 1; j++) {
  3381. Vector3 points[4] = {
  3382. ma.xform(arrow[j]),
  3383. mb.xform(arrow[j]),
  3384. mb.xform(arrow[j + 1]),
  3385. ma.xform(arrow[j + 1]),
  3386. };
  3387. surftool->add_vertex(points[0]);
  3388. surftool->add_vertex(points[1]);
  3389. surftool->add_vertex(points[2]);
  3390. surftool->add_vertex(points[0]);
  3391. surftool->add_vertex(points[2]);
  3392. surftool->add_vertex(points[3]);
  3393. }
  3394. }
  3395. surftool->set_material(mat);
  3396. surftool->commit(move_gizmo[i]);
  3397. }
  3398. // Plane Translation
  3399. {
  3400. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3401. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3402. Vector3 vec = ivec2 - ivec3;
  3403. Vector3 plane[4] = {
  3404. vec * GIZMO_PLANE_DST,
  3405. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  3406. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  3407. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  3408. };
  3409. Basis ma(ivec, Math_PI / 2);
  3410. Vector3 points[4] = {
  3411. ma.xform(plane[0]),
  3412. ma.xform(plane[1]),
  3413. ma.xform(plane[2]),
  3414. ma.xform(plane[3]),
  3415. };
  3416. surftool->add_vertex(points[0]);
  3417. surftool->add_vertex(points[1]);
  3418. surftool->add_vertex(points[2]);
  3419. surftool->add_vertex(points[0]);
  3420. surftool->add_vertex(points[2]);
  3421. surftool->add_vertex(points[3]);
  3422. Ref<SpatialMaterial> plane_mat = memnew(SpatialMaterial);
  3423. plane_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3424. plane_mat->set_on_top_of_alpha();
  3425. plane_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3426. plane_mat->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  3427. Color col;
  3428. col[i] = 1.0;
  3429. col.a = gizmo_alph;
  3430. plane_mat->set_albedo(col);
  3431. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  3432. surftool->set_material(plane_mat);
  3433. surftool->commit(move_plane_gizmo[i]);
  3434. }
  3435. // Rotate
  3436. {
  3437. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3438. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3439. Vector3 circle[5] = {
  3440. ivec * 0.02 + ivec2 * 0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3441. ivec * -0.02 + ivec2 * 0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3442. ivec * -0.02 + ivec2 * -0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3443. ivec * 0.02 + ivec2 * -0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3444. ivec * 0.02 + ivec2 * 0.02 + ivec2 * GIZMO_CIRCLE_SIZE,
  3445. };
  3446. for (int k = 0; k < 32; k++) {
  3447. Basis ma(ivec, Math_PI * 2 * float(k) / 32);
  3448. Basis mb(ivec, Math_PI * 2 * float(k + 1) / 32);
  3449. for (int j = 0; j < 4; j++) {
  3450. Vector3 points[4] = {
  3451. ma.xform(circle[j]),
  3452. mb.xform(circle[j]),
  3453. mb.xform(circle[j + 1]),
  3454. ma.xform(circle[j + 1]),
  3455. };
  3456. surftool->add_vertex(points[0]);
  3457. surftool->add_vertex(points[1]);
  3458. surftool->add_vertex(points[2]);
  3459. surftool->add_vertex(points[0]);
  3460. surftool->add_vertex(points[2]);
  3461. surftool->add_vertex(points[3]);
  3462. }
  3463. }
  3464. surftool->set_material(mat);
  3465. surftool->commit(rotate_gizmo[i]);
  3466. }
  3467. // Scale
  3468. {
  3469. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3470. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3471. // Cube arrow profile
  3472. const int arrow_points = 6;
  3473. Vector3 arrow[6] = {
  3474. nivec * 0.0 + ivec * 0.0,
  3475. nivec * 0.01 + ivec * 0.0,
  3476. nivec * 0.01 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  3477. nivec * 0.07 + ivec * 1.0 * GIZMO_SCALE_OFFSET,
  3478. nivec * 0.07 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  3479. nivec * 0.0 + ivec * 1.11 * GIZMO_SCALE_OFFSET,
  3480. };
  3481. int arrow_sides = 4;
  3482. for (int k = 0; k < 4; k++) {
  3483. Basis ma(ivec, Math_PI * 2 * float(k) / arrow_sides);
  3484. Basis mb(ivec, Math_PI * 2 * float(k + 1) / arrow_sides);
  3485. for (int j = 0; j < arrow_points - 1; j++) {
  3486. Vector3 points[4] = {
  3487. ma.xform(arrow[j]),
  3488. mb.xform(arrow[j]),
  3489. mb.xform(arrow[j + 1]),
  3490. ma.xform(arrow[j + 1]),
  3491. };
  3492. surftool->add_vertex(points[0]);
  3493. surftool->add_vertex(points[1]);
  3494. surftool->add_vertex(points[2]);
  3495. surftool->add_vertex(points[0]);
  3496. surftool->add_vertex(points[2]);
  3497. surftool->add_vertex(points[3]);
  3498. }
  3499. }
  3500. surftool->set_material(mat);
  3501. surftool->commit(scale_gizmo[i]);
  3502. }
  3503. // Plane Scale
  3504. {
  3505. Ref<SurfaceTool> surftool = memnew(SurfaceTool);
  3506. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  3507. Vector3 vec = ivec2 - ivec3;
  3508. Vector3 plane[4] = {
  3509. vec * GIZMO_PLANE_DST,
  3510. vec * GIZMO_PLANE_DST + ivec2 * GIZMO_PLANE_SIZE,
  3511. vec * (GIZMO_PLANE_DST + GIZMO_PLANE_SIZE),
  3512. vec * GIZMO_PLANE_DST - ivec3 * GIZMO_PLANE_SIZE
  3513. };
  3514. Basis ma(ivec, Math_PI / 2);
  3515. Vector3 points[4] = {
  3516. ma.xform(plane[0]),
  3517. ma.xform(plane[1]),
  3518. ma.xform(plane[2]),
  3519. ma.xform(plane[3]),
  3520. };
  3521. surftool->add_vertex(points[0]);
  3522. surftool->add_vertex(points[1]);
  3523. surftool->add_vertex(points[2]);
  3524. surftool->add_vertex(points[0]);
  3525. surftool->add_vertex(points[2]);
  3526. surftool->add_vertex(points[3]);
  3527. Ref<SpatialMaterial> plane_mat = memnew(SpatialMaterial);
  3528. plane_mat->set_flag(SpatialMaterial::FLAG_UNSHADED, true);
  3529. plane_mat->set_on_top_of_alpha();
  3530. plane_mat->set_feature(SpatialMaterial::FEATURE_TRANSPARENT, true);
  3531. plane_mat->set_cull_mode(SpatialMaterial::CULL_DISABLED);
  3532. Color col;
  3533. col[i] = 1.0;
  3534. col.a = gizmo_alph;
  3535. plane_mat->set_albedo(col);
  3536. plane_gizmo_color[i] = plane_mat; // needed, so we can draw planes from both sides
  3537. surftool->set_material(plane_mat);
  3538. surftool->commit(scale_plane_gizmo[i]);
  3539. }
  3540. }
  3541. }
  3542. _generate_selection_box();
  3543. }
  3544. void SpatialEditor::_init_grid() {
  3545. PoolVector<Color> grid_colors[3];
  3546. PoolVector<Vector3> grid_points[3];
  3547. Color primary_grid_color = EditorSettings::get_singleton()->get("editors/3d/primary_grid_color");
  3548. Color secondary_grid_color = EditorSettings::get_singleton()->get("editors/3d/secondary_grid_color");
  3549. int grid_size = EditorSettings::get_singleton()->get("editors/3d/grid_size");
  3550. int primary_grid_steps = EditorSettings::get_singleton()->get("editors/3d/primary_grid_steps");
  3551. for (int i = 0; i < 3; i++) {
  3552. Vector3 axis;
  3553. axis[i] = 1;
  3554. Vector3 axis_n1;
  3555. axis_n1[(i + 1) % 3] = 1;
  3556. Vector3 axis_n2;
  3557. axis_n2[(i + 2) % 3] = 1;
  3558. for (int j = -grid_size; j <= grid_size; j++) {
  3559. Vector3 p1 = axis_n1 * j + axis_n2 * -grid_size;
  3560. Vector3 p1_dest = p1 * (-axis_n2 + axis_n1);
  3561. Vector3 p2 = axis_n2 * j + axis_n1 * -grid_size;
  3562. Vector3 p2_dest = p2 * (-axis_n1 + axis_n2);
  3563. Color line_color = secondary_grid_color;
  3564. if (j % primary_grid_steps == 0) {
  3565. line_color = primary_grid_color;
  3566. }
  3567. grid_points[i].push_back(p1);
  3568. grid_points[i].push_back(p1_dest);
  3569. grid_colors[i].push_back(line_color);
  3570. grid_colors[i].push_back(line_color);
  3571. grid_points[i].push_back(p2);
  3572. grid_points[i].push_back(p2_dest);
  3573. grid_colors[i].push_back(line_color);
  3574. grid_colors[i].push_back(line_color);
  3575. }
  3576. grid[i] = VisualServer::get_singleton()->mesh_create();
  3577. Array d;
  3578. d.resize(VS::ARRAY_MAX);
  3579. d[VisualServer::ARRAY_VERTEX] = grid_points[i];
  3580. d[VisualServer::ARRAY_COLOR] = grid_colors[i];
  3581. VisualServer::get_singleton()->mesh_add_surface_from_arrays(grid[i], VisualServer::PRIMITIVE_LINES, d);
  3582. VisualServer::get_singleton()->mesh_surface_set_material(grid[i], 0, indicator_mat->get_rid());
  3583. grid_instance[i] = VisualServer::get_singleton()->instance_create2(grid[i], get_tree()->get_root()->get_world()->get_scenario());
  3584. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], grid_visible[i]);
  3585. VisualServer::get_singleton()->instance_geometry_set_cast_shadows_setting(grid_instance[i], VS::SHADOW_CASTING_SETTING_OFF);
  3586. VS::get_singleton()->instance_set_layer_mask(grid_instance[i], 1 << SpatialEditorViewport::GIZMO_GRID_LAYER);
  3587. }
  3588. }
  3589. void SpatialEditor::_finish_indicators() {
  3590. VisualServer::get_singleton()->free(origin_instance);
  3591. VisualServer::get_singleton()->free(origin);
  3592. _finish_grid();
  3593. }
  3594. void SpatialEditor::_finish_grid() {
  3595. for (int i = 0; i < 3; i++) {
  3596. VisualServer::get_singleton()->free(grid_instance[i]);
  3597. VisualServer::get_singleton()->free(grid[i]);
  3598. }
  3599. }
  3600. bool SpatialEditor::is_any_freelook_active() const {
  3601. for (unsigned int i = 0; i < VIEWPORTS_COUNT; ++i) {
  3602. if (viewports[i]->is_freelook_active())
  3603. return true;
  3604. }
  3605. return false;
  3606. }
  3607. void SpatialEditor::_refresh_menu_icons() {
  3608. bool all_locked = true;
  3609. List<Node *> &selection = editor_selection->get_selected_node_list();
  3610. if (selection.empty()) {
  3611. all_locked = false;
  3612. } else {
  3613. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3614. if (Object::cast_to<Spatial>(E->get()) && !Object::cast_to<Spatial>(E->get())->has_meta("_edit_lock_")) {
  3615. all_locked = false;
  3616. break;
  3617. }
  3618. }
  3619. }
  3620. tool_button[TOOL_LOCK_SELECTED]->set_visible(!all_locked);
  3621. tool_button[TOOL_LOCK_SELECTED]->set_disabled(selection.empty());
  3622. tool_button[TOOL_UNLOCK_SELECTED]->set_visible(all_locked);
  3623. }
  3624. template <typename T>
  3625. Set<T *> _get_child_nodes(Node *parent_node) {
  3626. Set<T *> nodes = Set<T *>();
  3627. T *node = Node::cast_to<T>(parent_node);
  3628. if (node) {
  3629. nodes.insert(node);
  3630. }
  3631. for (int i = 0; i < parent_node->get_child_count(); i++) {
  3632. Node *child_node = parent_node->get_child(i);
  3633. Set<T *> child_nodes = _get_child_nodes<T>(child_node);
  3634. for (typename Set<T *>::Element *I = child_nodes.front(); I; I = I->next()) {
  3635. nodes.insert(I->get());
  3636. }
  3637. }
  3638. return nodes;
  3639. }
  3640. Set<RID> _get_physics_bodies_rid(Node *node) {
  3641. Set<RID> rids = Set<RID>();
  3642. PhysicsBody *pb = Node::cast_to<PhysicsBody>(node);
  3643. if (pb) {
  3644. rids.insert(pb->get_rid());
  3645. }
  3646. Set<PhysicsBody *> child_nodes = _get_child_nodes<PhysicsBody>(node);
  3647. for (Set<PhysicsBody *>::Element *I = child_nodes.front(); I; I = I->next()) {
  3648. rids.insert(I->get()->get_rid());
  3649. }
  3650. return rids;
  3651. }
  3652. void SpatialEditor::snap_selected_nodes_to_floor() {
  3653. List<Node *> &selection = editor_selection->get_selected_node_list();
  3654. Dictionary snap_data;
  3655. for (List<Node *>::Element *E = selection.front(); E; E = E->next()) {
  3656. Spatial *sp = Object::cast_to<Spatial>(E->get());
  3657. if (sp) {
  3658. Vector3 from = Vector3();
  3659. Vector3 position_offset = Vector3();
  3660. // Priorities for snapping to floor are CollisionShapes, VisualInstances and then origin
  3661. Set<VisualInstance *> vi = _get_child_nodes<VisualInstance>(sp);
  3662. Set<CollisionShape *> cs = _get_child_nodes<CollisionShape>(sp);
  3663. if (cs.size()) {
  3664. AABB aabb = sp->get_global_transform().xform(cs.front()->get()->get_shape()->get_debug_mesh()->get_aabb());
  3665. for (Set<CollisionShape *>::Element *I = cs.front(); I; I = I->next()) {
  3666. aabb.merge_with(sp->get_global_transform().xform(I->get()->get_shape()->get_debug_mesh()->get_aabb()));
  3667. }
  3668. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  3669. from = aabb.position + size;
  3670. position_offset.y = from.y - sp->get_global_transform().origin.y;
  3671. } else if (vi.size()) {
  3672. AABB aabb = vi.front()->get()->get_transformed_aabb();
  3673. for (Set<VisualInstance *>::Element *I = vi.front(); I; I = I->next()) {
  3674. aabb.merge_with(I->get()->get_transformed_aabb());
  3675. }
  3676. Vector3 size = aabb.size * Vector3(0.5, 0.0, 0.5);
  3677. from = aabb.position + size;
  3678. position_offset.y = from.y - sp->get_global_transform().origin.y;
  3679. } else {
  3680. from = sp->get_global_transform().origin;
  3681. }
  3682. // We add a bit of margin to the from position to avoid it from snapping
  3683. // when the spatial is already on a floor and there's another floor under
  3684. // it
  3685. from = from + Vector3(0.0, 0.1, 0.0);
  3686. Dictionary d;
  3687. d["from"] = from;
  3688. d["position_offset"] = position_offset;
  3689. snap_data[sp] = d;
  3690. }
  3691. }
  3692. PhysicsDirectSpaceState *ss = get_tree()->get_root()->get_world()->get_direct_space_state();
  3693. PhysicsDirectSpaceState::RayResult result;
  3694. Array keys = snap_data.keys();
  3695. if (keys.size()) {
  3696. undo_redo->create_action("Snap Nodes To Floor");
  3697. for (int i = 0; i < keys.size(); i++) {
  3698. Node *node = keys[i];
  3699. Spatial *sp = Object::cast_to<Spatial>(node);
  3700. Dictionary d = snap_data[node];
  3701. Vector3 from = d["from"];
  3702. Vector3 position_offset = d["position_offset"];
  3703. Vector3 to = from - Vector3(0.0, 10.0, 0.0);
  3704. Set<RID> excluded = _get_physics_bodies_rid(sp);
  3705. if (ss->intersect_ray(from, to, result, excluded)) {
  3706. Transform new_transform = sp->get_global_transform();
  3707. new_transform.origin.y = result.position.y;
  3708. new_transform.origin = new_transform.origin - position_offset;
  3709. undo_redo->add_do_method(sp, "set_global_transform", new_transform);
  3710. undo_redo->add_undo_method(sp, "set_global_transform", sp->get_global_transform());
  3711. }
  3712. }
  3713. undo_redo->commit_action();
  3714. }
  3715. }
  3716. void SpatialEditor::_unhandled_key_input(Ref<InputEvent> p_event) {
  3717. if (!is_visible_in_tree() || get_viewport()->gui_has_modal_stack())
  3718. return;
  3719. snap_key_enabled = Input::get_singleton()->is_key_pressed(KEY_CONTROL);
  3720. Ref<InputEventKey> k = p_event;
  3721. if (k.is_valid()) {
  3722. // Note: need to check is_echo because first person movement keys might still be held
  3723. if (!is_any_freelook_active() && !p_event->is_echo()) {
  3724. if (!k->is_pressed())
  3725. return;
  3726. if (ED_IS_SHORTCUT("spatial_editor/tool_select", p_event))
  3727. _menu_item_pressed(MENU_TOOL_SELECT);
  3728. else if (ED_IS_SHORTCUT("spatial_editor/tool_move", p_event))
  3729. _menu_item_pressed(MENU_TOOL_MOVE);
  3730. else if (ED_IS_SHORTCUT("spatial_editor/tool_rotate", p_event))
  3731. _menu_item_pressed(MENU_TOOL_ROTATE);
  3732. else if (ED_IS_SHORTCUT("spatial_editor/tool_scale", p_event))
  3733. _menu_item_pressed(MENU_TOOL_SCALE);
  3734. else if (ED_IS_SHORTCUT("spatial_editor/snap_to_floor", p_event))
  3735. snap_selected_nodes_to_floor();
  3736. else if (ED_IS_SHORTCUT("spatial_editor/local_coords", p_event))
  3737. if (are_local_coords_enabled()) {
  3738. _menu_item_toggled(false, MENU_TOOL_LOCAL_COORDS);
  3739. } else {
  3740. _menu_item_toggled(true, MENU_TOOL_LOCAL_COORDS);
  3741. }
  3742. else if (ED_IS_SHORTCUT("spatial_editor/snap", p_event))
  3743. if (is_snap_enabled()) {
  3744. _menu_item_toggled(false, MENU_TOOL_USE_SNAP);
  3745. } else {
  3746. _menu_item_toggled(true, MENU_TOOL_USE_SNAP);
  3747. }
  3748. }
  3749. }
  3750. }
  3751. void SpatialEditor::_notification(int p_what) {
  3752. if (p_what == NOTIFICATION_READY) {
  3753. tool_button[SpatialEditor::TOOL_MODE_SELECT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  3754. tool_button[SpatialEditor::TOOL_MODE_MOVE]->set_icon(get_icon("ToolMove", "EditorIcons"));
  3755. tool_button[SpatialEditor::TOOL_MODE_ROTATE]->set_icon(get_icon("ToolRotate", "EditorIcons"));
  3756. tool_button[SpatialEditor::TOOL_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons"));
  3757. tool_button[SpatialEditor::TOOL_MODE_LIST_SELECT]->set_icon(get_icon("ListSelect", "EditorIcons"));
  3758. tool_button[SpatialEditor::TOOL_LOCK_SELECTED]->set_icon(get_icon("Lock", "EditorIcons"));
  3759. tool_button[SpatialEditor::TOOL_UNLOCK_SELECTED]->set_icon(get_icon("Unlock", "EditorIcons"));
  3760. tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->set_icon(get_icon("Object", "EditorIcons"));
  3761. tool_option_button[SpatialEditor::TOOL_OPT_USE_SNAP]->set_icon(get_icon("Snap", "EditorIcons"));
  3762. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_icon("Panels1", "EditorIcons"));
  3763. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_icon("Panels2", "EditorIcons"));
  3764. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_icon("Panels2Alt", "EditorIcons"));
  3765. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_icon("Panels3", "EditorIcons"));
  3766. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_icon("Panels3Alt", "EditorIcons"));
  3767. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_icon("Panels4", "EditorIcons"));
  3768. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VISIBILITY_SKELETON), view_menu->get_popup()->get_icon("visibility_visible"));
  3769. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  3770. _refresh_menu_icons();
  3771. get_tree()->connect("node_removed", this, "_node_removed");
  3772. EditorNode::get_singleton()->get_scene_tree_dock()->get_tree_editor()->connect("node_changed", this, "_refresh_menu_icons");
  3773. editor_selection->connect("selection_changed", this, "_refresh_menu_icons");
  3774. }
  3775. if (p_what == NOTIFICATION_ENTER_TREE) {
  3776. gizmos = memnew(SpatialEditorGizmos);
  3777. _init_indicators();
  3778. }
  3779. if (p_what == NOTIFICATION_EXIT_TREE) {
  3780. _finish_indicators();
  3781. memdelete(gizmos);
  3782. }
  3783. if (p_what == EditorSettings::NOTIFICATION_EDITOR_SETTINGS_CHANGED) {
  3784. tool_button[SpatialEditor::TOOL_MODE_SELECT]->set_icon(get_icon("ToolSelect", "EditorIcons"));
  3785. tool_button[SpatialEditor::TOOL_MODE_MOVE]->set_icon(get_icon("ToolMove", "EditorIcons"));
  3786. tool_button[SpatialEditor::TOOL_MODE_ROTATE]->set_icon(get_icon("ToolRotate", "EditorIcons"));
  3787. tool_button[SpatialEditor::TOOL_MODE_SCALE]->set_icon(get_icon("ToolScale", "EditorIcons"));
  3788. tool_button[SpatialEditor::TOOL_MODE_LIST_SELECT]->set_icon(get_icon("ListSelect", "EditorIcons"));
  3789. tool_option_button[SpatialEditor::TOOL_OPT_LOCAL_COORDS]->set_icon(get_icon("Object", "EditorIcons"));
  3790. tool_option_button[SpatialEditor::TOOL_OPT_USE_SNAP]->set_icon(get_icon("Snap", "EditorIcons"));
  3791. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), get_icon("Panels1", "EditorIcons"));
  3792. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), get_icon("Panels2", "EditorIcons"));
  3793. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), get_icon("Panels2Alt", "EditorIcons"));
  3794. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), get_icon("Panels3", "EditorIcons"));
  3795. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), get_icon("Panels3Alt", "EditorIcons"));
  3796. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), get_icon("Panels4", "EditorIcons"));
  3797. // Update grid color by rebuilding grid.
  3798. _finish_grid();
  3799. _init_grid();
  3800. }
  3801. }
  3802. void SpatialEditor::add_control_to_menu_panel(Control *p_control) {
  3803. hbc_menu->add_child(p_control);
  3804. }
  3805. void SpatialEditor::remove_control_from_menu_panel(Control *p_control) {
  3806. hbc_menu->remove_child(p_control);
  3807. }
  3808. void SpatialEditor::set_can_preview(Camera *p_preview) {
  3809. for (int i = 0; i < 4; i++) {
  3810. viewports[i]->set_can_preview(p_preview);
  3811. }
  3812. }
  3813. VSplitContainer *SpatialEditor::get_shader_split() {
  3814. return shader_split;
  3815. }
  3816. HSplitContainer *SpatialEditor::get_palette_split() {
  3817. return palette_split;
  3818. }
  3819. void SpatialEditor::_request_gizmo(Object *p_obj) {
  3820. Spatial *sp = Object::cast_to<Spatial>(p_obj);
  3821. if (!sp)
  3822. return;
  3823. if (editor->get_edited_scene() && (sp == editor->get_edited_scene() || (sp->get_owner() && editor->get_edited_scene()->is_a_parent_of(sp)))) {
  3824. Ref<SpatialEditorGizmo> seg;
  3825. for (int i = 0; i < EditorNode::get_singleton()->get_editor_data().get_editor_plugin_count(); i++) {
  3826. seg = EditorNode::get_singleton()->get_editor_data().get_editor_plugin(i)->create_spatial_gizmo(sp);
  3827. if (seg.is_valid())
  3828. break;
  3829. }
  3830. if (!seg.is_valid()) {
  3831. seg = gizmos->get_gizmo(sp);
  3832. }
  3833. if (seg.is_valid()) {
  3834. sp->set_gizmo(seg);
  3835. }
  3836. if (seg.is_valid() && sp == selected) {
  3837. seg->set_selected(true);
  3838. selected->update_gizmo();
  3839. }
  3840. }
  3841. }
  3842. void SpatialEditor::_toggle_maximize_view(Object *p_viewport) {
  3843. if (!p_viewport) return;
  3844. SpatialEditorViewport *current_viewport = Object::cast_to<SpatialEditorViewport>(p_viewport);
  3845. if (!current_viewport) return;
  3846. int index = -1;
  3847. bool maximized = false;
  3848. for (int i = 0; i < 4; i++) {
  3849. if (viewports[i] == current_viewport) {
  3850. index = i;
  3851. if (current_viewport->get_global_rect() == viewport_base->get_global_rect())
  3852. maximized = true;
  3853. break;
  3854. }
  3855. }
  3856. if (index == -1) return;
  3857. if (!maximized) {
  3858. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  3859. if (i == (uint32_t)index)
  3860. viewports[i]->set_anchors_and_margins_preset(Control::PRESET_WIDE);
  3861. else
  3862. viewports[i]->hide();
  3863. }
  3864. } else {
  3865. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++)
  3866. viewports[i]->show();
  3867. if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT)))
  3868. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  3869. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS)))
  3870. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  3871. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT)))
  3872. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  3873. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS)))
  3874. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  3875. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT)))
  3876. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  3877. else if (view_menu->get_popup()->is_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS)))
  3878. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  3879. }
  3880. }
  3881. void SpatialEditor::_node_removed(Node *p_node) {
  3882. if (p_node == selected)
  3883. selected = NULL;
  3884. }
  3885. void SpatialEditor::_bind_methods() {
  3886. ClassDB::bind_method("_unhandled_key_input", &SpatialEditor::_unhandled_key_input);
  3887. ClassDB::bind_method("_node_removed", &SpatialEditor::_node_removed);
  3888. ClassDB::bind_method("_menu_item_pressed", &SpatialEditor::_menu_item_pressed);
  3889. ClassDB::bind_method("_menu_item_toggled", &SpatialEditor::_menu_item_toggled);
  3890. ClassDB::bind_method("_xform_dialog_action", &SpatialEditor::_xform_dialog_action);
  3891. ClassDB::bind_method("_get_editor_data", &SpatialEditor::_get_editor_data);
  3892. ClassDB::bind_method("_request_gizmo", &SpatialEditor::_request_gizmo);
  3893. ClassDB::bind_method("_toggle_maximize_view", &SpatialEditor::_toggle_maximize_view);
  3894. ClassDB::bind_method("_refresh_menu_icons", &SpatialEditor::_refresh_menu_icons);
  3895. ADD_SIGNAL(MethodInfo("transform_key_request"));
  3896. ADD_SIGNAL(MethodInfo("item_lock_status_changed"));
  3897. }
  3898. void SpatialEditor::clear() {
  3899. settings_fov->set_value(EDITOR_DEF("editors/3d/default_fov", 70.0));
  3900. settings_znear->set_value(EDITOR_DEF("editors/3d/default_z_near", 0.05));
  3901. settings_zfar->set_value(EDITOR_DEF("editors/3d/default_z_far", 1500.0));
  3902. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  3903. viewports[i]->reset();
  3904. }
  3905. VisualServer::get_singleton()->instance_set_visible(origin_instance, true);
  3906. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), true);
  3907. for (int i = 0; i < 3; ++i) {
  3908. if (grid_enable[i]) {
  3909. VisualServer::get_singleton()->instance_set_visible(grid_instance[i], true);
  3910. grid_visible[i] = true;
  3911. }
  3912. }
  3913. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  3914. viewports[i]->view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(SpatialEditorViewport::VIEW_AUDIO_LISTENER), i == 0);
  3915. viewports[i]->viewport->set_as_audio_listener(i == 0);
  3916. }
  3917. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(MENU_VIEW_GRID), true);
  3918. }
  3919. SpatialEditor::SpatialEditor(EditorNode *p_editor) {
  3920. gizmo.visible = true;
  3921. gizmo.scale = 1.0;
  3922. viewport_environment = Ref<Environment>(memnew(Environment));
  3923. undo_redo = p_editor->get_undo_redo();
  3924. VBoxContainer *vbc = this;
  3925. custom_camera = NULL;
  3926. singleton = this;
  3927. editor = p_editor;
  3928. editor_selection = editor->get_editor_selection();
  3929. editor_selection->add_editor_plugin(this);
  3930. snap_enabled = false;
  3931. snap_key_enabled = false;
  3932. tool_mode = TOOL_MODE_SELECT;
  3933. hbc_menu = memnew(HBoxContainer);
  3934. vbc->add_child(hbc_menu);
  3935. Vector<Variant> button_binds;
  3936. button_binds.resize(1);
  3937. String sct;
  3938. tool_button[TOOL_MODE_SELECT] = memnew(ToolButton);
  3939. hbc_menu->add_child(tool_button[TOOL_MODE_SELECT]);
  3940. tool_button[TOOL_MODE_SELECT]->set_toggle_mode(true);
  3941. tool_button[TOOL_MODE_SELECT]->set_flat(true);
  3942. tool_button[TOOL_MODE_SELECT]->set_pressed(true);
  3943. button_binds.write[0] = MENU_TOOL_SELECT;
  3944. tool_button[TOOL_MODE_SELECT]->connect("pressed", this, "_menu_item_pressed", button_binds);
  3945. tool_button[TOOL_MODE_SELECT]->set_tooltip(TTR("Select Mode (Q)") + "\n" + keycode_get_string(KEY_MASK_CMD) + TTR("Drag: Rotate\nAlt+Drag: Move\nAlt+RMB: Depth list selection"));
  3946. tool_button[TOOL_MODE_MOVE] = memnew(ToolButton);
  3947. hbc_menu->add_child(tool_button[TOOL_MODE_MOVE]);
  3948. tool_button[TOOL_MODE_MOVE]->set_toggle_mode(true);
  3949. tool_button[TOOL_MODE_MOVE]->set_flat(true);
  3950. button_binds.write[0] = MENU_TOOL_MOVE;
  3951. tool_button[TOOL_MODE_MOVE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  3952. tool_button[TOOL_MODE_MOVE]->set_tooltip(TTR("Move Mode (W)"));
  3953. tool_button[TOOL_MODE_ROTATE] = memnew(ToolButton);
  3954. hbc_menu->add_child(tool_button[TOOL_MODE_ROTATE]);
  3955. tool_button[TOOL_MODE_ROTATE]->set_toggle_mode(true);
  3956. tool_button[TOOL_MODE_ROTATE]->set_flat(true);
  3957. button_binds.write[0] = MENU_TOOL_ROTATE;
  3958. tool_button[TOOL_MODE_ROTATE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  3959. tool_button[TOOL_MODE_ROTATE]->set_tooltip(TTR("Rotate Mode (E)"));
  3960. tool_button[TOOL_MODE_SCALE] = memnew(ToolButton);
  3961. hbc_menu->add_child(tool_button[TOOL_MODE_SCALE]);
  3962. tool_button[TOOL_MODE_SCALE]->set_toggle_mode(true);
  3963. tool_button[TOOL_MODE_SCALE]->set_flat(true);
  3964. button_binds.write[0] = MENU_TOOL_SCALE;
  3965. tool_button[TOOL_MODE_SCALE]->connect("pressed", this, "_menu_item_pressed", button_binds);
  3966. tool_button[TOOL_MODE_SCALE]->set_tooltip(TTR("Scale Mode (R)"));
  3967. tool_button[TOOL_MODE_LIST_SELECT] = memnew(ToolButton);
  3968. hbc_menu->add_child(tool_button[TOOL_MODE_LIST_SELECT]);
  3969. tool_button[TOOL_MODE_LIST_SELECT]->set_toggle_mode(true);
  3970. tool_button[TOOL_MODE_LIST_SELECT]->set_flat(true);
  3971. button_binds.write[0] = MENU_TOOL_LIST_SELECT;
  3972. tool_button[TOOL_MODE_LIST_SELECT]->connect("pressed", this, "_menu_item_pressed", button_binds);
  3973. tool_button[TOOL_MODE_LIST_SELECT]->set_tooltip(TTR("Show a list of all objects at the position clicked\n(same as Alt+RMB in select mode)."));
  3974. tool_button[TOOL_LOCK_SELECTED] = memnew(ToolButton);
  3975. hbc_menu->add_child(tool_button[TOOL_LOCK_SELECTED]);
  3976. button_binds.write[0] = MENU_LOCK_SELECTED;
  3977. tool_button[TOOL_LOCK_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  3978. tool_button[TOOL_LOCK_SELECTED]->set_tooltip(TTR("Lock the selected object in place (can't be moved)."));
  3979. tool_button[TOOL_UNLOCK_SELECTED] = memnew(ToolButton);
  3980. hbc_menu->add_child(tool_button[TOOL_UNLOCK_SELECTED]);
  3981. button_binds.write[0] = MENU_UNLOCK_SELECTED;
  3982. tool_button[TOOL_UNLOCK_SELECTED]->connect("pressed", this, "_menu_item_pressed", button_binds);
  3983. tool_button[TOOL_UNLOCK_SELECTED]->set_tooltip(TTR("Unlock the selected object (can be moved)."));
  3984. VSeparator *vs = memnew(VSeparator);
  3985. hbc_menu->add_child(vs);
  3986. tool_option_button[TOOL_OPT_LOCAL_COORDS] = memnew(ToolButton);
  3987. hbc_menu->add_child(tool_option_button[TOOL_OPT_LOCAL_COORDS]);
  3988. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_toggle_mode(true);
  3989. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_flat(true);
  3990. button_binds.write[0] = MENU_TOOL_LOCAL_COORDS;
  3991. tool_option_button[TOOL_OPT_LOCAL_COORDS]->connect("toggled", this, "_menu_item_toggled", button_binds);
  3992. ED_SHORTCUT("spatial_editor/local_coords", TTR("Local Coords"), KEY_T);
  3993. sct = ED_GET_SHORTCUT("spatial_editor/local_coords").ptr()->get_as_text();
  3994. tool_option_button[TOOL_OPT_LOCAL_COORDS]->set_tooltip(vformat(TTR("Local Space Mode (%s)"), sct));
  3995. tool_option_button[TOOL_OPT_USE_SNAP] = memnew(ToolButton);
  3996. hbc_menu->add_child(tool_option_button[TOOL_OPT_USE_SNAP]);
  3997. tool_option_button[TOOL_OPT_USE_SNAP]->set_toggle_mode(true);
  3998. tool_option_button[TOOL_OPT_USE_SNAP]->set_flat(true);
  3999. button_binds.write[0] = MENU_TOOL_USE_SNAP;
  4000. tool_option_button[TOOL_OPT_USE_SNAP]->connect("toggled", this, "_menu_item_toggled", button_binds);
  4001. ED_SHORTCUT("spatial_editor/snap", TTR("Snap"), KEY_Y);
  4002. sct = ED_GET_SHORTCUT("spatial_editor/snap").ptr()->get_as_text();
  4003. tool_option_button[TOOL_OPT_USE_SNAP]->set_tooltip(vformat(TTR("Snap Mode (%s)"), sct));
  4004. vs = memnew(VSeparator);
  4005. hbc_menu->add_child(vs);
  4006. // Drag and drop support;
  4007. preview_node = memnew(Spatial);
  4008. preview_bounds = AABB();
  4009. ED_SHORTCUT("spatial_editor/bottom_view", TTR("Bottom View"), KEY_MASK_ALT + KEY_KP_7);
  4010. ED_SHORTCUT("spatial_editor/top_view", TTR("Top View"), KEY_KP_7);
  4011. ED_SHORTCUT("spatial_editor/rear_view", TTR("Rear View"), KEY_MASK_ALT + KEY_KP_1);
  4012. ED_SHORTCUT("spatial_editor/front_view", TTR("Front View"), KEY_KP_1);
  4013. ED_SHORTCUT("spatial_editor/left_view", TTR("Left View"), KEY_MASK_ALT + KEY_KP_3);
  4014. ED_SHORTCUT("spatial_editor/right_view", TTR("Right View"), KEY_KP_3);
  4015. ED_SHORTCUT("spatial_editor/switch_perspective_orthogonal", TTR("Switch Perspective/Orthogonal view"), KEY_KP_5);
  4016. ED_SHORTCUT("spatial_editor/insert_anim_key", TTR("Insert Animation Key"), KEY_K);
  4017. ED_SHORTCUT("spatial_editor/focus_origin", TTR("Focus Origin"), KEY_O);
  4018. ED_SHORTCUT("spatial_editor/focus_selection", TTR("Focus Selection"), KEY_F);
  4019. ED_SHORTCUT("spatial_editor/align_selection_with_view", TTR("Align Selection With View"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_F);
  4020. ED_SHORTCUT("spatial_editor/tool_select", TTR("Tool Select"), KEY_Q);
  4021. ED_SHORTCUT("spatial_editor/tool_move", TTR("Tool Move"), KEY_W);
  4022. ED_SHORTCUT("spatial_editor/tool_rotate", TTR("Tool Rotate"), KEY_E);
  4023. ED_SHORTCUT("spatial_editor/tool_scale", TTR("Tool Scale"), KEY_R);
  4024. ED_SHORTCUT("spatial_editor/snap_to_floor", TTR("Snap To Floor"), KEY_PAGEDOWN);
  4025. ED_SHORTCUT("spatial_editor/freelook_toggle", TTR("Toggle Freelook"), KEY_MASK_SHIFT + KEY_F);
  4026. PopupMenu *p;
  4027. transform_menu = memnew(MenuButton);
  4028. transform_menu->set_text(TTR("Transform"));
  4029. hbc_menu->add_child(transform_menu);
  4030. p = transform_menu->get_popup();
  4031. p->add_shortcut(ED_SHORTCUT("spatial_editor/snap_to_floor", TTR("Snap object to floor")), MENU_SNAP_TO_FLOOR);
  4032. p->add_shortcut(ED_SHORTCUT("spatial_editor/configure_snap", TTR("Configure Snap...")), MENU_TRANSFORM_CONFIGURE_SNAP);
  4033. p->add_separator();
  4034. p->add_shortcut(ED_SHORTCUT("spatial_editor/transform_dialog", TTR("Transform Dialog...")), MENU_TRANSFORM_DIALOG);
  4035. p->connect("id_pressed", this, "_menu_item_pressed");
  4036. view_menu = memnew(MenuButton);
  4037. view_menu->set_text(TTR("View"));
  4038. view_menu->set_position(Point2(212, 0));
  4039. hbc_menu->add_child(view_menu);
  4040. p = view_menu->get_popup();
  4041. accept = memnew(AcceptDialog);
  4042. editor->get_gui_base()->add_child(accept);
  4043. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/1_viewport", TTR("1 Viewport"), KEY_MASK_CMD + KEY_1), MENU_VIEW_USE_1_VIEWPORT);
  4044. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports", TTR("2 Viewports"), KEY_MASK_CMD + KEY_2), MENU_VIEW_USE_2_VIEWPORTS);
  4045. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/2_viewports_alt", TTR("2 Viewports (Alt)"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_2), MENU_VIEW_USE_2_VIEWPORTS_ALT);
  4046. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports", TTR("3 Viewports"), KEY_MASK_CMD + KEY_3), MENU_VIEW_USE_3_VIEWPORTS);
  4047. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/3_viewports_alt", TTR("3 Viewports (Alt)"), KEY_MASK_ALT + KEY_MASK_CMD + KEY_3), MENU_VIEW_USE_3_VIEWPORTS_ALT);
  4048. p->add_radio_check_shortcut(ED_SHORTCUT("spatial_editor/4_viewports", TTR("4 Viewports"), KEY_MASK_CMD + KEY_4), MENU_VIEW_USE_4_VIEWPORTS);
  4049. p->add_separator();
  4050. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_origin", TTR("View Origin")), MENU_VIEW_ORIGIN);
  4051. p->add_check_shortcut(ED_SHORTCUT("spatial_editor/view_grid", TTR("View Grid")), MENU_VIEW_GRID);
  4052. p->add_separator();
  4053. p->add_shortcut(ED_SHORTCUT("spatial_editor/settings", TTR("Settings")), MENU_VIEW_CAMERA_SETTINGS);
  4054. p->add_separator();
  4055. p->add_multistate_item(TTR("Skeleton Gizmo visibility"), 3, 1, MENU_VISIBILITY_SKELETON);
  4056. p->set_item_checked(p->get_item_index(MENU_VIEW_ORIGIN), true);
  4057. p->set_item_checked(p->get_item_index(MENU_VIEW_GRID), true);
  4058. p->connect("id_pressed", this, "_menu_item_pressed");
  4059. /* REST OF MENU */
  4060. palette_split = memnew(HSplitContainer);
  4061. palette_split->set_v_size_flags(SIZE_EXPAND_FILL);
  4062. vbc->add_child(palette_split);
  4063. shader_split = memnew(VSplitContainer);
  4064. shader_split->set_h_size_flags(SIZE_EXPAND_FILL);
  4065. palette_split->add_child(shader_split);
  4066. viewport_base = memnew(SpatialEditorViewportContainer);
  4067. shader_split->add_child(viewport_base);
  4068. viewport_base->set_v_size_flags(SIZE_EXPAND_FILL);
  4069. for (uint32_t i = 0; i < VIEWPORTS_COUNT; i++) {
  4070. viewports[i] = memnew(SpatialEditorViewport(this, editor, i));
  4071. viewports[i]->connect("toggle_maximize_view", this, "_toggle_maximize_view");
  4072. viewports[i]->assign_pending_data_pointers(preview_node, &preview_bounds, accept);
  4073. viewport_base->add_child(viewports[i]);
  4074. }
  4075. /* SNAP DIALOG */
  4076. snap_dialog = memnew(ConfirmationDialog);
  4077. snap_dialog->set_title(TTR("Snap Settings"));
  4078. add_child(snap_dialog);
  4079. VBoxContainer *snap_dialog_vbc = memnew(VBoxContainer);
  4080. snap_dialog->add_child(snap_dialog_vbc);
  4081. snap_translate = memnew(LineEdit);
  4082. snap_translate->set_text("1");
  4083. snap_dialog_vbc->add_margin_child(TTR("Translate Snap:"), snap_translate);
  4084. snap_rotate = memnew(LineEdit);
  4085. snap_rotate->set_text("5");
  4086. snap_dialog_vbc->add_margin_child(TTR("Rotate Snap (deg.):"), snap_rotate);
  4087. snap_scale = memnew(LineEdit);
  4088. snap_scale->set_text("5");
  4089. snap_dialog_vbc->add_margin_child(TTR("Scale Snap (%):"), snap_scale);
  4090. /* SETTINGS DIALOG */
  4091. settings_dialog = memnew(ConfirmationDialog);
  4092. settings_dialog->set_title(TTR("Viewport Settings"));
  4093. add_child(settings_dialog);
  4094. settings_vbc = memnew(VBoxContainer);
  4095. settings_vbc->set_custom_minimum_size(Size2(200, 0) * EDSCALE);
  4096. settings_dialog->add_child(settings_vbc);
  4097. settings_fov = memnew(SpinBox);
  4098. settings_fov->set_max(MAX_FOV);
  4099. settings_fov->set_min(MIN_FOV);
  4100. settings_fov->set_step(0.01);
  4101. settings_fov->set_value(EDITOR_DEF("editors/3d/default_fov", 70.0));
  4102. settings_vbc->add_margin_child(TTR("Perspective FOV (deg.):"), settings_fov);
  4103. settings_znear = memnew(SpinBox);
  4104. settings_znear->set_max(MAX_Z);
  4105. settings_znear->set_min(MIN_Z);
  4106. settings_znear->set_step(0.01);
  4107. settings_znear->set_value(EDITOR_DEF("editors/3d/default_z_near", 0.05));
  4108. settings_vbc->add_margin_child(TTR("View Z-Near:"), settings_znear);
  4109. settings_zfar = memnew(SpinBox);
  4110. settings_zfar->set_max(MAX_Z);
  4111. settings_zfar->set_min(MIN_Z);
  4112. settings_zfar->set_step(0.01);
  4113. settings_zfar->set_value(EDITOR_DEF("editors/3d/default_z_far", 1500));
  4114. settings_vbc->add_margin_child(TTR("View Z-Far:"), settings_zfar);
  4115. /* XFORM DIALOG */
  4116. xform_dialog = memnew(ConfirmationDialog);
  4117. xform_dialog->set_title(TTR("Transform Change"));
  4118. add_child(xform_dialog);
  4119. VBoxContainer *xform_vbc = memnew(VBoxContainer);
  4120. xform_dialog->add_child(xform_vbc);
  4121. Label *l = memnew(Label);
  4122. l->set_text(TTR("Translate:"));
  4123. xform_vbc->add_child(l);
  4124. HBoxContainer *xform_hbc = memnew(HBoxContainer);
  4125. xform_vbc->add_child(xform_hbc);
  4126. for (int i = 0; i < 3; i++) {
  4127. xform_translate[i] = memnew(LineEdit);
  4128. xform_translate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  4129. xform_hbc->add_child(xform_translate[i]);
  4130. }
  4131. l = memnew(Label);
  4132. l->set_text(TTR("Rotate (deg.):"));
  4133. xform_vbc->add_child(l);
  4134. xform_hbc = memnew(HBoxContainer);
  4135. xform_vbc->add_child(xform_hbc);
  4136. for (int i = 0; i < 3; i++) {
  4137. xform_rotate[i] = memnew(LineEdit);
  4138. xform_rotate[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  4139. xform_hbc->add_child(xform_rotate[i]);
  4140. }
  4141. l = memnew(Label);
  4142. l->set_text(TTR("Scale (ratio):"));
  4143. xform_vbc->add_child(l);
  4144. xform_hbc = memnew(HBoxContainer);
  4145. xform_vbc->add_child(xform_hbc);
  4146. for (int i = 0; i < 3; i++) {
  4147. xform_scale[i] = memnew(LineEdit);
  4148. xform_scale[i]->set_h_size_flags(SIZE_EXPAND_FILL);
  4149. xform_hbc->add_child(xform_scale[i]);
  4150. }
  4151. l = memnew(Label);
  4152. l->set_text(TTR("Transform Type"));
  4153. xform_vbc->add_child(l);
  4154. xform_type = memnew(OptionButton);
  4155. xform_type->set_h_size_flags(SIZE_EXPAND_FILL);
  4156. xform_type->add_item(TTR("Pre"));
  4157. xform_type->add_item(TTR("Post"));
  4158. xform_vbc->add_child(xform_type);
  4159. xform_dialog->connect("confirmed", this, "_xform_dialog_action");
  4160. scenario_debug = VisualServer::SCENARIO_DEBUG_DISABLED;
  4161. selected = NULL;
  4162. set_process_unhandled_key_input(true);
  4163. add_to_group("_spatial_editor_group");
  4164. EDITOR_DEF("editors/3d/manipulator_gizmo_size", 80);
  4165. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT, "editors/3d/manipulator_gizmo_size", PROPERTY_HINT_RANGE, "16,1024,1"));
  4166. EDITOR_DEF("editors/3d/manipulator_gizmo_opacity", 0.2);
  4167. over_gizmo_handle = -1;
  4168. }
  4169. SpatialEditor::~SpatialEditor() {
  4170. memdelete(preview_node);
  4171. }
  4172. void SpatialEditorPlugin::make_visible(bool p_visible) {
  4173. if (p_visible) {
  4174. spatial_editor->show();
  4175. spatial_editor->set_process(true);
  4176. spatial_editor->grab_focus();
  4177. } else {
  4178. spatial_editor->hide();
  4179. spatial_editor->set_process(false);
  4180. }
  4181. }
  4182. void SpatialEditorPlugin::edit(Object *p_object) {
  4183. spatial_editor->edit(Object::cast_to<Spatial>(p_object));
  4184. }
  4185. bool SpatialEditorPlugin::handles(Object *p_object) const {
  4186. return p_object->is_class("Spatial");
  4187. }
  4188. Dictionary SpatialEditorPlugin::get_state() const {
  4189. return spatial_editor->get_state();
  4190. }
  4191. void SpatialEditorPlugin::set_state(const Dictionary &p_state) {
  4192. spatial_editor->set_state(p_state);
  4193. }
  4194. void SpatialEditor::snap_cursor_to_plane(const Plane &p_plane) {
  4195. //cursor.pos=p_plane.project(cursor.pos);
  4196. }
  4197. Vector3 SpatialEditor::snap_point(Vector3 p_target, Vector3 p_start) const {
  4198. if (is_snap_enabled()) {
  4199. p_target.x = Math::snap_scalar(0.0, get_translate_snap(), p_target.x);
  4200. p_target.y = Math::snap_scalar(0.0, get_translate_snap(), p_target.y);
  4201. p_target.z = Math::snap_scalar(0.0, get_translate_snap(), p_target.z);
  4202. }
  4203. return p_target;
  4204. }
  4205. void SpatialEditorPlugin::_bind_methods() {
  4206. ClassDB::bind_method("snap_cursor_to_plane", &SpatialEditorPlugin::snap_cursor_to_plane);
  4207. }
  4208. void SpatialEditorPlugin::snap_cursor_to_plane(const Plane &p_plane) {
  4209. spatial_editor->snap_cursor_to_plane(p_plane);
  4210. }
  4211. SpatialEditorPlugin::SpatialEditorPlugin(EditorNode *p_node) {
  4212. editor = p_node;
  4213. spatial_editor = memnew(SpatialEditor(p_node));
  4214. spatial_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
  4215. editor->get_viewport()->add_child(spatial_editor);
  4216. spatial_editor->hide();
  4217. spatial_editor->connect("transform_key_request", editor, "_transform_keyed");
  4218. }
  4219. SpatialEditorPlugin::~SpatialEditorPlugin() {
  4220. }