spatial_editor_plugin.cpp 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948
  1. /*************************************************************************/
  2. /* spatial_editor_plugin.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "spatial_editor_plugin.h"
  30. #include "print_string.h"
  31. #include "os/keyboard.h"
  32. #include "scene/3d/visual_instance.h"
  33. #include "scene/3d/camera.h"
  34. #include "camera_matrix.h"
  35. #include "sort.h"
  36. #include "tools/editor/editor_node.h"
  37. #include "tools/editor/editor_settings.h"
  38. #include "scene/resources/surface_tool.h"
  39. #include "tools/editor/spatial_editor_gizmos.h"
  40. #include "globals.h"
  41. #define DISTANCE_DEFAULT 4
  42. #define GIZMO_ARROW_SIZE 0.3
  43. #define GIZMO_RING_HALF_WIDTH 0.1
  44. //#define GIZMO_SCALE_DEFAULT 0.28
  45. #define GIZMO_SCALE_DEFAULT 0.15
  46. //void SpatialEditorViewport::_update_camera();
  47. String SpatialEditorGizmo::get_handle_name(int p_idx) const {
  48. return "";
  49. }
  50. Variant SpatialEditorGizmo::get_handle_value(int p_idx) const{
  51. return Variant();
  52. }
  53. void SpatialEditorGizmo::set_handle(int p_idx,Camera *p_camera, const Point2& p_point) {
  54. }
  55. void SpatialEditorGizmo::commit_handle(int p_idx,const Variant& p_restore,bool p_cancel){
  56. }
  57. bool SpatialEditorGizmo::intersect_frustum(const Camera *p_camera,const Vector<Plane> &p_frustum) {
  58. return false;
  59. }
  60. bool SpatialEditorGizmo::intersect_ray(const Camera *p_camera, const Point2 &p_point, Vector3& r_pos, Vector3& r_normal,int *r_gizmo_handle,bool p_sec_first) {
  61. return false;
  62. }
  63. SpatialEditorGizmo::SpatialEditorGizmo(){
  64. selected=false;
  65. }
  66. int SpatialEditorViewport::get_selected_count() const {
  67. Map<Node*,Object*> &selection = editor_selection->get_selection();
  68. int count=0;
  69. for(Map<Node*,Object*>::Element *E=selection.front();E;E=E->next()) {
  70. Spatial *sp = E->key()->cast_to<Spatial>();
  71. if (!sp)
  72. continue;
  73. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  74. if (!se)
  75. continue;
  76. count++;
  77. }
  78. return count;
  79. }
  80. float SpatialEditorViewport::get_znear() const {
  81. float val = spatial_editor->get_znear();
  82. if (val<0.001)
  83. val=0.001;
  84. return val;
  85. }
  86. float SpatialEditorViewport::get_zfar() const{
  87. float val = spatial_editor->get_zfar();
  88. if (val<0.001)
  89. val=0.001;
  90. return val;
  91. }
  92. float SpatialEditorViewport::get_fov() const{
  93. float val = spatial_editor->get_fov();
  94. if (val<0.001)
  95. val=0.001;
  96. if (val>89)
  97. val=89;
  98. return val;
  99. }
  100. Transform SpatialEditorViewport::_get_camera_transform() const {
  101. return camera->get_global_transform();
  102. }
  103. Vector3 SpatialEditorViewport::_get_camera_pos() const {
  104. return _get_camera_transform().origin;
  105. }
  106. Point2 SpatialEditorViewport::_point_to_screen(const Vector3& p_point) {
  107. return camera->unproject_position(p_point);
  108. }
  109. Vector3 SpatialEditorViewport::_get_ray_pos(const Vector2& p_pos) const {
  110. return camera->project_ray_origin(p_pos);
  111. }
  112. Vector3 SpatialEditorViewport::_get_camera_normal() const {
  113. return -_get_camera_transform().basis.get_axis(2);
  114. }
  115. Vector3 SpatialEditorViewport::_get_ray(const Vector2& p_pos) {
  116. return camera->project_ray_normal(p_pos);
  117. }
  118. /*
  119. void SpatialEditorViewport::_clear_id(Spatial *p_node) {
  120. editor_selection->remove_node(p_node);
  121. }
  122. */
  123. void SpatialEditorViewport::_clear_selected() {
  124. editor_selection->clear();
  125. }
  126. void SpatialEditorViewport::_select_clicked(bool p_append,bool p_single) {
  127. if (!clicked)
  128. return;
  129. Object *obj = ObjectDB::get_instance(clicked);
  130. if (!obj)
  131. return;
  132. Spatial *sp = obj->cast_to<Spatial>();
  133. if (!sp)
  134. return;
  135. _select(sp, clicked_wants_append,true);
  136. }
  137. void SpatialEditorViewport::_select(Spatial *p_node, bool p_append,bool p_single) {
  138. if (!p_append) {
  139. // should not modify the selection..
  140. editor_selection->clear();
  141. editor_selection->add_node(p_node);
  142. } else {
  143. if (editor_selection->is_selected(p_node) && p_single) {
  144. //erase
  145. editor_selection->remove_node(p_node);
  146. } else {
  147. editor_selection->add_node(p_node);
  148. }
  149. }
  150. }
  151. struct _RayResult {
  152. Spatial* item;
  153. float depth;
  154. int handle;
  155. _FORCE_INLINE_ bool operator<(const _RayResult& p_rr) const { return depth<p_rr.depth; }
  156. };
  157. ObjectID SpatialEditorViewport::_select_ray(const Point2& p_pos, bool p_append,bool &r_includes_current,int *r_gizmo_handle,bool p_alt_select) {
  158. if (r_gizmo_handle)
  159. *r_gizmo_handle=-1;
  160. Vector3 ray=_get_ray(p_pos);
  161. Vector3 pos=_get_ray_pos(p_pos);
  162. Vector<RID> instances=VisualServer::get_singleton()->instances_cull_ray(pos,ray,get_scene()->get_root()->get_world()->get_scenario() );
  163. Set<Ref<SpatialEditorGizmo> > found_gizmos;
  164. //uint32_t closest=0;
  165. // float closest_dist=0;
  166. r_includes_current=false;
  167. List<_RayResult> results;
  168. Vector3 cn=_get_camera_normal();
  169. Plane cplane(pos,cn.normalized());
  170. float min_d=1e20;
  171. for (int i=0;i<instances.size();i++) {
  172. uint32_t id=VisualServer::get_singleton()->instance_get_object_instance_ID(instances[i]);
  173. Object *obj=ObjectDB::get_instance(id);
  174. if (!obj)
  175. continue;
  176. Spatial *spat=obj->cast_to<Spatial>();
  177. if (!spat)
  178. continue;
  179. Ref<SpatialEditorGizmo> seg = spat->get_gizmo();
  180. if (!seg.is_valid())
  181. continue;
  182. if (found_gizmos.has(seg))
  183. continue;
  184. found_gizmos.insert(seg);
  185. Vector3 point;
  186. Vector3 normal;
  187. int handle=-1;
  188. bool inters = seg->intersect_ray(camera,p_pos,point,normal,NULL,p_alt_select);
  189. if (!inters)
  190. continue;
  191. float dist = pos.distance_to(point);
  192. if (dist<0)
  193. continue;
  194. if (editor_selection->is_selected(spat))
  195. r_includes_current=true;
  196. _RayResult res;
  197. res.item=spat;
  198. res.depth=dist;
  199. res.handle=handle;
  200. results.push_back(res);
  201. }
  202. if (results.empty())
  203. return 0;
  204. results.sort();
  205. Spatial *s=NULL;
  206. if (!r_includes_current || results.size()==1 || (r_gizmo_handle && results.front()->get().handle>=0)) {
  207. //return the nearest one
  208. s = results.front()->get().item;
  209. if (r_gizmo_handle)
  210. *r_gizmo_handle=results.front()->get().handle;
  211. } else {
  212. //returns the next one from a curent selection
  213. List<_RayResult>::Element *E=results.front();
  214. List<_RayResult>::Element *S=NULL;
  215. while(true) {
  216. //very strange loop algorithm that complies with object selection standards (tm).
  217. if (S==E) {
  218. //went all around and anothing was found
  219. //since can't rotate the selection
  220. //just return the first one
  221. s=results.front()->get().item;
  222. break;
  223. }
  224. if (!S && editor_selection->is_selected(E->get().item)) {
  225. //found an item currently in the selection,
  226. //so start from this one
  227. S=E;
  228. }
  229. if (S && !editor_selection->is_selected(E->get().item)) {
  230. // free item after a selected item, this one is desired.
  231. s=E->get().item;
  232. break;
  233. }
  234. E=E->next();
  235. if (!E) {
  236. if (!S) {
  237. //did a loop but nothing was selected, select first
  238. s=results.front()->get().item;
  239. break;
  240. }
  241. E=results.front();
  242. }
  243. }
  244. }
  245. if (!s)
  246. return 0;
  247. return s->get_instance_ID();
  248. }
  249. Vector3 SpatialEditorViewport::_get_screen_to_space(const Vector3& p_pos) {
  250. CameraMatrix cm;
  251. cm.set_perspective(get_fov(),get_size().get_aspect(),get_znear(),get_zfar());
  252. float screen_w,screen_h;
  253. cm.get_viewport_size(screen_w,screen_h);
  254. Transform camera_transform;
  255. camera_transform.translate( cursor.pos );
  256. camera_transform.basis.rotate(Vector3(0,1,0),cursor.y_rot);
  257. camera_transform.basis.rotate(Vector3(1,0,0),cursor.x_rot);
  258. camera_transform.translate(0,0,cursor.distance);
  259. return camera_transform.xform(Vector3( ((p_pos.x/get_size().width)*2.0-1.0)*screen_w, ((1.0-(p_pos.y/get_size().height))*2.0-1.0)*screen_h,-get_znear()));
  260. }
  261. void SpatialEditorViewport::_select_region() {
  262. if (cursor.region_begin==cursor.region_end)
  263. return; //nothing really
  264. Vector3 box[4]={
  265. Vector3(
  266. MIN( cursor.region_begin.x, cursor.region_end.x),
  267. MIN( cursor.region_begin.y, cursor.region_end.y),
  268. 0
  269. ),
  270. Vector3(
  271. MAX( cursor.region_begin.x, cursor.region_end.x),
  272. MIN( cursor.region_begin.y, cursor.region_end.y),
  273. 0
  274. ),
  275. Vector3(
  276. MAX( cursor.region_begin.x, cursor.region_end.x),
  277. MAX( cursor.region_begin.y, cursor.region_end.y),
  278. 0
  279. ),
  280. Vector3(
  281. MIN( cursor.region_begin.x, cursor.region_end.x),
  282. MAX( cursor.region_begin.y, cursor.region_end.y),
  283. 0
  284. )
  285. };
  286. Vector<Plane> frustum;
  287. Vector3 cam_pos=_get_camera_pos();
  288. Set<Ref<SpatialEditorGizmo> > found_gizmos;
  289. for(int i=0;i<4;i++) {
  290. Vector3 a=_get_screen_to_space(box[i]);
  291. Vector3 b=_get_screen_to_space(box[(i+1)%4]);
  292. frustum.push_back( Plane(a,b,cam_pos) );
  293. }
  294. Plane near( cam_pos, -_get_camera_normal() );
  295. near.d-=get_znear();
  296. frustum.push_back( near );
  297. Plane far=-near;
  298. far.d+=500.0;
  299. frustum.push_back( far );
  300. Vector<RID> instances=VisualServer::get_singleton()->instances_cull_convex(frustum,get_scene()->get_root()->get_world()->get_scenario());
  301. for (int i=0;i<instances.size();i++) {
  302. uint32_t id=VisualServer::get_singleton()->instance_get_object_instance_ID(instances[i]);
  303. Object *obj=ObjectDB::get_instance(id);
  304. if (!obj)
  305. continue;
  306. Spatial *sp = obj->cast_to<Spatial>();
  307. if (!sp)
  308. continue;
  309. Ref<SpatialEditorGizmo> seg = sp->get_gizmo();
  310. if (!seg.is_valid())
  311. continue;
  312. if (found_gizmos.has(seg))
  313. continue;
  314. if (seg->intersect_frustum(camera,frustum))
  315. _select(sp,true,false);
  316. }
  317. }
  318. void SpatialEditorViewport::_compute_edit(const Point2& p_point) {
  319. _edit.click_ray=_get_ray( Vector2( p_point.x, p_point.y ) );
  320. _edit.click_ray_pos=_get_ray_pos( Vector2( p_point.x, p_point.y ) );
  321. _edit.plane=TRANSFORM_VIEW;
  322. spatial_editor->update_transform_gizmo();
  323. _edit.center=spatial_editor->get_gizmo_transform().origin;
  324. List<Node*> &selection = editor_selection->get_selected_node_list();
  325. // Vector3 center;
  326. // int nc=0;
  327. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  328. Spatial *sp = E->get()->cast_to<Spatial>();
  329. if (!sp)
  330. continue;
  331. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  332. if (!se)
  333. continue;
  334. se->original=se->sp->get_global_transform();
  335. // center+=se->original.origin;
  336. // nc++;
  337. }
  338. // if (nc)
  339. // _edit.center=center/float(nc);
  340. }
  341. static int _get_key_modifier(const String& p_property) {
  342. switch(EditorSettings::get_singleton()->get(p_property).operator int()) {
  343. case 0: return 0;
  344. case 1: return KEY_SHIFT;
  345. case 2: return KEY_ALT;
  346. case 3: return KEY_META;
  347. case 4: return KEY_CONTROL;
  348. }
  349. return 0;
  350. }
  351. SpatialEditorViewport::NavigationScheme SpatialEditorViewport::_get_navigation_schema(const String& p_property) {
  352. switch(EditorSettings::get_singleton()->get(p_property).operator int()) {
  353. case 0: return NAVIGATION_GODOT;
  354. case 1: return NAVIGATION_MAYA;
  355. case 2: return NAVIGATION_MODO;
  356. }
  357. return NAVIGATION_GODOT;
  358. }
  359. bool SpatialEditorViewport::_gizmo_select(const Vector2& p_screenpos,bool p_hilite_only) {
  360. if (!spatial_editor->is_gizmo_visible())
  361. return false;
  362. if (get_selected_count()==0) {
  363. if (p_hilite_only)
  364. spatial_editor->select_gizmo_hilight_axis(-1);
  365. return false;
  366. }
  367. Vector3 ray_pos=_get_ray_pos( Vector2( p_screenpos.x, p_screenpos.y ) );
  368. Vector3 ray=_get_ray( Vector2( p_screenpos.x, p_screenpos.y ) );
  369. Vector3 cn=_get_camera_normal();
  370. Plane cplane(ray_pos,cn.normalized());
  371. Transform gt = spatial_editor->get_gizmo_transform();
  372. float gs=gizmo_scale;
  373. /*
  374. if (orthogonal) {
  375. gs= cursor.distance/surface->get_size().get_aspect();
  376. } else {
  377. gs = cplane.distance_to(gt.origin);
  378. }
  379. gs*=GIZMO_SCALE_DEFAULT;
  380. */
  381. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_MOVE) {
  382. int col_axis=-1;
  383. float col_d=1e20;
  384. for(int i=0;i<3;i++) {
  385. Vector3 grabber_pos = gt.origin+gt.basis.get_axis(i)*gs;
  386. float grabber_radius = gs*GIZMO_ARROW_SIZE;
  387. Vector3 r;
  388. if (Geometry::segment_intersects_sphere(ray_pos,ray_pos+ray*10000.0,grabber_pos,grabber_radius,&r)) {
  389. float d = r.distance_to(ray_pos);
  390. if (d<col_d) {
  391. col_d=d;
  392. col_axis=i;
  393. }
  394. }
  395. }
  396. if (col_axis!=-1) {
  397. if (p_hilite_only) {
  398. spatial_editor->select_gizmo_hilight_axis(col_axis);
  399. } else {
  400. //handle rotate
  401. _edit.mode=TRANSFORM_TRANSLATE;
  402. _compute_edit(Point2(p_screenpos.x,p_screenpos.y));
  403. _edit.plane=TransformPlane(TRANSFORM_X_AXIS+col_axis);
  404. }
  405. return true;
  406. }
  407. }
  408. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_ROTATE) {
  409. int col_axis=-1;
  410. float col_d=1e20;
  411. for(int i=0;i<3;i++) {
  412. Plane plane(gt.origin,gt.basis.get_axis(i).normalized());
  413. Vector3 r;
  414. if (!plane.intersects_ray(ray_pos,ray,&r))
  415. continue;
  416. float dist = r.distance_to(gt.origin);
  417. if (dist > gs*(1-GIZMO_RING_HALF_WIDTH) && dist < gs *(1+GIZMO_RING_HALF_WIDTH)) {
  418. float d = ray_pos.distance_to(r);
  419. if (d<col_d) {
  420. col_d=d;
  421. col_axis=i;
  422. }
  423. }
  424. }
  425. if (col_axis!=-1) {
  426. if (p_hilite_only) {
  427. spatial_editor->select_gizmo_hilight_axis(col_axis+3);
  428. } else {
  429. //handle rotate
  430. _edit.mode=TRANSFORM_ROTATE;
  431. _compute_edit(Point2(p_screenpos.x,p_screenpos.y));
  432. _edit.plane=TransformPlane(TRANSFORM_X_AXIS+col_axis);
  433. }
  434. return true;
  435. }
  436. }
  437. if (p_hilite_only)
  438. spatial_editor->select_gizmo_hilight_axis(-1);
  439. return false;
  440. }
  441. void SpatialEditorViewport::_smouseenter() {
  442. surface->grab_focus();
  443. }
  444. void SpatialEditorViewport::_sinput(const InputEvent &p_event) {
  445. if (previewing)
  446. return; //do NONE
  447. {
  448. EditorNode *en = editor;
  449. EditorPlugin *over_plugin = en->get_editor_plugin_over();
  450. if (over_plugin) {
  451. bool discard = over_plugin->forward_spatial_input_event(camera,p_event);
  452. if (discard)
  453. return;
  454. }
  455. }
  456. switch(p_event.type) {
  457. case InputEvent::MOUSE_BUTTON: {
  458. const InputEventMouseButton &b=p_event.mouse_button;
  459. switch(b.button_index) {
  460. case BUTTON_WHEEL_UP: {
  461. cursor.distance/=1.08;
  462. if (cursor.distance<0.001)
  463. cursor.distance=0.001;
  464. } break;
  465. case BUTTON_WHEEL_DOWN: {
  466. if (cursor.distance<0.001)
  467. cursor.distance=0.001;
  468. cursor.distance*=1.08;
  469. } break;
  470. case BUTTON_RIGHT: {
  471. if (b.pressed && _edit.gizmo.is_valid()) {
  472. //restore
  473. _edit.gizmo->commit_handle(_edit.gizmo_handle,_edit.gizmo_initial_value,true);
  474. _edit.gizmo=Ref<SpatialEditorGizmo>();
  475. }
  476. if (_edit.mode==TRANSFORM_NONE && b.pressed) {
  477. Plane cursor_plane(cursor.cursor_pos,_get_camera_normal());
  478. Vector3 ray_origin = _get_ray_pos(Vector2(b.x,b.y));
  479. Vector3 ray_dir = _get_ray(Vector2(b.x,b.y));
  480. //gizmo modify
  481. if (b.mod.control) {
  482. Vector<RID> instances=VisualServer::get_singleton()->instances_cull_ray(ray_origin,ray_dir,get_scene()->get_root()->get_world()->get_scenario() );
  483. Plane p(ray_origin,_get_camera_normal());
  484. float min_d=1e10;
  485. bool found=false;
  486. for (int i=0;i<instances.size();i++) {
  487. uint32_t id=VisualServer::get_singleton()->instance_get_object_instance_ID(instances[i]);
  488. Object *obj=ObjectDB::get_instance(id);
  489. if (!obj)
  490. continue;
  491. VisualInstance *vi=obj->cast_to<VisualInstance>();
  492. if (!vi)
  493. continue;
  494. //optimize by checking AABB (although should pre sort by distance)
  495. AABB aabb = vi->get_global_transform().xform(vi->get_aabb());
  496. if (p.distance_to(aabb.get_support(-ray_dir))>min_d)
  497. continue;
  498. DVector<Face3> faces = vi->get_faces(VisualInstance::FACES_SOLID);
  499. int c = faces.size();
  500. if (c>0) {
  501. DVector<Face3>::Read r = faces.read();
  502. for(int j=0;j<c;j++) {
  503. Vector3 inters;
  504. if (r[j].intersects_ray(ray_origin,ray_dir,&inters)) {
  505. float d = p.distance_to(inters);
  506. if (d<0)
  507. continue;
  508. if (d<min_d) {
  509. min_d=d;
  510. found=true;
  511. }
  512. }
  513. }
  514. }
  515. }
  516. if (found) {
  517. //cursor.cursor_pos=ray_origin+ray_dir*min_d;
  518. //VisualServer::get_singleton()->instance_set_transform(cursor_instance,Transform(Matrix3(),cursor.cursor_pos));
  519. }
  520. } else {
  521. Vector3 new_pos;
  522. if (cursor_plane.intersects_ray(ray_origin,ray_dir,&new_pos)) {
  523. //cursor.cursor_pos=new_pos;
  524. //VisualServer::get_singleton()->instance_set_transform(cursor_instance,Transform(Matrix3(),cursor.cursor_pos));
  525. }
  526. }
  527. }
  528. if (_edit.mode!=TRANSFORM_NONE && b.pressed) {
  529. //cancel motion
  530. _edit.mode=TRANSFORM_NONE;
  531. //_validate_selection();
  532. List<Node*> &selection = editor_selection->get_selected_node_list();
  533. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  534. Spatial *sp = E->get()->cast_to<Spatial>();
  535. if (!sp)
  536. continue;
  537. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  538. if (!se)
  539. continue;
  540. sp->set_global_transform( se->original );
  541. }
  542. surface->update();
  543. //VisualServer::get_singleton()->poly_clear(indicators);
  544. set_message("Transform Aborted.",3);
  545. }
  546. } break;
  547. case BUTTON_MIDDLE: {
  548. if (b.pressed && _edit.mode!=TRANSFORM_NONE) {
  549. switch(_edit.plane ) {
  550. case TRANSFORM_VIEW: {
  551. _edit.plane=TRANSFORM_X_AXIS;
  552. set_message("View Plane Transform.",2);
  553. } break;
  554. case TRANSFORM_X_AXIS: {
  555. _edit.plane=TRANSFORM_Y_AXIS;
  556. set_message("X-Axis Transform.",2);
  557. } break;
  558. case TRANSFORM_Y_AXIS: {
  559. _edit.plane=TRANSFORM_Z_AXIS;
  560. set_message("Y-Axis Transform.",2);
  561. } break;
  562. case TRANSFORM_Z_AXIS: {
  563. _edit.plane=TRANSFORM_VIEW;
  564. set_message("Z-Axis Transform.",2);
  565. } break;
  566. }
  567. }
  568. } break;
  569. case BUTTON_LEFT: {
  570. if (b.pressed) {
  571. NavigationScheme nav_scheme = _get_navigation_schema("3d_editor/navigation_scheme");
  572. if ( (nav_scheme==NAVIGATION_MAYA || nav_scheme==NAVIGATION_MODO) && b.mod.alt) {
  573. break;
  574. }
  575. _edit.mouse_pos=Point2(b.x,b.y);
  576. _edit.snap=false;
  577. _edit.mode=TRANSFORM_NONE;
  578. //gizmo has priority over everything
  579. bool can_select_gizmos=true;
  580. {
  581. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  582. can_select_gizmos = view_menu->get_popup()->is_item_checked( idx );
  583. }
  584. if (can_select_gizmos && spatial_editor->get_selected()) {
  585. Ref<SpatialEditorGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  586. if (seg.is_valid()) {
  587. int handle=-1;
  588. Vector3 point;
  589. Vector3 normal;
  590. bool inters = seg->intersect_ray(camera,_edit.mouse_pos,point,normal,&handle,b.mod.shift);
  591. if (inters && handle!=-1) {
  592. _edit.gizmo=seg;
  593. _edit.gizmo_handle=handle;
  594. //_edit.gizmo_initial_pos=seg->get_handle_pos(gizmo_handle);
  595. _edit.gizmo_initial_value=seg->get_handle_value(handle);
  596. break;
  597. }
  598. }
  599. }
  600. if (_gizmo_select(_edit.mouse_pos))
  601. break;
  602. clicked=0;
  603. clicked_includes_current=false;
  604. if ((spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT && b.mod.control) || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_ROTATE) {
  605. /* HANDLE ROTATION */
  606. if (get_selected_count()==0)
  607. break; //bye
  608. //handle rotate
  609. _edit.mode=TRANSFORM_ROTATE;
  610. _compute_edit(Point2(b.x,b.y));
  611. break;
  612. }
  613. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_MOVE) {
  614. if (get_selected_count()==0)
  615. break; //bye
  616. //handle rotate
  617. _edit.mode=TRANSFORM_TRANSLATE;
  618. _compute_edit(Point2(b.x,b.y));
  619. break;
  620. }
  621. if (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SCALE) {
  622. if (get_selected_count()==0)
  623. break; //bye
  624. //handle rotate
  625. _edit.mode=TRANSFORM_SCALE;
  626. _compute_edit(Point2(b.x,b.y));
  627. break;
  628. }
  629. // todo scale
  630. int gizmo_handle=-1;
  631. clicked=_select_ray(Vector2( b.x, b.y ),b.mod.shift,clicked_includes_current,&gizmo_handle,b.mod.shift);
  632. //clicking is always deferred to either move or release
  633. clicked_wants_append=b.mod.shift;
  634. if (!clicked) {
  635. if (!clicked_wants_append)
  636. _clear_selected();
  637. //default to regionselect
  638. cursor.region_select=true;
  639. cursor.region_begin=Point2(b.x,b.y);
  640. cursor.region_end=Point2(b.x,b.y);
  641. }
  642. if (clicked && gizmo_handle>=0) {
  643. Object *obj=ObjectDB::get_instance(clicked);
  644. if (obj) {
  645. Spatial *spa = obj->cast_to<Spatial>();
  646. if (spa) {
  647. Ref<SpatialEditorGizmo> seg=spa->get_gizmo();
  648. if (seg.is_valid()) {
  649. _edit.gizmo=seg;
  650. _edit.gizmo_handle=gizmo_handle;
  651. //_edit.gizmo_initial_pos=seg->get_handle_pos(gizmo_handle);
  652. _edit.gizmo_initial_value=seg->get_handle_value(gizmo_handle);
  653. //print_line("GIZMO: "+itos(gizmo_handle)+" FROMPOS: "+_edit.orig_gizmo_pos);
  654. break;
  655. }
  656. }
  657. }
  658. //_compute_edit(Point2(b.x,b.y)); //in case a motion happens..
  659. }
  660. surface->update();
  661. } else {
  662. if (_edit.gizmo.is_valid()) {
  663. _edit.gizmo->commit_handle(_edit.gizmo_handle,_edit.gizmo_initial_value,false);
  664. _edit.gizmo=Ref<SpatialEditorGizmo>();
  665. break;
  666. }
  667. if (clicked) {
  668. _select_clicked(clicked_wants_append,true);
  669. //clickd processing was deferred
  670. clicked=0;
  671. }
  672. if (cursor.region_select) {
  673. _select_region();
  674. cursor.region_select=false;
  675. surface->update();
  676. }
  677. if (_edit.mode!=TRANSFORM_NONE) {
  678. static const char* _transform_name[4]={"None","Rotate","Translate","Scale"};
  679. undo_redo->create_action(_transform_name[_edit.mode]);
  680. List<Node*> &selection = editor_selection->get_selected_node_list();
  681. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  682. Spatial *sp = E->get()->cast_to<Spatial>();
  683. if (!sp)
  684. continue;
  685. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  686. if (!se)
  687. continue;
  688. undo_redo->add_do_method(sp,"set_global_transform",sp->get_global_transform());
  689. undo_redo->add_undo_method(sp,"set_global_transform",se->original);
  690. }
  691. undo_redo->commit_action();
  692. _edit.mode=TRANSFORM_NONE;
  693. //VisualServer::get_singleton()->poly_clear(indicators);
  694. set_message("");
  695. }
  696. surface->update();
  697. }
  698. } break;
  699. }
  700. } break;
  701. case InputEvent::MOUSE_MOTION: {
  702. const InputEventMouseMotion &m=p_event.mouse_motion;
  703. _edit.mouse_pos=Point2(p_event.mouse_motion.x,p_event.mouse_motion.y);
  704. if (spatial_editor->get_selected()) {
  705. Ref<SpatialEditorGizmo> seg = spatial_editor->get_selected()->get_gizmo();
  706. if (seg.is_valid()) {
  707. int selected_handle=-1;
  708. int handle=-1;
  709. Vector3 point;
  710. Vector3 normal;
  711. bool inters = seg->intersect_ray(camera,_edit.mouse_pos,point,normal,&handle,false);
  712. if (inters && handle!=-1) {
  713. selected_handle=handle;
  714. }
  715. if (selected_handle!=spatial_editor->get_over_gizmo_handle()) {
  716. spatial_editor->set_over_gizmo_handle(selected_handle);
  717. spatial_editor->get_selected()->update_gizmo();
  718. if (selected_handle!=-1)
  719. spatial_editor->select_gizmo_hilight_axis(-1);
  720. }
  721. }
  722. }
  723. if (spatial_editor->get_over_gizmo_handle()==-1 && !(m.button_mask&1) && !_edit.gizmo.is_valid()) {
  724. _gizmo_select(_edit.mouse_pos,true);
  725. }
  726. NavigationScheme nav_scheme = _get_navigation_schema("3d_editor/navigation_scheme");
  727. NavigationMode nav_mode = NAVIGATION_NONE;
  728. if (_edit.gizmo.is_valid()) {
  729. Plane plane=Plane(_edit.gizmo_initial_pos,_get_camera_normal());
  730. Vector3 ray_pos=_get_ray_pos( Vector2( m.x, m.y ) );
  731. Vector3 ray=_get_ray( Vector2( m.x, m.y ) );
  732. //Vector3 intersection;
  733. //if (!plane.intersects_ray(ray_pos,ray,&intersection))
  734. // break;
  735. _edit.gizmo->set_handle(_edit.gizmo_handle,camera,Vector2(m.x,m.y));
  736. Variant v = _edit.gizmo->get_handle_value(_edit.gizmo_handle);
  737. String n = _edit.gizmo->get_handle_name(_edit.gizmo_handle);
  738. set_message(n+": "+String(v));
  739. } else if (m.button_mask&1) {
  740. if (nav_scheme == NAVIGATION_MAYA && m.mod.alt) {
  741. nav_mode = NAVIGATION_ORBIT;
  742. } else if (nav_scheme == NAVIGATION_MODO && m.mod.alt && m.mod.shift) {
  743. nav_mode = NAVIGATION_PAN;
  744. } else if (nav_scheme == NAVIGATION_MODO && m.mod.alt && m.mod.control) {
  745. nav_mode = NAVIGATION_ZOOM;
  746. } else if (nav_scheme == NAVIGATION_MODO && m.mod.alt) {
  747. nav_mode = NAVIGATION_ORBIT;
  748. } else {
  749. if (clicked) {
  750. if (!clicked_includes_current) {
  751. _select_clicked(clicked_wants_append,true);
  752. //clickd processing was deferred
  753. }
  754. _compute_edit(_edit.mouse_pos);
  755. clicked=0;
  756. _edit.mode=TRANSFORM_TRANSLATE;
  757. }
  758. if (cursor.region_select && nav_mode == NAVIGATION_NONE) {
  759. cursor.region_end=Point2(m.x,m.y);
  760. surface->update();
  761. return;
  762. }
  763. if (_edit.mode==TRANSFORM_NONE && nav_mode == NAVIGATION_NONE)
  764. break;
  765. Vector3 ray_pos=_get_ray_pos( Vector2( m.x, m.y ) );
  766. Vector3 ray=_get_ray( Vector2( m.x, m.y ) );
  767. switch(_edit.mode) {
  768. case TRANSFORM_SCALE: {
  769. Plane plane=Plane(_edit.center,_get_camera_normal());
  770. Vector3 intersection;
  771. if (!plane.intersects_ray(ray_pos,ray,&intersection))
  772. break;
  773. Vector3 click;
  774. if (!plane.intersects_ray(_edit.click_ray_pos,_edit.click_ray,&click))
  775. break;
  776. float center_click_dist = click.distance_to(_edit.center);
  777. float center_inters_dist = intersection.distance_to(_edit.center);
  778. if (center_click_dist==0)
  779. break;
  780. float scale = (center_inters_dist / center_click_dist)*100.0;
  781. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  782. scale = Math::stepify(scale,spatial_editor->get_scale_snap());
  783. }
  784. set_message("Scaling to "+String::num(scale,1)+"%.");
  785. scale/=100.0;
  786. Transform r;
  787. r.basis.scale(Vector3(scale,scale,scale));
  788. List<Node*> &selection = editor_selection->get_selected_node_list();
  789. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  790. Spatial *sp = E->get()->cast_to<Spatial>();
  791. if (!sp)
  792. continue;
  793. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  794. if (!se)
  795. continue;
  796. Transform original=se->original;
  797. Transform base=Transform( Matrix3(), _edit.center);
  798. Transform t=base * (r * (base.inverse() * original));
  799. sp->set_global_transform(t);
  800. }
  801. surface->update();
  802. } break;
  803. case TRANSFORM_TRANSLATE: {
  804. Vector3 motion_mask;
  805. Plane plane;
  806. switch( _edit.plane ) {
  807. case TRANSFORM_VIEW:
  808. motion_mask=Vector3(0,0,0);
  809. plane=Plane(_edit.center,_get_camera_normal());
  810. break;
  811. case TRANSFORM_X_AXIS:
  812. motion_mask=spatial_editor->get_gizmo_transform().basis.get_axis(0);
  813. plane=Plane(_edit.center,motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  814. break;
  815. case TRANSFORM_Y_AXIS:
  816. motion_mask=spatial_editor->get_gizmo_transform().basis.get_axis(1);
  817. plane=Plane(_edit.center,motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  818. break;
  819. case TRANSFORM_Z_AXIS:
  820. motion_mask=spatial_editor->get_gizmo_transform().basis.get_axis(2);
  821. plane=Plane(_edit.center,motion_mask.cross(motion_mask.cross(_get_camera_normal())).normalized());
  822. break;
  823. }
  824. Vector3 intersection;
  825. if (!plane.intersects_ray(ray_pos,ray,&intersection))
  826. break;
  827. Vector3 click;
  828. if (!plane.intersects_ray(_edit.click_ray_pos,_edit.click_ray,&click))
  829. break;
  830. //_validate_selection();
  831. Vector3 motion = intersection-click;
  832. if (motion_mask!=Vector3()) {
  833. motion=motion_mask.dot(motion)*motion_mask;
  834. }
  835. float snap=0;
  836. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  837. snap = spatial_editor->get_translate_snap();
  838. motion.snap(snap);
  839. }
  840. //set_message("Translating: "+motion);
  841. List<Node*> &selection = editor_selection->get_selected_node_list();
  842. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  843. Spatial *sp = E->get()->cast_to<Spatial>();
  844. if (!sp) {
  845. continue;
  846. }
  847. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  848. if (!se) {
  849. continue;
  850. }
  851. Transform t=se->original;
  852. t.origin+=motion;
  853. sp->set_global_transform(t);
  854. }
  855. } break;
  856. case TRANSFORM_ROTATE: {
  857. Plane plane;
  858. switch( _edit.plane ) {
  859. case TRANSFORM_VIEW:
  860. plane=Plane(_edit.center,_get_camera_normal());
  861. break;
  862. case TRANSFORM_X_AXIS:
  863. plane=Plane(_edit.center,spatial_editor->get_gizmo_transform().basis.get_axis(0));
  864. break;
  865. case TRANSFORM_Y_AXIS:
  866. plane=Plane(_edit.center,spatial_editor->get_gizmo_transform().basis.get_axis(1));
  867. break;
  868. case TRANSFORM_Z_AXIS:
  869. plane=Plane(_edit.center,spatial_editor->get_gizmo_transform().basis.get_axis(2));
  870. break;
  871. }
  872. Vector3 intersection;
  873. if (!plane.intersects_ray(ray_pos,ray,&intersection))
  874. break;
  875. Vector3 click;
  876. if (!plane.intersects_ray(_edit.click_ray_pos,_edit.click_ray,&click))
  877. break;
  878. Vector3 y_axis=(click-_edit.center).normalized();
  879. Vector3 x_axis=plane.normal.cross(y_axis).normalized();
  880. float angle=Math::atan2( x_axis.dot(intersection-_edit.center), y_axis.dot(intersection-_edit.center) );
  881. if (_edit.snap || spatial_editor->is_snap_enabled()) {
  882. float snap = spatial_editor->get_rotate_snap();
  883. if (snap) {
  884. angle=Math::rad2deg(angle)+snap*0.5; //else it wont reach +180
  885. angle-=Math::fmod(angle,snap);
  886. set_message("Rotating "+rtos(angle)+" degrees.");
  887. angle=Math::deg2rad(angle);
  888. } else
  889. set_message("Rotating "+rtos(Math::rad2deg(angle))+" degrees.");
  890. } else {
  891. set_message("Rotating "+rtos(Math::rad2deg(angle))+" degrees.");
  892. }
  893. Transform r;
  894. r.basis.rotate(plane.normal,-angle);
  895. List<Node*> &selection = editor_selection->get_selected_node_list();
  896. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  897. Spatial *sp = E->get()->cast_to<Spatial>();
  898. if (!sp)
  899. continue;
  900. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  901. if (!se)
  902. continue;
  903. Transform original=se->original;
  904. Transform base=Transform( Matrix3(), _edit.center);
  905. Transform t=base * (r * (base.inverse() * original));
  906. sp->set_global_transform(t);
  907. }
  908. surface->update();
  909. /*
  910. VisualServer::get_singleton()->poly_clear(indicators);
  911. Vector<Vector3> points;
  912. Vector<Vector3> empty;
  913. Vector<Color> colors;
  914. points.push_back(intersection);
  915. points.push_back(_edit.original.origin);
  916. colors.push_back( Color(255,155,100) );
  917. colors.push_back( Color(255,155,100) );
  918. VisualServer::get_singleton()->poly_add_primitive(indicators,points,empty,colors,empty);
  919. */
  920. } break;
  921. default:{}
  922. }
  923. }
  924. } else if (m.button_mask&2) {
  925. if (nav_scheme == NAVIGATION_MAYA && m.mod.alt) {
  926. nav_mode = NAVIGATION_ZOOM;
  927. }
  928. } else if (m.button_mask&4) {
  929. if (nav_scheme == NAVIGATION_GODOT) {
  930. int mod = 0;
  931. if (m.mod.shift)
  932. mod=KEY_SHIFT;
  933. if (m.mod.alt)
  934. mod=KEY_ALT;
  935. if (m.mod.control)
  936. mod=KEY_CONTROL;
  937. if (m.mod.meta)
  938. mod=KEY_META;
  939. if (mod == _get_key_modifier("3d_editor/pan_modifier"))
  940. nav_mode = NAVIGATION_PAN;
  941. else if (mod == _get_key_modifier("3d_editor/zoom_modifier"))
  942. nav_mode = NAVIGATION_ZOOM;
  943. else if (mod == _get_key_modifier("3d_editor/orbit_modifier"))
  944. nav_mode = NAVIGATION_ORBIT;
  945. } else if (nav_scheme == NAVIGATION_MAYA) {
  946. if (m.mod.alt)
  947. nav_mode = NAVIGATION_PAN;
  948. }
  949. }
  950. switch(nav_mode) {
  951. case NAVIGATION_PAN:{
  952. real_t pan_speed = 1/150.0;
  953. int pan_speed_modifier = 10;
  954. if (nav_scheme==NAVIGATION_MAYA && m.mod.shift)
  955. pan_speed *= pan_speed_modifier;
  956. Transform camera_transform;
  957. camera_transform.translate(cursor.pos);
  958. camera_transform.basis.rotate(Vector3(0,1,0),cursor.y_rot);
  959. camera_transform.basis.rotate(Vector3(1,0,0),cursor.x_rot);
  960. Vector3 translation(-m.relative_x*pan_speed,m.relative_y*pan_speed,0);
  961. translation*=cursor.distance/DISTANCE_DEFAULT;
  962. camera_transform.translate(translation);
  963. cursor.pos=camera_transform.origin;
  964. } break;
  965. case NAVIGATION_ZOOM: {
  966. real_t zoom_speed = 1/80.0;
  967. int zoom_speed_modifier = 10;
  968. if (nav_scheme==NAVIGATION_MAYA && m.mod.shift)
  969. zoom_speed *= zoom_speed_modifier;
  970. if ( m.relative_y > 0)
  971. cursor.distance*=1+m.relative_y*zoom_speed;
  972. else if (m.relative_y < 0)
  973. cursor.distance/=1-m.relative_y*zoom_speed;
  974. } break;
  975. case NAVIGATION_ORBIT: {
  976. cursor.x_rot+=m.relative_y/80.0;
  977. cursor.y_rot+=m.relative_x/80.0;
  978. if (cursor.x_rot>Math_PI/2.0)
  979. cursor.x_rot=Math_PI/2.0;
  980. if (cursor.x_rot<-Math_PI/2.0)
  981. cursor.x_rot=-Math_PI/2.0;
  982. } break;
  983. default: {}
  984. }
  985. } break;
  986. case InputEvent::KEY: {
  987. const InputEventKey &k = p_event.key;
  988. switch(k.scancode) {
  989. case KEY_S: {
  990. if (_edit.mode!=TRANSFORM_NONE) {
  991. _edit.snap=true;
  992. }
  993. } break;
  994. case KEY_KP_7: {
  995. cursor.y_rot=0;
  996. if (k.mod.shift) {
  997. cursor.x_rot=-Math_PI/2.0;
  998. set_message("Bottom View.",2);
  999. } else {
  1000. cursor.x_rot=Math_PI/2.0;
  1001. set_message("Top View.",2);
  1002. }
  1003. } break;
  1004. case KEY_KP_1: {
  1005. cursor.x_rot=0;
  1006. if (k.mod.shift) {
  1007. cursor.y_rot=Math_PI;
  1008. set_message("Rear View.",2);
  1009. } else {
  1010. cursor.y_rot=0;
  1011. set_message("Front View.",2);
  1012. }
  1013. } break;
  1014. case KEY_KP_3: {
  1015. cursor.x_rot=0;
  1016. if (k.mod.shift) {
  1017. cursor.y_rot=Math_PI/2.0;
  1018. set_message("Left View.",2);
  1019. } else {
  1020. cursor.y_rot=-Math_PI/2.0;
  1021. set_message("Right View.",2);
  1022. }
  1023. } break;
  1024. case KEY_KP_5: {
  1025. orthogonal = !orthogonal;
  1026. _menu_option(orthogonal?VIEW_PERSPECTIVE:VIEW_ORTHOGONAL);
  1027. } break;
  1028. case KEY_K: {
  1029. if (!get_selected_count() || _edit.mode!=TRANSFORM_NONE)
  1030. break;
  1031. if (!editor->get_animation_editor()->has_keying()) {
  1032. set_message("Keying is disabled (no key inserted).");
  1033. break;
  1034. }
  1035. List<Node*> &selection = editor_selection->get_selected_node_list();
  1036. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1037. Spatial *sp = E->get()->cast_to<Spatial>();
  1038. if (!sp)
  1039. continue;
  1040. emit_signal("transform_key_request",sp,"",sp->get_transform());
  1041. }
  1042. set_message("Animation Key Inserted.");
  1043. } break;
  1044. case KEY_F: {
  1045. if (k.pressed && k.mod.shift && k.mod.control) {
  1046. _menu_option(VIEW_ALIGN_SELECTION_WITH_VIEW);
  1047. } else if (k.pressed) {
  1048. _menu_option(VIEW_CENTER_TO_SELECTION);
  1049. }
  1050. } break;
  1051. case KEY_SPACE: {
  1052. if (!k.pressed)
  1053. emit_signal("toggle_maximize_view", this);
  1054. } break;
  1055. }
  1056. } break;
  1057. }
  1058. }
  1059. void SpatialEditorViewport::set_message(String p_message,float p_time) {
  1060. message=p_message;
  1061. message_time=p_time;
  1062. }
  1063. void SpatialEditorViewport::_notification(int p_what) {
  1064. if (p_what==NOTIFICATION_VISIBILITY_CHANGED) {
  1065. bool visible=is_visible();
  1066. set_process(visible);
  1067. call_deferred("update_transform_gizmo_view");
  1068. }
  1069. if (p_what==NOTIFICATION_RESIZED) {
  1070. call_deferred("update_transform_gizmo_view");
  1071. }
  1072. if (p_what==NOTIFICATION_PROCESS) {
  1073. //force editr camera
  1074. /*
  1075. current_camera=get_root_node()->get_current_camera();
  1076. if (current_camera!=camera) {
  1077. }
  1078. */
  1079. if (orthogonal) {
  1080. Size2 size=get_size();
  1081. Size2 vpsize = Point2(cursor.distance*size.get_aspect(),cursor.distance/size.get_aspect());
  1082. //camera->set_orthogonal(size.width*cursor.distance,get_znear(),get_zfar());
  1083. camera->set_orthogonal(2*cursor.distance,0.1,8192);
  1084. } else
  1085. camera->set_perspective(get_fov(),get_znear(),get_zfar());
  1086. Transform camera_transform;
  1087. camera_transform.translate( cursor.pos );
  1088. camera_transform.basis.rotate(Vector3(0,1,0),cursor.y_rot);
  1089. camera_transform.basis.rotate(Vector3(1,0,0),cursor.x_rot);
  1090. if (orthogonal)
  1091. camera_transform.translate(0,0,4096);
  1092. else
  1093. camera_transform.translate(0,0,cursor.distance);
  1094. if (camera->get_global_transform()!=camera_transform) {
  1095. camera->set_global_transform( camera_transform );
  1096. update_transform_gizmo_view();
  1097. }
  1098. Map<Node*,Object*> &selection = editor_selection->get_selection();
  1099. bool changed=false;
  1100. bool exist=false;
  1101. for(Map<Node*,Object*>::Element *E=selection.front();E;E=E->next()) {
  1102. Spatial *sp = E->key()->cast_to<Spatial>();
  1103. if (!sp)
  1104. continue;
  1105. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1106. if (!se)
  1107. continue;
  1108. /*
  1109. ??
  1110. if (!se->poly_instance.is_valid())
  1111. continue;
  1112. if (!ObjectDB::get_instance( E->key() )) {
  1113. VisualServer::get_singleton()->free( se->poly_instance );
  1114. se->poly_instance=RID();
  1115. continue;
  1116. }
  1117. */
  1118. VisualInstance *vi=sp->cast_to<VisualInstance>();
  1119. if (se->aabb.has_no_surface()) {
  1120. se->aabb=vi?vi->get_aabb():AABB( Vector3(-0.2,-0.2,-0.2),Vector3(0.4,0.4,0.4));
  1121. }
  1122. Transform t=sp->get_global_transform();
  1123. t.translate(se->aabb.pos);
  1124. t.basis.scale( se->aabb.size );
  1125. exist=true;
  1126. if (se->last_xform==t)
  1127. continue;
  1128. changed=true;
  1129. se->last_xform=t;
  1130. VisualServer::get_singleton()->instance_set_transform(se->sbox_instance,t);
  1131. }
  1132. if (changed || (spatial_editor->is_gizmo_visible() && !exist)) {
  1133. spatial_editor->update_transform_gizmo();
  1134. }
  1135. if (message_time>0) {
  1136. if (message!=last_message) {
  1137. surface->update();
  1138. last_message=message;
  1139. }
  1140. message_time-=get_fixed_process_delta_time();
  1141. if (message_time<0)
  1142. surface->update();
  1143. }
  1144. //grid
  1145. Vector3 grid_cam_axis=_get_camera_normal();
  1146. /*
  1147. for(int i=0;i<3;i++) {
  1148. Vector3 axis;
  1149. axis[i]=1;
  1150. bool should_be_visible= grid_enabled && (grid_enable[i] || (Math::abs(grid_cam_axis.dot(axis))>0.99 && orthogonal));
  1151. if (should_be_visible!=grid_visible[i]) {
  1152. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,should_be_visible);
  1153. grid_visible[i]=should_be_visible;
  1154. }
  1155. }
  1156. if (last_grid_snap!=spatial_editor->get_translate_snap()) {
  1157. last_grid_snap=spatial_editor->get_translate_snap()
  1158. Transform gridt;
  1159. gridt.basis.scale(Vector3(last_grid_snap,last_grid_snap,last_grid_snap));
  1160. for(int i=0;i<3;i++)
  1161. VisualServer::get_singleton()->instance_set_transform(grid_instance[i],gridt);
  1162. }*/
  1163. }
  1164. if (p_what==NOTIFICATION_ENTER_SCENE) {
  1165. surface->connect("draw",this,"_draw");
  1166. surface->connect("input_event",this,"_sinput");
  1167. surface->connect("mouse_enter",this,"_smouseenter");
  1168. preview_camera->set_icon(get_icon("Camera","EditorIcons"));
  1169. _init_gizmo_instance(index);
  1170. }
  1171. if (p_what==NOTIFICATION_MOUSE_ENTER) {
  1172. }
  1173. if (p_what==NOTIFICATION_DRAW) {
  1174. }
  1175. }
  1176. void SpatialEditorViewport::_draw() {
  1177. if (surface->has_focus()) {
  1178. Size2 size = surface->get_size();
  1179. Rect2 r =Rect2(Point2(),size);
  1180. get_stylebox("EditorFocus","EditorStyles")->draw(surface->get_canvas_item(),r);
  1181. }
  1182. RID ci=surface->get_canvas_item();
  1183. if (cursor.region_select) {
  1184. 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));
  1185. }
  1186. if (message_time>0) {
  1187. Ref<Font> font = get_font("font","Label");
  1188. Point2 msgpos=Point2(5,get_size().y-20);
  1189. font->draw(ci,msgpos+Point2(1,1),message,Color(0,0,0,0.8));
  1190. font->draw(ci,msgpos+Point2(-1,-1),message,Color(0,0,0,0.8));
  1191. font->draw(ci,msgpos,message,Color(1,1,1,1));
  1192. }
  1193. if (_edit.mode==TRANSFORM_ROTATE) {
  1194. Point2 center = _point_to_screen(_edit.center);
  1195. VisualServer::get_singleton()->canvas_item_add_line(ci,_edit.mouse_pos, center, Color(0.4,0.7,1.0,0.8));
  1196. }
  1197. if (previewing) {
  1198. Size2 ss = Size2( Globals::get_singleton()->get("display/width"), Globals::get_singleton()->get("display/height") );
  1199. float aspect = ss.get_aspect();
  1200. Size2 s = get_size();
  1201. Rect2 draw_rect;
  1202. switch(previewing->get_keep_aspect_mode()) {
  1203. case Camera::KEEP_WIDTH: {
  1204. draw_rect.size = Size2(s.width,s.width/aspect);
  1205. draw_rect.pos.x=0;
  1206. draw_rect.pos.y=(s.height-draw_rect.size.y)*0.5;
  1207. } break;
  1208. case Camera::KEEP_HEIGHT: {
  1209. draw_rect.size = Size2(s.height*aspect,s.height);
  1210. draw_rect.pos.y=0;
  1211. draw_rect.pos.x=(s.width-draw_rect.size.x)*0.5;
  1212. } break;
  1213. }
  1214. draw_rect = Rect2(Vector2(),s).clip(draw_rect);
  1215. surface->draw_line(draw_rect.pos,draw_rect.pos+Vector2(draw_rect.size.x,0),Color(0.6,0.6,0.1,0.5),2.0);
  1216. surface->draw_line(draw_rect.pos+Vector2(draw_rect.size.x,0),draw_rect.pos+draw_rect.size,Color(0.6,0.6,0.1,0.5),2.0);
  1217. surface->draw_line(draw_rect.pos+draw_rect.size,draw_rect.pos+Vector2(0,draw_rect.size.y),Color(0.6,0.6,0.1,0.5),2.0);
  1218. surface->draw_line(draw_rect.pos,draw_rect.pos+Vector2(0,draw_rect.size.y),Color(0.6,0.6,0.1,0.5),2.0);
  1219. }
  1220. }
  1221. void SpatialEditorViewport::_menu_option(int p_option) {
  1222. switch(p_option) {
  1223. case VIEW_TOP: {
  1224. cursor.x_rot=Math_PI/2.0;
  1225. cursor.y_rot=0;
  1226. } break;
  1227. case VIEW_BOTTOM: {
  1228. cursor.x_rot=-Math_PI/2.0;
  1229. cursor.y_rot=0;
  1230. } break;
  1231. case VIEW_LEFT: {
  1232. cursor.y_rot=Math_PI/2.0;
  1233. cursor.x_rot=0;
  1234. } break;
  1235. case VIEW_RIGHT: {
  1236. cursor.y_rot=-Math_PI/2.0;
  1237. cursor.x_rot=0;
  1238. } break;
  1239. case VIEW_FRONT: {
  1240. cursor.y_rot=0;
  1241. cursor.x_rot=0;
  1242. } break;
  1243. case VIEW_REAR: {
  1244. cursor.y_rot=Math_PI;
  1245. cursor.x_rot=0;
  1246. } break;
  1247. case VIEW_CENTER_TO_SELECTION: {
  1248. if (!get_selected_count())
  1249. break;
  1250. Vector3 center;
  1251. int count=0;
  1252. List<Node*> &selection = editor_selection->get_selected_node_list();
  1253. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1254. Spatial *sp = E->get()->cast_to<Spatial>();
  1255. if (!sp)
  1256. continue;
  1257. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1258. if (!se)
  1259. continue;
  1260. center+=sp->get_global_transform().origin;
  1261. count++;
  1262. }
  1263. center/=float(count);
  1264. cursor.pos=center;
  1265. } break;
  1266. case VIEW_ALIGN_SELECTION_WITH_VIEW: {
  1267. if (!get_selected_count())
  1268. break;
  1269. Transform camera_transform = camera->get_global_transform();
  1270. List<Node*> &selection = editor_selection->get_selected_node_list();
  1271. undo_redo->create_action("Align with view");
  1272. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1273. Spatial *sp = E->get()->cast_to<Spatial>();
  1274. if (!sp)
  1275. continue;
  1276. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1277. if (!se)
  1278. continue;
  1279. Vector3 original_scale = sp->get_scale();
  1280. sp->set_global_transform(camera_transform);
  1281. sp->set_scale(original_scale);
  1282. undo_redo->add_do_method(sp,"set_global_transform",sp->get_global_transform());
  1283. undo_redo->add_undo_method(sp,"set_global_transform",se->original);
  1284. }
  1285. undo_redo->commit_action();
  1286. } break;
  1287. case VIEW_ENVIRONMENT: {
  1288. int idx = view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT);
  1289. bool current = view_menu->get_popup()->is_item_checked( idx );
  1290. current=!current;
  1291. if (current) {
  1292. camera->set_environment(RES());
  1293. } else {
  1294. camera->set_environment(SpatialEditor::get_singleton()->get_viewport_environment());
  1295. }
  1296. view_menu->get_popup()->set_item_checked( idx, current );
  1297. } break;
  1298. case VIEW_PERSPECTIVE: {
  1299. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), true );
  1300. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), false );
  1301. orthogonal=false;
  1302. call_deferred("update_transform_gizmo_view");
  1303. } break;
  1304. case VIEW_ORTHOGONAL: {
  1305. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE), false );
  1306. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_ORTHOGONAL), true );
  1307. orthogonal=true;
  1308. call_deferred("update_transform_gizmo_view");
  1309. } break;
  1310. case VIEW_AUDIO_LISTENER: {
  1311. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  1312. bool current = view_menu->get_popup()->is_item_checked( idx );
  1313. current=!current;
  1314. viewport->set_as_audio_listener(current);
  1315. view_menu->get_popup()->set_item_checked( idx, current );
  1316. } break;
  1317. case VIEW_GIZMOS: {
  1318. int idx = view_menu->get_popup()->get_item_index(VIEW_GIZMOS);
  1319. bool current = view_menu->get_popup()->is_item_checked( idx );
  1320. current=!current;
  1321. if (current)
  1322. camera->set_visible_layers( ((1<<20)-1)|(1<<(GIZMO_BASE_LAYER+index))|(1<<GIZMO_EDIT_LAYER) );
  1323. else
  1324. camera->set_visible_layers( ((1<<20)-1)|(1<<(GIZMO_BASE_LAYER+index)) );
  1325. view_menu->get_popup()->set_item_checked( idx, current );
  1326. } break;
  1327. }
  1328. }
  1329. void SpatialEditorViewport::_preview_exited_scene() {
  1330. preview_camera->set_pressed(false);
  1331. _toggle_camera_preview(false);
  1332. view_menu->show();
  1333. }
  1334. void SpatialEditorViewport::_init_gizmo_instance(int p_idx) {
  1335. uint32_t layer=1<<(GIZMO_BASE_LAYER+p_idx);
  1336. for(int i=0;i<3;i++) {
  1337. move_gizmo_instance[i]=VS::get_singleton()->instance_create();
  1338. VS::get_singleton()->instance_set_base(move_gizmo_instance[i],spatial_editor->get_move_gizmo(i)->get_rid());
  1339. VS::get_singleton()->instance_set_scenario(move_gizmo_instance[i],get_scene()->get_root()->get_world()->get_scenario());
  1340. VS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i],VS::INSTANCE_FLAG_VISIBLE,false);
  1341. //VS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i],VS::INSTANCE_FLAG_DEPH_SCALE,true);
  1342. VS::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i],VS::INSTANCE_FLAG_CAST_SHADOW,false);
  1343. VS::get_singleton()->instance_set_layer_mask(move_gizmo_instance[i],layer);
  1344. rotate_gizmo_instance[i]=VS::get_singleton()->instance_create();
  1345. VS::get_singleton()->instance_set_base(rotate_gizmo_instance[i],spatial_editor->get_rotate_gizmo(i)->get_rid());
  1346. VS::get_singleton()->instance_set_scenario(rotate_gizmo_instance[i],get_scene()->get_root()->get_world()->get_scenario());
  1347. VS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i],VS::INSTANCE_FLAG_VISIBLE,false);
  1348. //VS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i],VS::INSTANCE_FLAG_DEPH_SCALE,true);
  1349. VS::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i],VS::INSTANCE_FLAG_CAST_SHADOW,false);
  1350. VS::get_singleton()->instance_set_layer_mask(rotate_gizmo_instance[i],layer);
  1351. }
  1352. }
  1353. void SpatialEditorViewport::_toggle_camera_preview(bool p_activate) {
  1354. ERR_FAIL_COND(p_activate && !preview);
  1355. ERR_FAIL_COND(!p_activate && !previewing);
  1356. if (!p_activate) {
  1357. previewing->disconnect("exit_scene",this,"_preview_exited_scene");
  1358. previewing=NULL;
  1359. VS::get_singleton()->viewport_attach_camera( viewport->get_viewport(), camera->get_camera() ); //restore
  1360. if (!preview)
  1361. preview_camera->hide();
  1362. view_menu->show();
  1363. surface->update();
  1364. } else {
  1365. previewing=preview;
  1366. previewing->connect("exit_scene",this,"_preview_exited_scene");
  1367. VS::get_singleton()->viewport_attach_camera( viewport->get_viewport(), preview->get_camera() ); //replace
  1368. view_menu->hide();
  1369. surface->update();
  1370. }
  1371. }
  1372. void SpatialEditorViewport::set_can_preview(Camera* p_preview) {
  1373. preview=p_preview;
  1374. if (!preview_camera->is_pressed()) {
  1375. if (p_preview) {
  1376. preview_camera->show();
  1377. } else {
  1378. preview_camera->hide();
  1379. }
  1380. }
  1381. }
  1382. void SpatialEditorViewport::update_transform_gizmo_view() {
  1383. if (!is_visible())
  1384. return;
  1385. Transform xform = spatial_editor->get_gizmo_transform();
  1386. Transform camera_xform = camera->get_transform();
  1387. Vector3 camz = -camera_xform.get_basis().get_axis(2).normalized();
  1388. Vector3 camy = -camera_xform.get_basis().get_axis(1).normalized();
  1389. Plane p(camera_xform.origin,camz);
  1390. float gizmo_d = Math::abs( p.distance_to(xform.origin ));
  1391. float d0 = camera->unproject_position(camera_xform.origin+camz*gizmo_d).y;
  1392. float d1 = camera->unproject_position(camera_xform.origin+camz*gizmo_d+camy).y;
  1393. float dd = Math::abs(d0-d1);
  1394. if (dd==0)
  1395. dd=0.0001;
  1396. float gsize = EditorSettings::get_singleton()->get("3d_editor/manipulator_gizmo_size");
  1397. gizmo_scale=(gsize/Math::abs(dd));
  1398. Vector3 scale = Vector3(1,1,1) * gizmo_scale;
  1399. xform.basis.scale(scale);
  1400. //xform.basis.scale(GIZMO_SCALE_DEFAULT*Vector3(1,1,1));
  1401. for(int i=0;i<3;i++) {
  1402. VisualServer::get_singleton()->instance_set_transform(move_gizmo_instance[i], xform );
  1403. VisualServer::get_singleton()->instance_geometry_set_flag(move_gizmo_instance[i],VS::INSTANCE_FLAG_VISIBLE,spatial_editor->is_gizmo_visible()&& (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_MOVE) );
  1404. VisualServer::get_singleton()->instance_set_transform(rotate_gizmo_instance[i], xform );
  1405. VisualServer::get_singleton()->instance_geometry_set_flag(rotate_gizmo_instance[i],VS::INSTANCE_FLAG_VISIBLE,spatial_editor->is_gizmo_visible() && (spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_SELECT || spatial_editor->get_tool_mode()==SpatialEditor::TOOL_MODE_ROTATE) );
  1406. }
  1407. }
  1408. void SpatialEditorViewport::set_state(const Dictionary& p_state) {
  1409. cursor.pos=p_state["pos"];
  1410. cursor.x_rot=p_state["x_rot"];
  1411. cursor.y_rot=p_state["y_rot"];
  1412. cursor.distance=p_state["distance"];
  1413. bool env = p_state["use_environment"];
  1414. bool orth = p_state["use_orthogonal"];
  1415. if (orth)
  1416. _menu_option(VIEW_ORTHOGONAL);
  1417. else
  1418. _menu_option(VIEW_PERSPECTIVE);
  1419. if (env != camera->get_environment().is_valid())
  1420. _menu_option(VIEW_ENVIRONMENT);
  1421. if (p_state.has("listener")) {
  1422. bool listener = p_state["listener"];
  1423. int idx = view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER);
  1424. viewport->set_as_audio_listener(listener);
  1425. view_menu->get_popup()->set_item_checked( idx, listener );
  1426. }
  1427. }
  1428. Dictionary SpatialEditorViewport::get_state() const {
  1429. Dictionary d;
  1430. d["pos"]=cursor.pos;
  1431. d["x_rot"]=cursor.x_rot;
  1432. d["y_rot"]=cursor.y_rot;
  1433. d["distance"]=cursor.distance;
  1434. d["use_environment"]=camera->get_environment().is_valid();
  1435. d["use_orthogonal"]=camera->get_projection()==Camera::PROJECTION_ORTHOGONAL;
  1436. d["listener"]=viewport->is_audio_listener();
  1437. return d;
  1438. }
  1439. void SpatialEditorViewport::_bind_methods(){
  1440. ObjectTypeDB::bind_method(_MD("_draw"),&SpatialEditorViewport::_draw);
  1441. ObjectTypeDB::bind_method(_MD("_smouseenter"),&SpatialEditorViewport::_smouseenter);
  1442. ObjectTypeDB::bind_method(_MD("_sinput"),&SpatialEditorViewport::_sinput);
  1443. ObjectTypeDB::bind_method(_MD("_menu_option"),&SpatialEditorViewport::_menu_option);
  1444. ObjectTypeDB::bind_method(_MD("_toggle_camera_preview"),&SpatialEditorViewport::_toggle_camera_preview);
  1445. ObjectTypeDB::bind_method(_MD("_preview_exited_scene"),&SpatialEditorViewport::_preview_exited_scene);
  1446. ObjectTypeDB::bind_method(_MD("update_transform_gizmo_view"),&SpatialEditorViewport::update_transform_gizmo_view);
  1447. ADD_SIGNAL( MethodInfo("toggle_maximize_view", PropertyInfo(Variant::OBJECT, "viewport")) );
  1448. }
  1449. void SpatialEditorViewport::reset() {
  1450. orthogonal=false;
  1451. message_time=0;
  1452. message="";
  1453. last_message="";
  1454. cursor.x_rot=0;
  1455. cursor.y_rot=0;
  1456. cursor.distance=4;
  1457. cursor.region_select=false;
  1458. }
  1459. SpatialEditorViewport::SpatialEditorViewport(SpatialEditor *p_spatial_editor, EditorNode *p_editor, int p_index) {
  1460. index=p_index;
  1461. editor=p_editor;
  1462. editor_selection=editor->get_editor_selection();;
  1463. undo_redo=editor->get_undo_redo();
  1464. clicked=0;
  1465. clicked_includes_current=false;
  1466. orthogonal=false;
  1467. message_time=0;
  1468. spatial_editor=p_spatial_editor;
  1469. Control *c=memnew(Control);
  1470. add_child(c);
  1471. c->set_area_as_parent_rect();
  1472. viewport = memnew( Viewport );
  1473. c->add_child(viewport);
  1474. surface = memnew( Control );
  1475. add_child(surface);
  1476. surface->set_area_as_parent_rect();
  1477. camera = memnew(Camera);
  1478. camera->set_disable_gizmo(true);
  1479. camera->set_visible_layers( ((1<<20)-1)|(1<<(GIZMO_BASE_LAYER+p_index))|(1<<GIZMO_EDIT_LAYER) );
  1480. //camera->set_environment(SpatialEditor::get_singleton()->get_viewport_environment());
  1481. viewport->add_child(camera);
  1482. camera->make_current();
  1483. surface->set_focus_mode(FOCUS_ALL);
  1484. view_menu = memnew( MenuButton );
  1485. surface->add_child(view_menu);
  1486. view_menu->set_pos( Point2(4,4));
  1487. view_menu->set_text("[view]");
  1488. view_menu->set_self_opacity(0.5);
  1489. view_menu->get_popup()->add_item("Top",VIEW_TOP);
  1490. view_menu->get_popup()->add_item("Bottom",VIEW_BOTTOM);
  1491. view_menu->get_popup()->add_item("Left",VIEW_LEFT);
  1492. view_menu->get_popup()->add_item("Right",VIEW_RIGHT);
  1493. view_menu->get_popup()->add_item("Front",VIEW_FRONT);
  1494. view_menu->get_popup()->add_item("Rear",VIEW_REAR);
  1495. view_menu->get_popup()->add_separator();
  1496. view_menu->get_popup()->add_check_item("Perspective",VIEW_PERSPECTIVE);
  1497. view_menu->get_popup()->add_check_item("Orthogonal",VIEW_ORTHOGONAL);
  1498. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_PERSPECTIVE),true);
  1499. view_menu->get_popup()->add_separator();
  1500. view_menu->get_popup()->add_check_item("Environment",VIEW_ENVIRONMENT);
  1501. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_ENVIRONMENT),true);
  1502. view_menu->get_popup()->add_separator();
  1503. view_menu->get_popup()->add_check_item("Audio Listener",VIEW_AUDIO_LISTENER);
  1504. view_menu->get_popup()->add_separator();
  1505. view_menu->get_popup()->add_check_item("Gizmos",VIEW_GIZMOS);
  1506. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(VIEW_GIZMOS),true);
  1507. view_menu->get_popup()->add_separator();
  1508. view_menu->get_popup()->add_item("Selection (F)",VIEW_CENTER_TO_SELECTION);
  1509. view_menu->get_popup()->add_item("Align with view (Ctrl+Shift+F)",VIEW_ALIGN_SELECTION_WITH_VIEW);
  1510. view_menu->get_popup()->connect("item_pressed",this,"_menu_option");
  1511. preview_camera = memnew( Button );
  1512. preview_camera->set_toggle_mode(true);
  1513. preview_camera->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_END,90);
  1514. preview_camera->set_anchor_and_margin(MARGIN_TOP,ANCHOR_BEGIN,10);
  1515. preview_camera->set_text("preview");
  1516. surface->add_child(preview_camera);
  1517. preview_camera->hide();
  1518. preview_camera->connect("toggled",this,"_toggle_camera_preview");
  1519. previewing=NULL;
  1520. preview=NULL;
  1521. gizmo_scale=1.0;
  1522. if (p_index==0) {
  1523. view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(VIEW_AUDIO_LISTENER),true);
  1524. viewport->set_as_audio_listener(true);
  1525. }
  1526. EditorSettings::get_singleton()->connect("settings_changed",this,"update_transform_gizmo_view");
  1527. }
  1528. SpatialEditor *SpatialEditor::singleton=NULL;
  1529. SpatialEditorSelectedItem::~SpatialEditorSelectedItem() {
  1530. if (sbox_instance.is_valid())
  1531. VisualServer::get_singleton()->free(sbox_instance);
  1532. }
  1533. void SpatialEditor::select_gizmo_hilight_axis(int p_axis) {
  1534. for(int i=0;i<3;i++) {
  1535. move_gizmo[i]->surface_set_material(0,i==p_axis?gizmo_hl:gizmo_color[i]);
  1536. rotate_gizmo[i]->surface_set_material(0,(i+3)==p_axis?gizmo_hl:gizmo_color[i]);
  1537. }
  1538. }
  1539. void SpatialEditor::update_transform_gizmo() {
  1540. List<Node*> &selection = editor_selection->get_selected_node_list();
  1541. AABB center;
  1542. bool first=true;
  1543. Matrix3 gizmo_basis;
  1544. bool local_gizmo_coords = transform_menu->get_popup()->is_item_checked( transform_menu->get_popup()->get_item_index(MENU_TRANSFORM_LOCAL_COORDS) );
  1545. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1546. Spatial *sp = E->get()->cast_to<Spatial>();
  1547. if (!sp)
  1548. continue;
  1549. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1550. if (!se)
  1551. continue;
  1552. Transform xf = se->sp->get_global_transform();
  1553. if (first) {
  1554. center.pos=xf.origin;
  1555. first=false;
  1556. if (local_gizmo_coords) {
  1557. gizmo_basis=xf.basis;
  1558. gizmo_basis.orthonormalize();
  1559. }
  1560. } else {
  1561. center.expand_to(xf.origin);
  1562. gizmo_basis=Matrix3();
  1563. }
  1564. // count++;
  1565. }
  1566. Vector3 pcenter = center.pos+center.size*0.5;
  1567. gizmo.visible=!first;
  1568. gizmo.transform.origin=pcenter;
  1569. gizmo.transform.basis=gizmo_basis;
  1570. for(int i=0;i<4;i++) {
  1571. viewports[i]->update_transform_gizmo_view();
  1572. }
  1573. }
  1574. Object *SpatialEditor::_get_editor_data(Object *p_what) {
  1575. Spatial *sp = p_what->cast_to<Spatial>();
  1576. if (!sp)
  1577. return NULL;
  1578. SpatialEditorSelectedItem *si = memnew( SpatialEditorSelectedItem );
  1579. si->sp=sp;
  1580. si->sbox_instance=VisualServer::get_singleton()->instance_create2(selection_box->get_rid(),sp->get_world()->get_scenario());
  1581. VS::get_singleton()->instance_geometry_set_flag(si->sbox_instance,VS::INSTANCE_FLAG_CAST_SHADOW,false);
  1582. RID inst = sp->call("_get_visual_instance_rid");
  1583. // if (inst.is_valid())
  1584. // si->aabb = VisualServer::get_singleton()->instance_get_base_aabb(inst);
  1585. if (get_scene()->is_editor_hint())
  1586. editor->call("edit_node",sp);
  1587. return si;
  1588. }
  1589. void SpatialEditor::_generate_selection_box() {
  1590. AABB aabb( Vector3(), Vector3(1,1,1) );
  1591. aabb.grow_by( aabb.get_longest_axis_size()/20.0 );
  1592. Ref<SurfaceTool> st = memnew( SurfaceTool );
  1593. st->begin(Mesh::PRIMITIVE_LINES);
  1594. for (int i=0;i<12;i++) {
  1595. Vector3 a,b;
  1596. aabb.get_edge(i,a,b);
  1597. /*Vector<Vector3> points;
  1598. Vector<Color> colors;
  1599. points.push_back(a);
  1600. points.push_back(b);*/
  1601. st->add_color( Color(1.0,1.0,0.8,0.8) );
  1602. st->add_vertex(a);
  1603. st->add_color( Color(1.0,1.0,0.8,0.4) );
  1604. st->add_vertex(a.linear_interpolate(b,0.2));
  1605. st->add_color( Color(1.0,1.0,0.8,0.4) );
  1606. st->add_vertex(a.linear_interpolate(b,0.8));
  1607. st->add_color( Color(1.0,1.0,0.8,0.8) );
  1608. st->add_vertex(b);
  1609. }
  1610. Ref<FixedMaterial> mat = memnew( FixedMaterial );
  1611. mat->set_flag(Material::FLAG_UNSHADED,true);
  1612. mat->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1));
  1613. mat->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA,true);
  1614. mat->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY,true);
  1615. st->set_material(mat);
  1616. selection_box = st->commit();
  1617. }
  1618. Dictionary SpatialEditor::get_state() const {
  1619. Dictionary d;
  1620. int vc=0;
  1621. if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT) ))
  1622. vc=1;
  1623. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS) ))
  1624. vc=2;
  1625. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS) ))
  1626. vc=3;
  1627. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS) ))
  1628. vc=4;
  1629. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT) ))
  1630. vc=5;
  1631. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT) ))
  1632. vc=6;
  1633. d["viewport_mode"]=vc;
  1634. Array vpdata;
  1635. for(int i=0;i<4;i++) {
  1636. vpdata.push_back( viewports[i]->get_state() );
  1637. }
  1638. d["viewports"]=vpdata;
  1639. d["default_light"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_LIGHT) );;
  1640. d["ambient_light_color"]=settings_ambient_color->get_color();
  1641. d["default_srgb"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_SRGB) );;
  1642. d["show_grid"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_GRID) );;
  1643. d["show_origin"]=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN) );;
  1644. d["fov"]=get_fov();
  1645. d["znear"]=get_znear();
  1646. d["zfar"]=get_zfar();
  1647. d["deflight_rot_x"]=settings_default_light_rot_x;
  1648. d["deflight_rot_y"]=settings_default_light_rot_y;
  1649. return d;
  1650. }
  1651. void SpatialEditor::set_state(const Dictionary& p_state) {
  1652. Dictionary d = p_state;
  1653. ERR_FAIL_COND(!d.has("viewport_mode"));
  1654. ERR_FAIL_COND(!d.has("viewports"));
  1655. ERR_FAIL_COND(!d.has("default_light"));
  1656. ERR_FAIL_COND(!d.has("show_grid"));
  1657. ERR_FAIL_COND(!d.has("show_origin"));
  1658. ERR_FAIL_COND(!d.has("fov"));
  1659. ERR_FAIL_COND(!d.has("znear"));
  1660. ERR_FAIL_COND(!d.has("zfar"));
  1661. int vc = d["viewport_mode"];
  1662. if (vc==1)
  1663. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  1664. else if (vc==2)
  1665. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  1666. else if (vc==3)
  1667. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  1668. else if (vc==4)
  1669. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  1670. else if (vc==5)
  1671. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  1672. else if (vc==6)
  1673. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  1674. Array vp = d["viewports"];
  1675. ERR_FAIL_COND(vp.size()>4);
  1676. for(int i=0;i<4;i++) {
  1677. viewports[i]->set_state(vp[i]);
  1678. }
  1679. if (d.has("zfar"))
  1680. settings_zfar->set_val(float(d["zfar"]));
  1681. if (d.has("znear"))
  1682. settings_znear->set_val(float(d["znear"]));
  1683. if (d.has("fov"))
  1684. settings_fov->set_val(float(d["fov"]));
  1685. if (d.has("default_light")) {
  1686. bool use = d["default_light"];
  1687. bool existing = light_instance.is_valid();
  1688. if (use!=existing) {
  1689. if (existing) {
  1690. VisualServer::get_singleton()->free(light_instance);
  1691. light_instance=RID();
  1692. } else {
  1693. light_instance=VisualServer::get_singleton()->instance_create2(light,get_scene()->get_root()->get_world()->get_scenario());
  1694. VisualServer::get_singleton()->instance_set_transform(light_instance,light_transform);
  1695. }
  1696. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_LIGHT), light_instance.is_valid() );
  1697. }
  1698. }
  1699. if (d.has("ambient_light_color")) {
  1700. settings_ambient_color->set_color(d["ambient_light_color"]);
  1701. viewport_environment->fx_set_param(Environment::FX_PARAM_AMBIENT_LIGHT_COLOR,d["ambient_light_color"]);
  1702. }
  1703. if (d.has("default_srgb")) {
  1704. bool use = d["default_srgb"];
  1705. viewport_environment->set_enable_fx(Environment::FX_SRGB,use);
  1706. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_DEFAULT_SRGB), use );
  1707. }
  1708. if (d.has("show_grid")) {
  1709. bool use = d["show_grid"];
  1710. if (use!=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_GRID))) {
  1711. _menu_item_pressed(MENU_VIEW_GRID);
  1712. }
  1713. }
  1714. if (d.has("show_origin")) {
  1715. bool use = d["show_origin"];
  1716. if (use!=view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN))) {
  1717. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), use );
  1718. VisualServer::get_singleton()->instance_geometry_set_flag(origin_instance,VS::INSTANCE_FLAG_VISIBLE,use);
  1719. }
  1720. }
  1721. if (d.has("deflight_rot_x"))
  1722. settings_default_light_rot_x=d["deflight_rot_x"];
  1723. if (d.has("deflight_rot_y"))
  1724. settings_default_light_rot_y=d["deflight_rot_y"];
  1725. _update_default_light_angle();
  1726. }
  1727. void SpatialEditor::edit(Spatial *p_spatial) {
  1728. if (p_spatial!=selected) {
  1729. if (selected) {
  1730. Ref<SpatialEditorGizmo> seg = selected->get_gizmo();
  1731. if (seg.is_valid()) {
  1732. seg->set_selected(false);
  1733. selected->update_gizmo();
  1734. }
  1735. }
  1736. selected=p_spatial;
  1737. over_gizmo_handle=-1;
  1738. if (selected) {
  1739. Ref<SpatialEditorGizmo> seg = selected->get_gizmo();
  1740. if (seg.is_valid()) {
  1741. seg->set_selected(true);
  1742. selected->update_gizmo();
  1743. }
  1744. }
  1745. }
  1746. if (p_spatial) {
  1747. //_validate_selection();
  1748. //if (selected.has(p_spatial->get_instance_ID()) && selected.size()==1)
  1749. // return;
  1750. //_select(p_spatial->get_instance_ID(),false,true);
  1751. // should become the selection
  1752. }
  1753. }
  1754. void SpatialEditor::_xform_dialog_action() {
  1755. Transform t;
  1756. //translation
  1757. Vector3 scale;
  1758. Vector3 rotate;
  1759. Vector3 translate;
  1760. for(int i=0;i<3;i++) {
  1761. translate[i]=xform_translate[i]->get_text().to_double();
  1762. rotate[i]=Math::deg2rad(xform_rotate[i]->get_text().to_double());
  1763. scale[i]=xform_scale[i]->get_text().to_double();
  1764. }
  1765. t.origin=translate;
  1766. for(int i=0;i<3;i++) {
  1767. if (!rotate[i])
  1768. continue;
  1769. Vector3 axis;
  1770. axis[i]=1.0;
  1771. t.basis.rotate(axis,rotate[i]);
  1772. }
  1773. for(int i=0;i<3;i++) {
  1774. if (scale[i]==1)
  1775. continue;
  1776. t.basis.set_axis(i,t.basis.get_axis(i)*scale[i]);
  1777. }
  1778. undo_redo->create_action("XForm Dialog");
  1779. List<Node*> &selection = editor_selection->get_selected_node_list();
  1780. for(List<Node*>::Element *E=selection.front();E;E=E->next()) {
  1781. Spatial *sp = E->get()->cast_to<Spatial>();
  1782. if (!sp)
  1783. continue;
  1784. SpatialEditorSelectedItem *se=editor_selection->get_node_editor_data<SpatialEditorSelectedItem>(sp);
  1785. if (!se)
  1786. continue;
  1787. bool post = xform_type->get_selected()>0;
  1788. Transform tr = sp->get_global_transform();
  1789. if (post)
  1790. tr = tr * t;
  1791. else {
  1792. tr.basis = t.basis * tr.basis;
  1793. tr.origin+=t.origin;
  1794. }
  1795. undo_redo->add_do_method(sp,"set_global_transform",tr);
  1796. undo_redo->add_undo_method(sp,"set_global_transform",sp->get_global_transform());
  1797. }
  1798. undo_redo->commit_action();
  1799. }
  1800. void SpatialEditor::_menu_item_pressed(int p_option) {
  1801. switch(p_option) {
  1802. case MENU_TOOL_SELECT:
  1803. case MENU_TOOL_MOVE:
  1804. case MENU_TOOL_ROTATE:
  1805. case MENU_TOOL_SCALE: {
  1806. for(int i=0;i<4;i++)
  1807. tool_button[i]->set_pressed(i==p_option);
  1808. tool_mode=(ToolMode)p_option;
  1809. static const char *_mode[]={"Selection Mode.","Translation Mode.","Rotation Mode.","Scale Mode."};
  1810. // set_message(_mode[p_option],3);
  1811. update_transform_gizmo();
  1812. } break;
  1813. case MENU_TRANSFORM_USE_SNAP: {
  1814. bool is_checked = transform_menu->get_popup()->is_item_checked( transform_menu->get_popup()->get_item_index(p_option) );
  1815. snap_enabled=!is_checked;
  1816. transform_menu->get_popup()->set_item_checked( transform_menu->get_popup()->get_item_index(p_option), snap_enabled );
  1817. } break;
  1818. case MENU_TRANSFORM_CONFIGURE_SNAP: {
  1819. snap_dialog->popup_centered(Size2(200,160));
  1820. } break;
  1821. case MENU_TRANSFORM_LOCAL_COORDS: {
  1822. bool is_checked = transform_menu->get_popup()->is_item_checked( transform_menu->get_popup()->get_item_index(p_option) );
  1823. transform_menu->get_popup()->set_item_checked( transform_menu->get_popup()->get_item_index(p_option), !is_checked );
  1824. update_transform_gizmo();
  1825. } break;
  1826. case MENU_TRANSFORM_DIALOG: {
  1827. for(int i=0;i<3;i++) {
  1828. xform_translate[i]->set_text("0");
  1829. xform_rotate[i]->set_text("0");
  1830. xform_scale[i]->set_text("1");
  1831. }
  1832. xform_dialog->popup_centered(Size2(200,200));
  1833. } break;
  1834. case MENU_VIEW_USE_DEFAULT_LIGHT: {
  1835. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  1836. if (is_checked) {
  1837. VisualServer::get_singleton()->free(light_instance);
  1838. light_instance=RID();
  1839. } else {
  1840. light_instance=VisualServer::get_singleton()->instance_create2(light,get_scene()->get_root()->get_world()->get_scenario());
  1841. VisualServer::get_singleton()->instance_set_transform(light_instance,light_transform);
  1842. _update_default_light_angle();
  1843. }
  1844. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), light_instance.is_valid() );
  1845. } break;
  1846. case MENU_VIEW_USE_DEFAULT_SRGB: {
  1847. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  1848. if (is_checked) {
  1849. viewport_environment->set_enable_fx(Environment::FX_SRGB,false);
  1850. } else {
  1851. viewport_environment->set_enable_fx(Environment::FX_SRGB,true);
  1852. }
  1853. is_checked = ! is_checked;
  1854. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), is_checked );
  1855. } break;
  1856. case MENU_VIEW_USE_1_VIEWPORT: {
  1857. for(int i=1;i<4;i++) {
  1858. viewports[i]->hide();
  1859. }
  1860. viewports[0]->set_area_as_parent_rect();
  1861. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), true );
  1862. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  1863. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  1864. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  1865. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  1866. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  1867. } break;
  1868. case MENU_VIEW_USE_2_VIEWPORTS: {
  1869. for(int i=1;i<4;i++) {
  1870. if (i==1 || i==3)
  1871. viewports[i]->hide();
  1872. else
  1873. viewports[i]->show();
  1874. }
  1875. viewports[0]->set_area_as_parent_rect();
  1876. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  1877. viewports[2]->set_area_as_parent_rect();
  1878. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  1879. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  1880. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), true );
  1881. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  1882. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  1883. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  1884. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  1885. } break;
  1886. case MENU_VIEW_USE_2_VIEWPORTS_ALT: {
  1887. for(int i=1;i<4;i++) {
  1888. if (i==1 || i==3)
  1889. viewports[i]->hide();
  1890. else
  1891. viewports[i]->show();
  1892. }
  1893. viewports[0]->set_area_as_parent_rect();
  1894. viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  1895. viewports[2]->set_area_as_parent_rect();
  1896. viewports[2]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  1897. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  1898. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  1899. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  1900. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  1901. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), true );
  1902. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  1903. } break;
  1904. case MENU_VIEW_USE_3_VIEWPORTS: {
  1905. for(int i=1;i<4;i++) {
  1906. if (i==1)
  1907. viewports[i]->hide();
  1908. else
  1909. viewports[i]->show();
  1910. }
  1911. viewports[0]->set_area_as_parent_rect();
  1912. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  1913. viewports[2]->set_area_as_parent_rect();
  1914. viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  1915. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  1916. viewports[3]->set_area_as_parent_rect();
  1917. viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  1918. viewports[3]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  1919. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  1920. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  1921. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), true );
  1922. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  1923. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  1924. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  1925. } break;
  1926. case MENU_VIEW_USE_3_VIEWPORTS_ALT: {
  1927. for(int i=1;i<4;i++) {
  1928. if (i==1)
  1929. viewports[i]->hide();
  1930. else
  1931. viewports[i]->show();
  1932. }
  1933. viewports[0]->set_area_as_parent_rect();
  1934. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  1935. viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  1936. viewports[2]->set_area_as_parent_rect();
  1937. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  1938. viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  1939. viewports[3]->set_area_as_parent_rect();
  1940. viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  1941. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  1942. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  1943. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  1944. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), false );
  1945. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  1946. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), true );
  1947. } break;
  1948. case MENU_VIEW_USE_4_VIEWPORTS: {
  1949. for(int i=1;i<4;i++) {
  1950. viewports[i]->show();
  1951. }
  1952. viewports[0]->set_area_as_parent_rect();
  1953. viewports[0]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  1954. viewports[0]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  1955. viewports[1]->set_area_as_parent_rect();
  1956. viewports[1]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  1957. viewports[1]->set_anchor_and_margin(MARGIN_BOTTOM,ANCHOR_RATIO,0.5);
  1958. viewports[2]->set_area_as_parent_rect();
  1959. viewports[2]->set_anchor_and_margin(MARGIN_RIGHT,ANCHOR_RATIO,0.5);
  1960. viewports[2]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  1961. viewports[3]->set_area_as_parent_rect();
  1962. viewports[3]->set_anchor_and_margin(MARGIN_LEFT,ANCHOR_RATIO,0.5);
  1963. viewports[3]->set_anchor_and_margin(MARGIN_TOP,ANCHOR_RATIO,0.5);
  1964. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT), false );
  1965. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS), false );
  1966. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS), false );
  1967. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS), true );
  1968. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT), false );
  1969. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT), false );
  1970. } break;
  1971. case MENU_VIEW_DISPLAY_NORMAL: {
  1972. VisualServer::get_singleton()->scenario_set_debug( get_scene()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_DISABLED );
  1973. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), true );
  1974. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false );
  1975. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false );
  1976. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false );
  1977. } break;
  1978. case MENU_VIEW_DISPLAY_WIREFRAME: {
  1979. VisualServer::get_singleton()->scenario_set_debug( get_scene()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_WIREFRAME );
  1980. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false );
  1981. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), true );
  1982. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false );
  1983. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false );
  1984. } break;
  1985. case MENU_VIEW_DISPLAY_OVERDRAW: {
  1986. VisualServer::get_singleton()->scenario_set_debug( get_scene()->get_root()->get_world()->get_scenario(), VisualServer::SCENARIO_DEBUG_OVERDRAW );
  1987. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_NORMAL), false );
  1988. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false );
  1989. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), true );
  1990. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), false );
  1991. } break;
  1992. case MENU_VIEW_DISPLAY_SHADELESS: {
  1993. VisualServer::get_singleton()->scenario_set_debug( get_scene()->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(MENU_VIEW_DISPLAY_NORMAL), false );
  1995. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_WIREFRAME), false );
  1996. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_OVERDRAW), false );
  1997. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_DISPLAY_SHADELESS), true );
  1998. } break;
  1999. case MENU_VIEW_ORIGIN: {
  2000. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  2001. is_checked=!is_checked;
  2002. VisualServer::get_singleton()->instance_geometry_set_flag(origin_instance,VS::INSTANCE_FLAG_VISIBLE,is_checked);
  2003. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), is_checked);
  2004. } break;
  2005. case MENU_VIEW_GRID: {
  2006. bool is_checked = view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(p_option) );
  2007. grid_enabled=!is_checked;
  2008. for(int i=0;i<3;++i) {
  2009. if (grid_enable[i]) {
  2010. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,grid_enabled);
  2011. grid_visible[i]=grid_enabled;
  2012. }
  2013. }
  2014. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(p_option), grid_enabled );
  2015. } break;
  2016. case MENU_VIEW_CAMERA_SETTINGS: {
  2017. settings_dialog->popup_centered(settings_vbc->get_combined_minimum_size()+Size2(50,50));
  2018. } break;
  2019. }
  2020. }
  2021. void SpatialEditor::_init_indicators() {
  2022. //make sure that the camera indicator is not selectable
  2023. light=VisualServer::get_singleton()->light_create( VisualServer::LIGHT_DIRECTIONAL );
  2024. //VisualServer::get_singleton()->light_set_shadow( light, true );
  2025. light_instance=VisualServer::get_singleton()->instance_create2(light,get_scene()->get_root()->get_world()->get_scenario());
  2026. light_transform.rotate(Vector3(1,0,0),Math_PI/5.0);
  2027. VisualServer::get_singleton()->instance_set_transform(light_instance,light_transform);
  2028. RID mat = VisualServer::get_singleton()->fixed_material_create();
  2029. VisualServer::get_singleton()->fixed_material_set_flag(mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA,true);
  2030. VisualServer::get_singleton()->fixed_material_set_flag(mat, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);
  2031. {
  2032. RID indicator_mat = VisualServer::get_singleton()->fixed_material_create();
  2033. VisualServer::get_singleton()->material_set_flag( indicator_mat, VisualServer::MATERIAL_FLAG_UNSHADED, true );
  2034. VisualServer::get_singleton()->material_set_flag( indicator_mat, VisualServer::MATERIAL_FLAG_ONTOP, false );
  2035. VisualServer::get_singleton()->fixed_material_set_flag(indicator_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA,true);
  2036. VisualServer::get_singleton()->fixed_material_set_flag(indicator_mat, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);
  2037. DVector<Color> grid_colors[3];
  2038. DVector<Vector3> grid_points[3];
  2039. Vector<Color> origin_colors;
  2040. Vector<Vector3> origin_points;
  2041. for(int i=0;i<3;i++) {
  2042. Vector3 axis;
  2043. axis[i]=1;
  2044. Vector3 axis_n1;
  2045. axis_n1[(i+1)%3]=1;
  2046. Vector3 axis_n2;
  2047. axis_n2[(i+2)%3]=1;
  2048. origin_colors.push_back(Color(axis.x,axis.y,axis.z));
  2049. origin_colors.push_back(Color(axis.x,axis.y,axis.z));
  2050. origin_points.push_back(axis*4096);
  2051. origin_points.push_back(axis*-4096);
  2052. #define ORIGIN_GRID_SIZE 25
  2053. for(int j=-ORIGIN_GRID_SIZE;j<=ORIGIN_GRID_SIZE;j++) {
  2054. grid_colors[i].push_back(Color(axis.x,axis.y,axis.z,0.2));
  2055. grid_colors[i].push_back(Color(axis.x,axis.y,axis.z,0.2));
  2056. grid_colors[i].push_back(Color(axis.x,axis.y,axis.z,0.2));
  2057. grid_colors[i].push_back(Color(axis.x,axis.y,axis.z,0.2));
  2058. grid_points[i].push_back(axis_n1*ORIGIN_GRID_SIZE+axis_n2*j);
  2059. grid_points[i].push_back(-axis_n1*ORIGIN_GRID_SIZE+axis_n2*j);
  2060. grid_points[i].push_back(axis_n2*ORIGIN_GRID_SIZE+axis_n1*j);
  2061. grid_points[i].push_back(-axis_n2*ORIGIN_GRID_SIZE+axis_n1*j);
  2062. }
  2063. grid[i] = VisualServer::get_singleton()->mesh_create();
  2064. Array d;
  2065. d.resize(VS::ARRAY_MAX);
  2066. d[VisualServer::ARRAY_VERTEX]=grid_points[i];
  2067. d[VisualServer::ARRAY_COLOR]=grid_colors[i];
  2068. VisualServer::get_singleton()->mesh_add_surface(grid[i],VisualServer::PRIMITIVE_LINES,d);
  2069. VisualServer::get_singleton()->mesh_surface_set_material(grid[i],0,indicator_mat);
  2070. grid_instance[i] = VisualServer::get_singleton()->instance_create2(grid[i],get_scene()->get_root()->get_world()->get_scenario());
  2071. grid_visible[i]=false;
  2072. grid_enable[i]=false;
  2073. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,false);
  2074. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_CAST_SHADOW,false);
  2075. }
  2076. origin = VisualServer::get_singleton()->mesh_create();
  2077. Array d;
  2078. d.resize(VS::ARRAY_MAX);
  2079. d[VisualServer::ARRAY_VERTEX]=origin_points;
  2080. d[VisualServer::ARRAY_COLOR]=origin_colors;
  2081. VisualServer::get_singleton()->mesh_add_surface(origin,VisualServer::PRIMITIVE_LINES,d);
  2082. VisualServer::get_singleton()->mesh_surface_set_material(origin,0,indicator_mat,true);
  2083. // origin = VisualServer::get_singleton()->poly_create();
  2084. // VisualServer::get_singleton()->poly_add_primitive(origin,origin_points,Vector<Vector3>(),origin_colors,Vector<Vector3>());
  2085. // VisualServer::get_singleton()->poly_set_material(origin,indicator_mat,true);
  2086. origin_instance = VisualServer::get_singleton()->instance_create2(origin,get_scene()->get_root()->get_world()->get_scenario());
  2087. VisualServer::get_singleton()->instance_geometry_set_flag(origin_instance,VS::INSTANCE_FLAG_CAST_SHADOW,false);
  2088. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[1],VS::INSTANCE_FLAG_VISIBLE,true);
  2089. grid_enable[1]=true;
  2090. grid_visible[1]=true;
  2091. grid_enabled=true;
  2092. last_grid_snap=1;
  2093. }
  2094. {
  2095. cursor_mesh = VisualServer::get_singleton()->mesh_create();
  2096. DVector<Vector3> cursor_points;
  2097. float cs = 0.25;
  2098. cursor_points.push_back(Vector3(+cs,0,0));
  2099. cursor_points.push_back(Vector3(-cs,0,0));
  2100. cursor_points.push_back(Vector3(0,+cs,0));
  2101. cursor_points.push_back(Vector3(0,-cs,0));
  2102. cursor_points.push_back(Vector3(0,0,+cs));
  2103. cursor_points.push_back(Vector3(0,0,-cs));
  2104. RID cmat=VisualServer::get_singleton()->fixed_material_create();
  2105. VisualServer::get_singleton()->fixed_material_set_param(cmat,VS::FIXED_MATERIAL_PARAM_DIFFUSE,Color(0,1,1));
  2106. VisualServer::get_singleton()->material_set_flag( cmat, VisualServer::MATERIAL_FLAG_UNSHADED, true );
  2107. VisualServer::get_singleton()->fixed_material_set_flag(cmat, VisualServer::FIXED_MATERIAL_FLAG_USE_ALPHA,true);
  2108. VisualServer::get_singleton()->fixed_material_set_flag(cmat, VisualServer::FIXED_MATERIAL_FLAG_USE_COLOR_ARRAY,true);
  2109. Array d;
  2110. d.resize(VS::ARRAY_MAX);
  2111. d[VS::ARRAY_VERTEX]=cursor_points;
  2112. VisualServer::get_singleton()->mesh_add_surface(cursor_mesh,VS::PRIMITIVE_LINES,d);
  2113. VisualServer::get_singleton()->mesh_surface_set_material(cursor_mesh,0,cmat,true);
  2114. cursor_instance = VisualServer::get_singleton()->instance_create2(cursor_mesh,get_scene()->get_root()->get_world()->get_scenario());
  2115. VisualServer::get_singleton()->instance_geometry_set_flag(cursor_instance,VS::INSTANCE_FLAG_CAST_SHADOW,false);
  2116. }
  2117. {
  2118. //move gizmo
  2119. float gizmo_alph = EditorSettings::get_singleton()->get("3d_editor/manipulator_gizmo_opacity");
  2120. gizmo_hl = Ref<FixedMaterial>( memnew( FixedMaterial ) );
  2121. gizmo_hl->set_flag(Material::FLAG_UNSHADED, true);
  2122. gizmo_hl->set_flag(Material::FLAG_ONTOP, true);
  2123. gizmo_hl->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2124. gizmo_hl->set_parameter(FixedMaterial::PARAM_DIFFUSE,Color(1,1,1,gizmo_alph+0.2f));
  2125. for(int i=0;i<3;i++) {
  2126. move_gizmo[i]=Ref<Mesh>( memnew( Mesh ) );
  2127. rotate_gizmo[i]=Ref<Mesh>( memnew( Mesh ) );
  2128. Ref<FixedMaterial> mat = memnew( FixedMaterial );
  2129. mat->set_flag(Material::FLAG_UNSHADED, true);
  2130. mat->set_flag(Material::FLAG_ONTOP, true);
  2131. mat->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2132. Color col;
  2133. col[i]=1.0;
  2134. col.a= gizmo_alph;
  2135. mat->set_parameter(FixedMaterial::PARAM_DIFFUSE,col);
  2136. gizmo_color[i]=mat;
  2137. Vector3 ivec;
  2138. ivec[i]=1;
  2139. Vector3 nivec;
  2140. nivec[(i+1)%3]=1;
  2141. nivec[(i+2)%3]=1;
  2142. Vector3 ivec2;
  2143. ivec2[(i+1)%3]=1;
  2144. Vector3 ivec3;
  2145. ivec3[(i+2)%3]=1;
  2146. {
  2147. Ref<SurfaceTool> surftool = memnew( SurfaceTool );
  2148. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  2149. //translate
  2150. const int arrow_points=5;
  2151. Vector3 arrow[5]={
  2152. nivec*0.0+ivec*0.0,
  2153. nivec*0.01+ivec*0.0,
  2154. nivec*0.01+ivec*1.0,
  2155. nivec*0.1+ivec*1.0,
  2156. nivec*0.0+ivec*(1+GIZMO_ARROW_SIZE),
  2157. };
  2158. int arrow_sides=6;
  2159. for(int i = 0; i < 7 ; i++) {
  2160. Matrix3 ma(ivec,Math_PI*2*float(i)/arrow_sides);
  2161. Matrix3 mb(ivec,Math_PI*2*float(i+1)/arrow_sides);
  2162. for(int j=0;j<arrow_points-1;j++) {
  2163. Vector3 points[4]={
  2164. ma.xform(arrow[j]),
  2165. mb.xform(arrow[j]),
  2166. mb.xform(arrow[j+1]),
  2167. ma.xform(arrow[j+1]),
  2168. };
  2169. surftool->add_vertex(points[0]);
  2170. surftool->add_vertex(points[1]);
  2171. surftool->add_vertex(points[2]);
  2172. surftool->add_vertex(points[0]);
  2173. surftool->add_vertex(points[2]);
  2174. surftool->add_vertex(points[3]);
  2175. }
  2176. }
  2177. surftool->set_material(mat);
  2178. surftool->commit(move_gizmo[i]);
  2179. }
  2180. {
  2181. Ref<SurfaceTool> surftool = memnew( SurfaceTool );
  2182. surftool->begin(Mesh::PRIMITIVE_TRIANGLES);
  2183. Vector3 circle[5]={
  2184. ivec*0.02+ivec2*0.02+ivec2*1.0,
  2185. ivec*-0.02+ivec2*0.02+ivec2*1.0,
  2186. ivec*-0.02+ivec2*-0.02+ivec2*1.0,
  2187. ivec*0.02+ivec2*-0.02+ivec2*1.0,
  2188. ivec*0.02+ivec2*0.02+ivec2*1.0,
  2189. };
  2190. for(int k = 0; k < 33 ; k++) {
  2191. Matrix3 ma(ivec,Math_PI*2*float(k)/32);
  2192. Matrix3 mb(ivec,Math_PI*2*float(k+1)/32);
  2193. for(int j=0;j<4;j++) {
  2194. Vector3 points[4]={
  2195. ma.xform(circle[j]),
  2196. mb.xform(circle[j]),
  2197. mb.xform(circle[j+1]),
  2198. ma.xform(circle[j+1]),
  2199. };
  2200. surftool->add_vertex(points[0]);
  2201. surftool->add_vertex(points[1]);
  2202. surftool->add_vertex(points[2]);
  2203. surftool->add_vertex(points[0]);
  2204. surftool->add_vertex(points[2]);
  2205. surftool->add_vertex(points[3]);
  2206. }
  2207. }
  2208. surftool->set_material(mat);
  2209. surftool->commit(rotate_gizmo[i]);
  2210. }
  2211. }
  2212. }
  2213. /*for(int i=0;i<4;i++) {
  2214. viewports[i]->init_gizmo_instance(i);
  2215. }*/
  2216. _generate_selection_box();
  2217. //get_scene()->get_root_node()->cast_to<EditorNode>()->get_scene_root()->add_child(camera);
  2218. //current_camera=camera;
  2219. }
  2220. void SpatialEditor::_finish_indicators() {
  2221. VisualServer::get_singleton()->free(origin_instance);
  2222. VisualServer::get_singleton()->free(origin);
  2223. for(int i=0;i<3;i++) {
  2224. VisualServer::get_singleton()->free(grid_instance[i]);
  2225. VisualServer::get_singleton()->free(grid[i]);
  2226. }
  2227. VisualServer::get_singleton()->free(light_instance);
  2228. VisualServer::get_singleton()->free(light);
  2229. //VisualServer::get_singleton()->free(poly);
  2230. //VisualServer::get_singleton()->free(indicators_instance);
  2231. //VisualServer::get_singleton()->free(indicators);
  2232. VisualServer::get_singleton()->free(cursor_instance);
  2233. VisualServer::get_singleton()->free(cursor_mesh);
  2234. }
  2235. void SpatialEditor::_instance_scene() {
  2236. #if 0
  2237. EditorNode *en = get_scene()->get_root_node()->cast_to<EditorNode>();
  2238. ERR_FAIL_COND(!en);
  2239. String path = en->get_scenes_dock()->get_selected_path();
  2240. if (path=="") {
  2241. set_message("No scene selected to instance!");
  2242. return;
  2243. }
  2244. undo_redo->create_action("Instance at Cursor");
  2245. Node* scene = en->request_instance_scene(path);
  2246. if (!scene) {
  2247. set_message("Could not instance scene!");
  2248. undo_redo->commit_action(); //bleh
  2249. return;
  2250. }
  2251. Spatial *s = scene->cast_to<Spatial>();
  2252. if (s) {
  2253. undo_redo->add_do_method(s,"set_global_transform",Transform(Matrix3(),cursor.cursor_pos));
  2254. }
  2255. undo_redo->commit_action();
  2256. #endif
  2257. }
  2258. /*
  2259. void SpatialEditor::_update_selection() {
  2260. }
  2261. */
  2262. void SpatialEditor::_unhandled_key_input(InputEvent p_event) {
  2263. if (!is_visible())
  2264. return;
  2265. {
  2266. EditorNode *en = editor;
  2267. EditorPlugin *over_plugin = en->get_editor_plugin_over();
  2268. if (over_plugin && over_plugin->forward_input_event(p_event)) {
  2269. return; //ate the over input event
  2270. }
  2271. }
  2272. switch(p_event.type) {
  2273. case InputEvent::KEY: {
  2274. const InputEventKey &k=p_event.key;
  2275. if (!k.pressed)
  2276. break;
  2277. switch(k.scancode) {
  2278. case KEY_Q: _menu_item_pressed(MENU_TOOL_SELECT); break;
  2279. case KEY_W: _menu_item_pressed(MENU_TOOL_MOVE); break;
  2280. case KEY_E: _menu_item_pressed(MENU_TOOL_ROTATE); break;
  2281. case KEY_R: _menu_item_pressed(MENU_TOOL_SCALE); break;
  2282. #if 0
  2283. #endif
  2284. }
  2285. } break;
  2286. }
  2287. }
  2288. void SpatialEditor::_notification(int p_what) {
  2289. if (p_what==NOTIFICATION_READY) {
  2290. tool_button[SpatialEditor::TOOL_MODE_SELECT]->set_icon( get_icon("ToolSelect","EditorIcons") );
  2291. tool_button[SpatialEditor::TOOL_MODE_MOVE]->set_icon( get_icon("ToolMove","EditorIcons") );
  2292. tool_button[SpatialEditor::TOOL_MODE_ROTATE]->set_icon( get_icon("ToolRotate","EditorIcons") );
  2293. tool_button[SpatialEditor::TOOL_MODE_SCALE]->set_icon( get_icon("ToolScale","EditorIcons") );
  2294. instance_button->set_icon( get_icon("SpatialAdd","EditorIcons") );
  2295. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT),get_icon("Panels1","EditorIcons"));
  2296. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS),get_icon("Panels2","EditorIcons"));
  2297. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT),get_icon("Panels2Alt","EditorIcons"));
  2298. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS),get_icon("Panels3","EditorIcons"));
  2299. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT),get_icon("Panels3Alt","EditorIcons"));
  2300. view_menu->get_popup()->set_item_icon(view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS),get_icon("Panels4","EditorIcons"));
  2301. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  2302. get_scene()->connect("node_removed",this,"_node_removed");
  2303. VS::get_singleton()->scenario_set_fallback_environment(get_viewport()->find_world()->get_scenario(),viewport_environment->get_rid());
  2304. }
  2305. if (p_what==NOTIFICATION_ENTER_SCENE) {
  2306. gizmos = memnew( SpatialEditorGizmos );
  2307. _init_indicators();
  2308. _update_default_light_angle();
  2309. }
  2310. if (p_what==NOTIFICATION_EXIT_SCENE) {
  2311. _finish_indicators();
  2312. memdelete( gizmos );
  2313. }
  2314. }
  2315. void SpatialEditor::add_control_to_menu_panel(Control *p_control) {
  2316. hbc_menu->add_child(p_control);
  2317. }
  2318. void SpatialEditor::set_can_preview(Camera* p_preview) {
  2319. for(int i=0;i<4;i++) {
  2320. viewports[i]->set_can_preview(p_preview);
  2321. }
  2322. }
  2323. VSplitContainer *SpatialEditor::get_shader_split() {
  2324. return shader_split;
  2325. }
  2326. HSplitContainer *SpatialEditor::get_palette_split() {
  2327. return palette_split;
  2328. }
  2329. void SpatialEditor::_request_gizmo(Object* p_obj) {
  2330. Spatial *sp=p_obj->cast_to<Spatial>();
  2331. if (!sp)
  2332. return;
  2333. if (editor->get_edited_scene() && (sp==editor->get_edited_scene() || sp->get_owner()==editor->get_edited_scene())) {
  2334. Ref<SpatialEditorGizmo> seg = gizmos->get_gizmo(sp);
  2335. if (seg.is_valid()) {
  2336. sp->set_gizmo(seg);
  2337. }
  2338. for (List<EditorPlugin*>::Element *E=gizmo_plugins.front();E;E=E->next()) {
  2339. if (E->get()->create_spatial_gizmo(sp)) {
  2340. seg = sp->get_gizmo();
  2341. if (sp==selected && seg.is_valid()) {
  2342. seg->set_selected(true);
  2343. selected->update_gizmo();
  2344. }
  2345. return;
  2346. }
  2347. }
  2348. if (seg.is_valid() && sp==selected) {
  2349. seg->set_selected(true);
  2350. selected->update_gizmo();
  2351. }
  2352. }
  2353. }
  2354. void SpatialEditor::_toggle_maximize_view(Object* p_viewport) {
  2355. if (!p_viewport) return;
  2356. SpatialEditorViewport *current_viewport = p_viewport->cast_to<SpatialEditorViewport>();
  2357. if (!current_viewport) return;
  2358. int index=-1;
  2359. bool maximized = false;
  2360. for(int i=0;i<4;i++) {
  2361. if (viewports[i]==current_viewport) {
  2362. index=i;
  2363. if ( current_viewport->get_global_rect() == viewport_base->get_global_rect() )
  2364. maximized=true;
  2365. break;
  2366. }
  2367. }
  2368. if (index==-1) return;
  2369. if (!maximized) {
  2370. for(int i=0;i<4;i++) {
  2371. if (i==index)
  2372. viewports[i]->set_area_as_parent_rect();
  2373. else
  2374. viewports[i]->hide();
  2375. }
  2376. } else {
  2377. for(int i=0;i<4;i++)
  2378. viewports[i]->show();
  2379. if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_1_VIEWPORT) ))
  2380. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  2381. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS) ))
  2382. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS);
  2383. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_2_VIEWPORTS_ALT) ))
  2384. _menu_item_pressed(MENU_VIEW_USE_2_VIEWPORTS_ALT);
  2385. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS) ))
  2386. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS);
  2387. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_3_VIEWPORTS_ALT) ))
  2388. _menu_item_pressed(MENU_VIEW_USE_3_VIEWPORTS_ALT);
  2389. else if (view_menu->get_popup()->is_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_USE_4_VIEWPORTS) ))
  2390. _menu_item_pressed(MENU_VIEW_USE_4_VIEWPORTS);
  2391. }
  2392. }
  2393. void SpatialEditor::_node_removed(Node* p_node) {
  2394. if (p_node==selected)
  2395. selected=NULL;
  2396. }
  2397. void SpatialEditor::_bind_methods() {
  2398. // ObjectTypeDB::bind_method("_input_event",&SpatialEditor::_input_event);
  2399. ObjectTypeDB::bind_method("_unhandled_key_input",&SpatialEditor::_unhandled_key_input);
  2400. ObjectTypeDB::bind_method("_node_removed",&SpatialEditor::_node_removed);
  2401. ObjectTypeDB::bind_method("_menu_item_pressed",&SpatialEditor::_menu_item_pressed);
  2402. ObjectTypeDB::bind_method("_xform_dialog_action",&SpatialEditor::_xform_dialog_action);
  2403. ObjectTypeDB::bind_method("_instance_scene",&SpatialEditor::_instance_scene);
  2404. // ObjectTypeDB::bind_method("_update_selection",&SpatialEditor::_update_selection);
  2405. ObjectTypeDB::bind_method("_get_editor_data",&SpatialEditor::_get_editor_data);
  2406. ObjectTypeDB::bind_method("_request_gizmo",&SpatialEditor::_request_gizmo);
  2407. ObjectTypeDB::bind_method("_default_light_angle_input",&SpatialEditor::_default_light_angle_input);
  2408. ObjectTypeDB::bind_method("_update_ambient_light_color",&SpatialEditor::_update_ambient_light_color);
  2409. ObjectTypeDB::bind_method("_toggle_maximize_view",&SpatialEditor::_toggle_maximize_view);
  2410. ADD_SIGNAL( MethodInfo("transform_key_request") );
  2411. }
  2412. void SpatialEditor::clear() {
  2413. settings_fov->set_val(EDITOR_DEF("3d_editor/default_fov",60.0));
  2414. settings_znear->set_val(EDITOR_DEF("3d_editor/default_z_near",0.1));
  2415. settings_zfar->set_val(EDITOR_DEF("3d_editor/default_z_far",1500.0));
  2416. for(int i=0;i<4;i++) {
  2417. viewports[i]->reset();
  2418. }
  2419. _menu_item_pressed(MENU_VIEW_USE_1_VIEWPORT);
  2420. _menu_item_pressed(MENU_VIEW_DISPLAY_NORMAL);
  2421. VisualServer::get_singleton()->instance_geometry_set_flag(origin_instance,VS::INSTANCE_FLAG_VISIBLE,true);
  2422. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_ORIGIN), true);
  2423. for(int i=0;i<3;++i) {
  2424. if (grid_enable[i]) {
  2425. VisualServer::get_singleton()->instance_geometry_set_flag(grid_instance[i],VS::INSTANCE_FLAG_VISIBLE,true);
  2426. grid_visible[i]=true;
  2427. }
  2428. }
  2429. for(int i=0;i<4;i++) {
  2430. viewports[i]->view_menu->get_popup()->set_item_checked(view_menu->get_popup()->get_item_index(SpatialEditorViewport::VIEW_AUDIO_LISTENER),i==0);
  2431. viewports[i]->viewport->set_as_audio_listener(i==0);
  2432. }
  2433. view_menu->get_popup()->set_item_checked( view_menu->get_popup()->get_item_index(MENU_VIEW_GRID), true );
  2434. settings_default_light_rot_x=Math_PI*0.3;
  2435. settings_default_light_rot_y=Math_PI*0.2;
  2436. viewport_environment->fx_set_param(Environment::FX_PARAM_AMBIENT_LIGHT_COLOR,Color(0.15,0.15,0.15));
  2437. settings_ambient_color->set_color(Color(0.15,0.15,0.15));
  2438. if (!light_instance.is_valid())
  2439. _menu_item_pressed(MENU_VIEW_USE_DEFAULT_LIGHT);
  2440. _update_default_light_angle();
  2441. }
  2442. void SpatialEditor::_update_ambient_light_color(const Color& p_color) {
  2443. viewport_environment->fx_set_param(Environment::FX_PARAM_AMBIENT_LIGHT_COLOR,settings_ambient_color->get_color());
  2444. }
  2445. void SpatialEditor::_update_default_light_angle() {
  2446. Transform t;
  2447. t.basis.rotate(Vector3(1,0,0),settings_default_light_rot_x);
  2448. t.basis.rotate(Vector3(0,1,0),settings_default_light_rot_y);
  2449. settings_dlight->set_transform(t);
  2450. if (light_instance.is_valid()) {
  2451. VS::get_singleton()->instance_set_transform(light_instance,t);
  2452. }
  2453. }
  2454. void SpatialEditor::_default_light_angle_input(const InputEvent& p_event) {
  2455. if (p_event.type==InputEvent::MOUSE_MOTION && p_event.mouse_motion.button_mask&(0x1|0x2|0x4)) {
  2456. settings_default_light_rot_y = Math::fposmod(settings_default_light_rot_y - p_event.mouse_motion.relative_x*0.01,Math_PI*2.0);
  2457. settings_default_light_rot_x = Math::fposmod(settings_default_light_rot_x - p_event.mouse_motion.relative_y*0.01,Math_PI*2.0);
  2458. _update_default_light_angle();
  2459. }
  2460. }
  2461. SpatialEditor::SpatialEditor(EditorNode *p_editor) {
  2462. viewport_environment = Ref<Environment>( memnew( Environment ) );
  2463. undo_redo=p_editor->get_undo_redo();
  2464. VBoxContainer *vbc = this;
  2465. custom_camera=NULL;
  2466. singleton=this;
  2467. editor=p_editor;
  2468. editor_selection=editor->get_editor_selection();
  2469. editor_selection->add_editor_plugin(this);
  2470. editor_selection->connect("selection_changed",this,"_update_selection");
  2471. snap_enabled=false;
  2472. tool_mode = TOOL_MODE_SELECT;
  2473. //set_focus_mode(FOCUS_ALL);
  2474. hbc_menu = memnew( HBoxContainer );
  2475. vbc->add_child(hbc_menu);
  2476. Vector<Variant> button_binds;
  2477. button_binds.resize(1);
  2478. tool_button[TOOL_MODE_SELECT] = memnew( ToolButton );
  2479. hbc_menu->add_child( tool_button[TOOL_MODE_SELECT] );
  2480. tool_button[TOOL_MODE_SELECT]->set_toggle_mode(true);
  2481. tool_button[TOOL_MODE_SELECT]->set_flat(true);
  2482. tool_button[TOOL_MODE_SELECT]->set_pressed(true);
  2483. button_binds[0]=MENU_TOOL_SELECT;
  2484. tool_button[TOOL_MODE_SELECT]->connect("pressed", this,"_menu_item_pressed",button_binds);
  2485. tool_button[TOOL_MODE_SELECT]->set_tooltip("Select Mode (Q)");
  2486. tool_button[TOOL_MODE_MOVE] = memnew( ToolButton );
  2487. hbc_menu->add_child( tool_button[TOOL_MODE_MOVE] );
  2488. tool_button[TOOL_MODE_MOVE]->set_toggle_mode(true);
  2489. tool_button[TOOL_MODE_MOVE]->set_flat(true);
  2490. button_binds[0]=MENU_TOOL_MOVE;
  2491. tool_button[TOOL_MODE_MOVE]->connect("pressed", this,"_menu_item_pressed",button_binds);
  2492. tool_button[TOOL_MODE_MOVE]->set_tooltip("Move Mode (W)");
  2493. tool_button[TOOL_MODE_ROTATE] = memnew( ToolButton );
  2494. hbc_menu->add_child( tool_button[TOOL_MODE_ROTATE] );
  2495. tool_button[TOOL_MODE_ROTATE]->set_toggle_mode(true);
  2496. tool_button[TOOL_MODE_ROTATE]->set_flat(true);
  2497. button_binds[0]=MENU_TOOL_ROTATE;
  2498. tool_button[TOOL_MODE_ROTATE]->connect("pressed", this,"_menu_item_pressed",button_binds);
  2499. tool_button[TOOL_MODE_ROTATE]->set_tooltip("Rotate Mode (E)");
  2500. tool_button[TOOL_MODE_SCALE] = memnew( ToolButton );
  2501. hbc_menu->add_child( tool_button[TOOL_MODE_SCALE] );
  2502. tool_button[TOOL_MODE_SCALE]->set_toggle_mode(true);
  2503. tool_button[TOOL_MODE_SCALE]->set_flat(true);
  2504. button_binds[0]=MENU_TOOL_SCALE;
  2505. tool_button[TOOL_MODE_SCALE]->connect("pressed", this,"_menu_item_pressed",button_binds);
  2506. tool_button[TOOL_MODE_SCALE]->set_tooltip("Scale Mode (R)");
  2507. instance_button = memnew( Button );
  2508. hbc_menu->add_child( instance_button );
  2509. instance_button->set_flat(true);
  2510. instance_button->connect("pressed",this,"_instance_scene");
  2511. VSeparator *vs = memnew( VSeparator );
  2512. hbc_menu->add_child(vs);
  2513. PopupMenu *p;
  2514. transform_menu = memnew( MenuButton );
  2515. transform_menu->set_text("Transform");
  2516. hbc_menu->add_child( transform_menu );
  2517. p = transform_menu->get_popup();
  2518. p->add_check_item("Use Snap",MENU_TRANSFORM_USE_SNAP);
  2519. p->add_item("Configure Snap..",MENU_TRANSFORM_CONFIGURE_SNAP);
  2520. p->add_separator();
  2521. p->add_check_item("Local Coords",MENU_TRANSFORM_LOCAL_COORDS);
  2522. //p->set_item_checked(p->get_item_count()-1,true);
  2523. p->add_separator();
  2524. p->add_item("Transform Dialog..",MENU_TRANSFORM_DIALOG);
  2525. p->connect("item_pressed", this,"_menu_item_pressed");
  2526. view_menu = memnew( MenuButton );
  2527. view_menu->set_text("View");
  2528. view_menu->set_pos( Point2( 212,0) );
  2529. hbc_menu->add_child( view_menu );
  2530. p = view_menu->get_popup();
  2531. p->add_check_item("Use Default Light",MENU_VIEW_USE_DEFAULT_LIGHT);
  2532. p->add_check_item("Use Default sRGB",MENU_VIEW_USE_DEFAULT_SRGB);
  2533. p->add_separator();
  2534. p->add_check_item("1 Viewport",MENU_VIEW_USE_1_VIEWPORT,KEY_MASK_ALT+KEY_1);
  2535. p->add_check_item("2 Viewports",MENU_VIEW_USE_2_VIEWPORTS,KEY_MASK_ALT+KEY_2);
  2536. p->add_check_item("2 Viewports (Alt)",MENU_VIEW_USE_2_VIEWPORTS_ALT,KEY_MASK_SHIFT+KEY_MASK_ALT+KEY_2);
  2537. p->add_check_item("3 Viewports",MENU_VIEW_USE_3_VIEWPORTS,KEY_MASK_ALT+KEY_3);
  2538. p->add_check_item("3 Viewports (Alt)",MENU_VIEW_USE_3_VIEWPORTS_ALT,KEY_MASK_SHIFT+KEY_MASK_ALT+KEY_3);
  2539. p->add_check_item("4 Viewports",MENU_VIEW_USE_4_VIEWPORTS,KEY_MASK_ALT+KEY_4);
  2540. p->add_separator();
  2541. p->add_check_item("Display Normal",MENU_VIEW_DISPLAY_NORMAL);
  2542. p->add_check_item("Display Wireframe",MENU_VIEW_DISPLAY_WIREFRAME);
  2543. p->add_check_item("Display Overdraw",MENU_VIEW_DISPLAY_OVERDRAW);
  2544. p->add_check_item("Display Shadeless",MENU_VIEW_DISPLAY_SHADELESS);
  2545. p->add_separator();
  2546. p->add_check_item("View Origin",MENU_VIEW_ORIGIN);
  2547. p->add_check_item("View Grid",MENU_VIEW_GRID);
  2548. p->add_separator();
  2549. p->add_check_item("Settings",MENU_VIEW_CAMERA_SETTINGS );
  2550. p->set_item_checked( p->get_item_index(MENU_VIEW_USE_DEFAULT_LIGHT), true );
  2551. p->set_item_checked( p->get_item_index(MENU_VIEW_DISPLAY_NORMAL), true );
  2552. p->set_item_checked( p->get_item_index(MENU_VIEW_ORIGIN), true );
  2553. p->set_item_checked( p->get_item_index(MENU_VIEW_GRID), true );
  2554. p->connect("item_pressed", this,"_menu_item_pressed");
  2555. /* REST OF MENU */
  2556. palette_split = memnew( HSplitContainer);
  2557. palette_split->set_v_size_flags(SIZE_EXPAND_FILL);
  2558. vbc->add_child(palette_split);
  2559. shader_split = memnew( VSplitContainer );
  2560. shader_split->set_h_size_flags(SIZE_EXPAND_FILL);
  2561. palette_split->add_child(shader_split);
  2562. viewport_base = memnew( Control );
  2563. shader_split->add_child(viewport_base);
  2564. viewport_base->set_v_size_flags(SIZE_EXPAND_FILL);
  2565. for(int i=0;i<4;i++) {
  2566. viewports[i] = memnew( SpatialEditorViewport(this,editor,i) );
  2567. viewports[i]->connect("toggle_maximize_view",this,"_toggle_maximize_view");
  2568. viewport_base->add_child(viewports[i]);
  2569. }
  2570. //vbc->add_child(viewport_base);
  2571. /* SNAP DIALOG */
  2572. snap_dialog = memnew( ConfirmationDialog );
  2573. snap_dialog->set_title("Snap Settings");
  2574. add_child(snap_dialog);
  2575. Label *l = memnew(Label);
  2576. l->set_text("Translate Snap:");
  2577. l->set_pos(Point2(5,5));
  2578. snap_dialog->add_child(l);
  2579. snap_translate = memnew( LineEdit );
  2580. snap_translate->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  2581. snap_translate->set_begin( Point2(15,22) );
  2582. snap_translate->set_end( Point2(15,35) );
  2583. snap_translate->set_text("1");
  2584. snap_dialog->add_child(snap_translate);
  2585. l = memnew(Label);
  2586. l->set_text("Rotate Snap (deg.):");
  2587. l->set_pos(Point2(5,45));
  2588. snap_dialog->add_child(l);
  2589. snap_rotate = memnew( LineEdit );
  2590. snap_rotate->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  2591. snap_rotate->set_begin( Point2(15,62) );
  2592. snap_rotate->set_end( Point2(15,75) );
  2593. snap_rotate->set_text("5");
  2594. snap_dialog->add_child(snap_rotate);
  2595. l = memnew(Label);
  2596. l->set_text("Scale Snap (%):");
  2597. l->set_pos(Point2(5,85));
  2598. snap_dialog->add_child(l);
  2599. snap_scale = memnew( LineEdit );
  2600. snap_scale->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  2601. snap_scale->set_begin( Point2(15,102) );
  2602. snap_scale->set_end( Point2(15,115) );
  2603. snap_scale->set_text("5");
  2604. snap_dialog->add_child(snap_scale);
  2605. //snap_dialog->get_cancel()->hide();
  2606. /* SNAP DIALOG */
  2607. settings_dialog = memnew( ConfirmationDialog );
  2608. settings_dialog->set_title("Viewport Settings");
  2609. add_child(settings_dialog);
  2610. settings_vbc = memnew( VBoxContainer );
  2611. settings_vbc->set_custom_minimum_size(Size2(200,0));
  2612. settings_dialog->add_child(settings_vbc);
  2613. settings_dialog->set_child_rect(settings_vbc);
  2614. settings_light_base = memnew( Control );
  2615. settings_light_base->set_custom_minimum_size(Size2(128,128));
  2616. settings_light_base->connect("input_event",this,"_default_light_angle_input");
  2617. settings_vbc->add_margin_child("Default Light Normal:",settings_light_base);
  2618. settings_light_vp = memnew( Viewport );
  2619. settings_light_vp->set_use_own_world(true);
  2620. settings_light_base->add_child(settings_light_vp);
  2621. settings_dlight = memnew( DirectionalLight );
  2622. settings_light_vp->add_child(settings_dlight);
  2623. settings_sphere = memnew( ImmediateGeometry );
  2624. settings_sphere->begin(Mesh::PRIMITIVE_TRIANGLES,Ref<Texture>());
  2625. settings_sphere->set_color(Color(1,1,1));
  2626. settings_sphere->add_sphere(32,16,1);
  2627. settings_sphere->end();
  2628. settings_light_vp->add_child(settings_sphere);
  2629. settings_camera = memnew( Camera );
  2630. settings_light_vp->add_child(settings_camera);
  2631. settings_camera->set_translation(Vector3(0,0,2));
  2632. settings_camera->set_orthogonal(2.1,0.1,5);
  2633. settings_default_light_rot_x=Math_PI*0.3;
  2634. settings_default_light_rot_y=Math_PI*0.2;
  2635. settings_ambient_color = memnew( ColorPickerButton );
  2636. settings_vbc->add_margin_child("Ambient Light Color:",settings_ambient_color);
  2637. settings_ambient_color->connect("color_changed",this,"_update_ambient_light_color");
  2638. viewport_environment->set_enable_fx(Environment::FX_AMBIENT_LIGHT,true);
  2639. viewport_environment->fx_set_param(Environment::FX_PARAM_AMBIENT_LIGHT_COLOR,Color(0.15,0.15,0.15));
  2640. settings_ambient_color->set_color(Color(0.15,0.15,0.15));
  2641. settings_fov = memnew( SpinBox );
  2642. settings_fov->set_max(179);
  2643. settings_fov->set_min(1);
  2644. settings_fov->set_step(0.01);
  2645. settings_fov->set_val(EDITOR_DEF("3d_editor/default_fov",60.0));
  2646. settings_vbc->add_margin_child("Perspective FOV (deg.):",settings_fov);
  2647. settings_znear = memnew( SpinBox );
  2648. settings_znear->set_max(10000);
  2649. settings_znear->set_min(0.1);
  2650. settings_znear->set_step(0.01);
  2651. settings_znear->set_val(EDITOR_DEF("3d_editor/default_z_near",0.1));
  2652. settings_vbc->add_margin_child("View Z-Near:",settings_znear);
  2653. settings_zfar = memnew( SpinBox );
  2654. settings_zfar->set_max(10000);
  2655. settings_zfar->set_min(0.1);
  2656. settings_zfar->set_step(0.01);
  2657. settings_zfar->set_val(EDITOR_DEF("3d_editor/default_z_far",1500));
  2658. settings_vbc->add_margin_child("View Z-Far:",settings_zfar);
  2659. //settings_dialog->get_cancel()->hide();
  2660. /* XFORM DIALOG */
  2661. xform_dialog = memnew( ConfirmationDialog );
  2662. xform_dialog->set_title("Transform Change");
  2663. add_child(xform_dialog);
  2664. l = memnew(Label);
  2665. l->set_text("Translate:");
  2666. l->set_pos(Point2(5,5));
  2667. xform_dialog->add_child(l);
  2668. for(int i=0;i<3;i++) {
  2669. xform_translate[i] = memnew( LineEdit );
  2670. xform_translate[i]->set_pos( Point2(15+i*60,22) );
  2671. xform_translate[i]->set_size( Size2(50,12 ) );
  2672. xform_dialog->add_child( xform_translate[i] );
  2673. }
  2674. l = memnew(Label);
  2675. l->set_text("Rotate (deg.):");
  2676. l->set_pos(Point2(5,45));
  2677. xform_dialog->add_child(l);
  2678. for(int i=0;i<3;i++) {
  2679. xform_rotate[i] = memnew( LineEdit );
  2680. xform_rotate[i]->set_pos( Point2(15+i*60,62) );
  2681. xform_rotate[i]->set_size( Size2(50,22 ) );
  2682. xform_dialog->add_child(xform_rotate[i]);
  2683. }
  2684. l = memnew(Label);
  2685. l->set_text("Scale (ratio):");
  2686. l->set_pos(Point2(5,85));
  2687. xform_dialog->add_child(l);
  2688. for(int i=0;i<3;i++) {
  2689. xform_scale[i] = memnew( LineEdit );
  2690. xform_scale[i]->set_pos( Point2(15+i*60,102) );
  2691. xform_scale[i]->set_size( Size2(50,22 ) );
  2692. xform_dialog->add_child(xform_scale[i]);
  2693. }
  2694. l = memnew(Label);
  2695. l->set_text("Transform Type");
  2696. l->set_pos(Point2(5,125));
  2697. xform_dialog->add_child(l);
  2698. xform_type = memnew( OptionButton );
  2699. xform_type->set_anchor( MARGIN_RIGHT, ANCHOR_END );
  2700. xform_type->set_begin( Point2(15,142) );
  2701. xform_type->set_end( Point2(15,75) );
  2702. xform_type->add_item("Pre");
  2703. xform_type->add_item("Post");
  2704. xform_dialog->add_child(xform_type);
  2705. xform_dialog->connect("confirmed", this,"_xform_dialog_action");
  2706. scenario_debug=VisualServer::SCENARIO_DEBUG_DISABLED;
  2707. selected=NULL;
  2708. set_process_unhandled_key_input(true);
  2709. add_to_group("_spatial_editor_group");
  2710. EDITOR_DEF("3d_editor/manipulator_gizmo_size",80);
  2711. EditorSettings::get_singleton()->add_property_hint(PropertyInfo(Variant::INT,"3d_editor/manipulator_gizmo_size",PROPERTY_HINT_RANGE,"16,1024,1"));
  2712. EDITOR_DEF("3d_editor/manipulator_gizmo_opacity",0.2);
  2713. over_gizmo_handle=-1;
  2714. }
  2715. SpatialEditor::~SpatialEditor() {
  2716. }
  2717. void SpatialEditorPlugin::make_visible(bool p_visible) {
  2718. if (p_visible) {
  2719. spatial_editor->show();
  2720. spatial_editor->set_process(true);
  2721. //VisualServer::get_singleton()->viewport_set_hide_scenario(editor->get_scene_root()->get_viewport(),false);
  2722. spatial_editor->grab_focus();
  2723. } else {
  2724. spatial_editor->hide();
  2725. spatial_editor->set_process(false);
  2726. //VisualServer::get_singleton()->viewport_set_hide_scenario(editor->get_scene_root()->get_viewport(),true);
  2727. }
  2728. }
  2729. void SpatialEditorPlugin::edit(Object *p_object) {
  2730. spatial_editor->edit(p_object->cast_to<Spatial>());
  2731. }
  2732. bool SpatialEditorPlugin::handles(Object *p_object) const {
  2733. return p_object->is_type("Spatial");
  2734. }
  2735. Dictionary SpatialEditorPlugin::get_state() const {
  2736. return spatial_editor->get_state();
  2737. }
  2738. void SpatialEditorPlugin::set_state(const Dictionary& p_state) {
  2739. spatial_editor->set_state(p_state);
  2740. }
  2741. void SpatialEditor::snap_cursor_to_plane(const Plane& p_plane) {
  2742. // cursor.pos=p_plane.project(cursor.pos);
  2743. }
  2744. void SpatialEditorPlugin::_bind_methods() {
  2745. ObjectTypeDB::bind_method("snap_cursor_to_plane",&SpatialEditorPlugin::snap_cursor_to_plane);
  2746. }
  2747. void SpatialEditorPlugin::snap_cursor_to_plane(const Plane& p_plane) {
  2748. spatial_editor->snap_cursor_to_plane(p_plane);
  2749. }
  2750. SpatialEditorPlugin::SpatialEditorPlugin(EditorNode *p_node) {
  2751. editor=p_node;
  2752. spatial_editor = memnew( SpatialEditor(p_node) );
  2753. editor->get_viewport()->add_child(spatial_editor);
  2754. spatial_editor->set_area_as_parent_rect();
  2755. spatial_editor->hide();
  2756. spatial_editor->connect("transform_key_request",editor,"_transform_keyed");
  2757. //spatial_editor->set_process(true);
  2758. }
  2759. SpatialEditorPlugin::~SpatialEditorPlugin() {
  2760. }