2
0

spatial_editor_gizmos.cpp 125 KB

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