spatial_editor_gizmos.cpp 80 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967
  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-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 "geometry.h"
  32. #include "quick_hull.h"
  33. #include "scene/3d/camera.h"
  34. #include "scene/resources/box_shape.h"
  35. #include "scene/resources/capsule_shape.h"
  36. #include "scene/resources/convex_polygon_shape.h"
  37. #include "scene/resources/plane_shape.h"
  38. #include "scene/resources/ray_shape.h"
  39. #include "scene/resources/sphere_shape.h"
  40. #include "scene/resources/surface_tool.h"
  41. // Keep small children away from this file.
  42. // It's so ugly it will eat them alive
  43. #define HANDLE_HALF_SIZE 0.05
  44. void EditorSpatialGizmo::clear() {
  45. for (int i = 0; i < instances.size(); i++) {
  46. if (instances[i].instance.is_valid())
  47. VS::get_singleton()->free(instances[i].instance);
  48. }
  49. billboard_handle = false;
  50. collision_segments.clear();
  51. collision_mesh = Ref<TriangleMesh>();
  52. instances.clear();
  53. handles.clear();
  54. secondary_handles.clear();
  55. }
  56. void EditorSpatialGizmo::redraw() {
  57. if (get_script_instance() && get_script_instance()->has_method("redraw"))
  58. get_script_instance()->call("redraw");
  59. }
  60. void EditorSpatialGizmo::Instance::create_instance(Spatial *p_base) {
  61. instance = VS::get_singleton()->instance_create2(mesh->get_rid(), p_base->get_world()->get_scenario());
  62. VS::get_singleton()->instance_attach_object_instance_ID(instance, p_base->get_instance_ID());
  63. if (billboard)
  64. VS::get_singleton()->instance_geometry_set_flag(instance, VS::INSTANCE_FLAG_BILLBOARD, true);
  65. if (unscaled)
  66. VS::get_singleton()->instance_geometry_set_flag(instance, VS::INSTANCE_FLAG_DEPH_SCALE, true);
  67. if (skeleton.is_valid())
  68. VS::get_singleton()->instance_attach_skeleton(instance, skeleton);
  69. if (extra_margin)
  70. VS::get_singleton()->instance_set_extra_visibility_margin(instance, 1);
  71. VS::get_singleton()->instance_geometry_set_cast_shadows_setting(instance, VS::SHADOW_CASTING_SETTING_OFF);
  72. VS::get_singleton()->instance_geometry_set_flag(instance, VS::INSTANCE_FLAG_RECEIVE_SHADOWS, false);
  73. VS::get_singleton()->instance_set_layer_mask(instance, 1 << SpatialEditorViewport::GIZMO_EDIT_LAYER); //gizmos are 26
  74. }
  75. void EditorSpatialGizmo::add_mesh(const Ref<Mesh> &p_mesh, bool p_billboard, const RID &p_skeleton) {
  76. ERR_FAIL_COND(!spatial_node);
  77. Instance ins;
  78. ins.billboard = p_billboard;
  79. ins.mesh = p_mesh;
  80. ins.skeleton = p_skeleton;
  81. if (valid) {
  82. ins.create_instance(spatial_node);
  83. VS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  84. }
  85. instances.push_back(ins);
  86. }
  87. void EditorSpatialGizmo::add_lines(const Vector<Vector3> &p_lines, const Ref<Material> &p_material, bool p_billboard) {
  88. ERR_FAIL_COND(!spatial_node);
  89. Instance ins;
  90. Ref<Mesh> mesh = memnew(Mesh);
  91. Array a;
  92. a.resize(Mesh::ARRAY_MAX);
  93. a[Mesh::ARRAY_VERTEX] = p_lines;
  94. DVector<Color> color;
  95. color.resize(p_lines.size());
  96. {
  97. DVector<Color>::Write w = color.write();
  98. for (int i = 0; i < p_lines.size(); i++) {
  99. if (is_selected())
  100. w[i] = Color(1, 1, 1, 0.6);
  101. else
  102. w[i] = Color(1, 1, 1, 0.25);
  103. }
  104. }
  105. a[Mesh::ARRAY_COLOR] = color;
  106. mesh->add_surface(Mesh::PRIMITIVE_LINES, a);
  107. mesh->surface_set_material(0, p_material);
  108. if (p_billboard) {
  109. float md = 0;
  110. for (int i = 0; i < p_lines.size(); i++) {
  111. md = MAX(0, p_lines[i].length());
  112. }
  113. if (md) {
  114. mesh->set_custom_aabb(AABB(Vector3(-md, -md, -md), Vector3(md, md, md) * 2.0));
  115. }
  116. }
  117. ins.billboard = p_billboard;
  118. ins.mesh = mesh;
  119. if (valid) {
  120. ins.create_instance(spatial_node);
  121. VS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  122. }
  123. instances.push_back(ins);
  124. }
  125. void EditorSpatialGizmo::add_unscaled_billboard(const Ref<Material> &p_material, float p_scale) {
  126. ERR_FAIL_COND(!spatial_node);
  127. Instance ins;
  128. Vector<Vector3> vs;
  129. Vector<Vector2> uv;
  130. vs.push_back(Vector3(-p_scale, p_scale, 0));
  131. vs.push_back(Vector3(p_scale, p_scale, 0));
  132. vs.push_back(Vector3(p_scale, -p_scale, 0));
  133. vs.push_back(Vector3(-p_scale, -p_scale, 0));
  134. uv.push_back(Vector2(1, 0));
  135. uv.push_back(Vector2(0, 0));
  136. uv.push_back(Vector2(0, 1));
  137. uv.push_back(Vector2(1, 1));
  138. Ref<Mesh> mesh = memnew(Mesh);
  139. Array a;
  140. a.resize(Mesh::ARRAY_MAX);
  141. a[Mesh::ARRAY_VERTEX] = vs;
  142. a[Mesh::ARRAY_TEX_UV] = uv;
  143. mesh->add_surface(Mesh::PRIMITIVE_TRIANGLE_FAN, a);
  144. mesh->surface_set_material(0, p_material);
  145. if (true) {
  146. float md = 0;
  147. for (int i = 0; i < vs.size(); i++) {
  148. md = MAX(0, vs[i].length());
  149. }
  150. if (md) {
  151. mesh->set_custom_aabb(AABB(Vector3(-md, -md, -md), Vector3(md, md, md) * 2.0));
  152. }
  153. }
  154. ins.mesh = mesh;
  155. ins.unscaled = true;
  156. ins.billboard = true;
  157. if (valid) {
  158. ins.create_instance(spatial_node);
  159. VS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  160. }
  161. instances.push_back(ins);
  162. }
  163. void EditorSpatialGizmo::add_collision_triangles(const Ref<TriangleMesh> &p_tmesh) {
  164. collision_mesh = p_tmesh;
  165. }
  166. void EditorSpatialGizmo::add_collision_segments(const Vector<Vector3> &p_lines) {
  167. int from = collision_segments.size();
  168. collision_segments.resize(from + p_lines.size());
  169. for (int i = 0; i < p_lines.size(); i++) {
  170. collision_segments[from + i] = p_lines[i];
  171. }
  172. }
  173. void EditorSpatialGizmo::add_handles(const Vector<Vector3> &p_handles, bool p_billboard, bool p_secondary) {
  174. billboard_handle = p_billboard;
  175. if (!is_selected())
  176. return;
  177. ERR_FAIL_COND(!spatial_node);
  178. ERR_FAIL_COND(!spatial_node);
  179. Instance ins;
  180. Ref<Mesh> mesh = memnew(Mesh);
  181. #if 1
  182. Array a;
  183. a.resize(VS::ARRAY_MAX);
  184. a[VS::ARRAY_VERTEX] = p_handles;
  185. DVector<Color> colors;
  186. {
  187. colors.resize(p_handles.size());
  188. DVector<Color>::Write w = colors.write();
  189. for (int i = 0; i < p_handles.size(); i++) {
  190. Color col(1, 1, 1, 1);
  191. if (SpatialEditor::get_singleton()->get_over_gizmo_handle() != i)
  192. col = Color(0.9, 0.9, 0.9, 0.9);
  193. w[i] = col;
  194. }
  195. }
  196. a[VS::ARRAY_COLOR] = colors;
  197. mesh->add_surface(Mesh::PRIMITIVE_POINTS, a);
  198. mesh->surface_set_material(0, SpatialEditorGizmos::singleton->handle2_material);
  199. if (p_billboard) {
  200. float md = 0;
  201. for (int i = 0; i < p_handles.size(); i++) {
  202. md = MAX(0, p_handles[i].length());
  203. }
  204. if (md) {
  205. mesh->set_custom_aabb(AABB(Vector3(-md, -md, -md), Vector3(md, md, md) * 2.0));
  206. }
  207. }
  208. #else
  209. for (int ih = 0; ih < p_handles.size(); ih++) {
  210. Vector<Vector3> vertices;
  211. Vector<Vector3> normals;
  212. int vtx_idx = 0;
  213. #define ADD_VTX(m_idx) \
  214. vertices.push_back((face_points[m_idx] * HANDLE_HALF_SIZE + p_handles[ih])); \
  215. normals.push_back(normal_points[m_idx]); \
  216. vtx_idx++;
  217. for (int i = 0; i < 6; i++) {
  218. Vector3 face_points[4];
  219. Vector3 normal_points[4];
  220. float uv_points[8] = { 0, 0, 0, 1, 1, 1, 1, 0 };
  221. for (int j = 0; j < 4; j++) {
  222. float v[3];
  223. v[0] = 1.0;
  224. v[1] = 1 - 2 * ((j >> 1) & 1);
  225. v[2] = v[1] * (1 - 2 * (j & 1));
  226. for (int k = 0; k < 3; k++) {
  227. if (i < 3)
  228. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  229. else
  230. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  231. }
  232. normal_points[j] = Vector3();
  233. normal_points[j][i % 3] = (i >= 3 ? -1 : 1);
  234. }
  235. //tri 1
  236. ADD_VTX(0);
  237. ADD_VTX(1);
  238. ADD_VTX(2);
  239. //tri 2
  240. ADD_VTX(2);
  241. ADD_VTX(3);
  242. ADD_VTX(0);
  243. }
  244. Array d;
  245. d.resize(VS::ARRAY_MAX);
  246. d[VisualServer::ARRAY_NORMAL] = normals;
  247. d[VisualServer::ARRAY_VERTEX] = vertices;
  248. mesh->add_surface(Mesh::PRIMITIVE_TRIANGLES, d);
  249. mesh->surface_set_material(ih, SpatialEditorGizmos::singleton->handle_material);
  250. }
  251. #endif
  252. ins.mesh = mesh;
  253. ins.billboard = p_billboard;
  254. ins.extra_margin = true;
  255. if (valid) {
  256. ins.create_instance(spatial_node);
  257. VS::get_singleton()->instance_set_transform(ins.instance, spatial_node->get_global_transform());
  258. }
  259. instances.push_back(ins);
  260. if (!p_secondary) {
  261. int chs = handles.size();
  262. handles.resize(chs + p_handles.size());
  263. for (int i = 0; i < p_handles.size(); i++) {
  264. handles[i + chs] = p_handles[i];
  265. }
  266. } else {
  267. int chs = secondary_handles.size();
  268. secondary_handles.resize(chs + p_handles.size());
  269. for (int i = 0; i < p_handles.size(); i++) {
  270. secondary_handles[i + chs] = p_handles[i];
  271. }
  272. }
  273. }
  274. void EditorSpatialGizmo::set_spatial_node(Spatial *p_node) {
  275. ERR_FAIL_NULL(p_node);
  276. spatial_node = p_node;
  277. }
  278. bool EditorSpatialGizmo::intersect_frustum(const Camera *p_camera, const Vector<Plane> &p_frustum) {
  279. ERR_FAIL_COND_V(!spatial_node, false);
  280. ERR_FAIL_COND_V(!valid, false);
  281. if (collision_segments.size()) {
  282. const Plane *p = p_frustum.ptr();
  283. int fc = p_frustum.size();
  284. int vc = collision_segments.size();
  285. const Vector3 *vptr = collision_segments.ptr();
  286. Transform t = spatial_node->get_global_transform();
  287. for (int i = 0; i < vc / 2; i++) {
  288. Vector3 a = t.xform(vptr[i * 2 + 0]);
  289. Vector3 b = t.xform(vptr[i * 2 + 1]);
  290. bool any_out = false;
  291. for (int j = 0; j < fc; j++) {
  292. if (p[j].distance_to(a) > 0 && p[j].distance_to(b) > 0) {
  293. any_out = true;
  294. break;
  295. }
  296. }
  297. if (!any_out)
  298. return true;
  299. }
  300. return false;
  301. }
  302. return false;
  303. }
  304. bool EditorSpatialGizmo::intersect_ray(const Camera *p_camera, const Point2 &p_point, Vector3 &r_pos, Vector3 &r_normal, int *r_gizmo_handle, bool p_sec_first) {
  305. ERR_FAIL_COND_V(!spatial_node, false);
  306. ERR_FAIL_COND_V(!valid, false);
  307. if (r_gizmo_handle) {
  308. Transform t = spatial_node->get_global_transform();
  309. t.orthonormalize();
  310. if (billboard_handle) {
  311. t.set_look_at(t.origin, t.origin + p_camera->get_transform().basis.get_axis(2), p_camera->get_transform().basis.get_axis(1));
  312. }
  313. Transform ti = t.affine_inverse();
  314. float min_d = 1e20;
  315. int idx = -1;
  316. for (int i = 0; i < secondary_handles.size(); i++) {
  317. Vector3 hpos = t.xform(secondary_handles[i]);
  318. Vector2 p = p_camera->unproject_position(hpos);
  319. if (p.distance_to(p_point) < SpatialEditorGizmos::singleton->handle_t->get_width() * 0.6) {
  320. real_t dp = p_camera->get_transform().origin.distance_to(hpos);
  321. if (dp < min_d) {
  322. r_pos = t.xform(hpos);
  323. r_normal = p_camera->get_transform().basis.get_axis(2);
  324. min_d = dp;
  325. idx = i + handles.size();
  326. }
  327. }
  328. }
  329. if (p_sec_first && idx != -1) {
  330. *r_gizmo_handle = idx;
  331. return true;
  332. }
  333. min_d = 1e20;
  334. for (int i = 0; i < handles.size(); i++) {
  335. Vector3 hpos = t.xform(handles[i]);
  336. Vector2 p = p_camera->unproject_position(hpos);
  337. if (p.distance_to(p_point) < SpatialEditorGizmos::singleton->handle_t->get_width() * 0.6) {
  338. real_t dp = p_camera->get_transform().origin.distance_to(hpos);
  339. if (dp < min_d) {
  340. r_pos = t.xform(hpos);
  341. r_normal = p_camera->get_transform().basis.get_axis(2);
  342. min_d = dp;
  343. idx = i;
  344. }
  345. }
  346. }
  347. if (idx >= 0) {
  348. *r_gizmo_handle = idx;
  349. return true;
  350. }
  351. }
  352. if (collision_segments.size()) {
  353. Plane camp(p_camera->get_transform().origin, (-p_camera->get_transform().basis.get_axis(2)).normalized());
  354. int vc = collision_segments.size();
  355. const Vector3 *vptr = collision_segments.ptr();
  356. Transform t = spatial_node->get_global_transform();
  357. if (billboard_handle) {
  358. t.set_look_at(t.origin, t.origin + p_camera->get_transform().basis.get_axis(2), p_camera->get_transform().basis.get_axis(1));
  359. }
  360. Vector3 cp;
  361. float cpd = 1e20;
  362. for (int i = 0; i < vc / 2; i++) {
  363. Vector3 a = t.xform(vptr[i * 2 + 0]);
  364. Vector3 b = t.xform(vptr[i * 2 + 1]);
  365. Vector2 s[2];
  366. s[0] = p_camera->unproject_position(a);
  367. s[1] = p_camera->unproject_position(b);
  368. Vector2 p = Geometry::get_closest_point_to_segment_2d(p_point, s);
  369. float pd = p.distance_to(p_point);
  370. if (pd < cpd) {
  371. float d = s[0].distance_to(s[1]);
  372. Vector3 tcp;
  373. if (d > 0) {
  374. float d2 = s[0].distance_to(p) / d;
  375. tcp = a + (b - a) * d2;
  376. } else {
  377. tcp = a;
  378. }
  379. if (camp.distance_to(tcp) < p_camera->get_znear())
  380. continue;
  381. cp = tcp;
  382. cpd = pd;
  383. }
  384. }
  385. if (cpd < 8) {
  386. r_pos = cp;
  387. r_normal = -p_camera->project_ray_normal(p_point);
  388. return true;
  389. }
  390. return false;
  391. }
  392. if (collision_mesh.is_valid()) {
  393. Transform gt = spatial_node->get_global_transform();
  394. if (billboard_handle) {
  395. gt.set_look_at(gt.origin, gt.origin + p_camera->get_transform().basis.get_axis(2), p_camera->get_transform().basis.get_axis(1));
  396. }
  397. Transform ai = gt.affine_inverse();
  398. Vector3 ray_from = ai.xform(p_camera->project_ray_origin(p_point));
  399. Vector3 ray_dir = ai.basis.xform(p_camera->project_ray_normal(p_point)).normalized();
  400. Vector3 rpos, rnorm;
  401. if (collision_mesh->intersect_ray(ray_from, ray_dir, rpos, rnorm)) {
  402. r_pos = gt.xform(rpos);
  403. r_normal = gt.basis.xform(rnorm).normalized();
  404. return true;
  405. }
  406. }
  407. return false;
  408. }
  409. void EditorSpatialGizmo::create() {
  410. ERR_FAIL_COND(!spatial_node);
  411. ERR_FAIL_COND(valid);
  412. valid = true;
  413. for (int i = 0; i < instances.size(); i++) {
  414. instances[i].create_instance(spatial_node);
  415. }
  416. transform();
  417. }
  418. void EditorSpatialGizmo::transform() {
  419. ERR_FAIL_COND(!spatial_node);
  420. ERR_FAIL_COND(!valid);
  421. for (int i = 0; i < instances.size(); i++) {
  422. VS::get_singleton()->instance_set_transform(instances[i].instance, spatial_node->get_global_transform());
  423. }
  424. }
  425. void EditorSpatialGizmo::free() {
  426. ERR_FAIL_COND(!spatial_node);
  427. ERR_FAIL_COND(!valid);
  428. for (int i = 0; i < instances.size(); i++) {
  429. if (instances[i].instance.is_valid())
  430. VS::get_singleton()->free(instances[i].instance);
  431. instances[i].instance = RID();
  432. }
  433. valid = false;
  434. }
  435. void EditorSpatialGizmo::_bind_methods() {
  436. ObjectTypeDB::bind_method(_MD("add_lines", "lines", "material:Material", "billboard"), &EditorSpatialGizmo::add_lines, DEFVAL(false));
  437. ObjectTypeDB::bind_method(_MD("add_mesh", "mesh:Mesh", "billboard", "skeleton"), &EditorSpatialGizmo::add_mesh, DEFVAL(false), DEFVAL(RID()));
  438. ObjectTypeDB::bind_method(_MD("add_collision_segments", "segments"), &EditorSpatialGizmo::add_collision_segments);
  439. ObjectTypeDB::bind_method(_MD("add_collision_triangles", "triangles:TriangleMesh"), &EditorSpatialGizmo::add_collision_triangles);
  440. ObjectTypeDB::bind_method(_MD("add_unscaled_billboard", "material:Material", "default_scale"), &EditorSpatialGizmo::add_unscaled_billboard, DEFVAL(1));
  441. ObjectTypeDB::bind_method(_MD("add_handles", "handles", "billboard", "secondary"), &EditorSpatialGizmo::add_handles, DEFVAL(false), DEFVAL(false));
  442. ObjectTypeDB::bind_method(_MD("set_spatial_node", "node:Spatial"), &EditorSpatialGizmo::_set_spatial_node);
  443. ObjectTypeDB::bind_method(_MD("clear"), &EditorSpatialGizmo::clear);
  444. BIND_VMETHOD(MethodInfo("redraw"));
  445. BIND_VMETHOD(MethodInfo(Variant::STRING, "get_handle_name", PropertyInfo(Variant::INT, "index")));
  446. BIND_VMETHOD(MethodInfo("get_handle_value:Variant", PropertyInfo(Variant::INT, "index")));
  447. BIND_VMETHOD(MethodInfo("set_handle", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::OBJECT, "camera:Camera"), PropertyInfo(Variant::VECTOR2, "point")));
  448. MethodInfo cm = MethodInfo("commit_handle", PropertyInfo(Variant::INT, "index"), PropertyInfo(Variant::NIL, "restore:Variant"), PropertyInfo(Variant::BOOL, "cancel"));
  449. cm.default_arguments.push_back(false);
  450. BIND_VMETHOD(cm);
  451. }
  452. EditorSpatialGizmo::EditorSpatialGizmo() {
  453. valid = false;
  454. billboard_handle = false;
  455. base = NULL;
  456. spatial_node = NULL;
  457. }
  458. EditorSpatialGizmo::~EditorSpatialGizmo() {
  459. clear();
  460. }
  461. Vector3 EditorSpatialGizmo::get_handle_pos(int p_idx) const {
  462. ERR_FAIL_INDEX_V(p_idx, handles.size(), Vector3());
  463. return handles[p_idx];
  464. }
  465. //// light gizmo
  466. String LightSpatialGizmo::get_handle_name(int p_idx) const {
  467. if (p_idx == 0)
  468. return "Radius";
  469. else
  470. return "Aperture";
  471. }
  472. Variant LightSpatialGizmo::get_handle_value(int p_idx) const {
  473. if (p_idx == 0)
  474. return light->get_parameter(Light::PARAM_RADIUS);
  475. if (p_idx == 1)
  476. return light->get_parameter(Light::PARAM_SPOT_ANGLE);
  477. return Variant();
  478. }
  479. 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) {
  480. //bleh, discrete is simpler
  481. static const int arc_test_points = 64;
  482. float min_d = 1e20;
  483. Vector3 min_p;
  484. for (int i = 0; i < arc_test_points; i++) {
  485. float a = i * Math_PI * 0.5 / arc_test_points;
  486. float an = (i + 1) * Math_PI * 0.5 / arc_test_points;
  487. Vector3 p = Vector3(Math::cos(a), 0, -Math::sin(a)) * p_arc_radius;
  488. Vector3 n = Vector3(Math::cos(an), 0, -Math::sin(an)) * p_arc_radius;
  489. Vector3 ra, rb;
  490. Geometry::get_closest_points_between_segments(p, n, p_from, p_to, ra, rb);
  491. float d = ra.distance_to(rb);
  492. if (d < min_d) {
  493. min_d = d;
  494. min_p = ra;
  495. }
  496. }
  497. //min_p = p_arc_xform.affine_inverse().xform(min_p);
  498. float a = Vector2(min_p.x, -min_p.z).angle();
  499. return a * 180.0 / Math_PI;
  500. }
  501. void LightSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
  502. Transform gt = light->get_global_transform();
  503. gt.orthonormalize();
  504. Transform gi = gt.affine_inverse();
  505. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  506. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  507. Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  508. if (p_idx == 0) {
  509. if (light->cast_to<SpotLight>()) {
  510. Vector3 ra, rb;
  511. Geometry::get_closest_points_between_segments(Vector3(), Vector3(0, 0, -4096), s[0], s[1], ra, rb);
  512. float d = -ra.z;
  513. if (d < 0)
  514. d = 0;
  515. light->set_parameter(Light::PARAM_RADIUS, d);
  516. } else if (light->cast_to<OmniLight>()) {
  517. Plane cp = Plane(gt.origin, p_camera->get_transform().basis.get_axis(2));
  518. Vector3 inters;
  519. if (cp.intersects_ray(ray_from, ray_dir, &inters)) {
  520. float r = inters.distance_to(gt.origin);
  521. light->set_parameter(Light::PARAM_RADIUS, r);
  522. }
  523. }
  524. } else if (p_idx == 1) {
  525. float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], light->get_parameter(Light::PARAM_RADIUS), gt);
  526. light->set_parameter(Light::PARAM_SPOT_ANGLE, CLAMP(a, 0.01, 89.99));
  527. }
  528. }
  529. void LightSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
  530. if (p_cancel) {
  531. light->set_parameter(p_idx == 0 ? Light::PARAM_RADIUS : Light::PARAM_SPOT_ANGLE, p_restore);
  532. } else if (p_idx == 0) {
  533. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  534. ur->create_action(TTR("Change Light Radius"));
  535. ur->add_do_method(light, "set_parameter", Light::PARAM_RADIUS, light->get_parameter(Light::PARAM_RADIUS));
  536. ur->add_undo_method(light, "set_parameter", Light::PARAM_RADIUS, p_restore);
  537. ur->commit_action();
  538. } else if (p_idx == 1) {
  539. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  540. ur->create_action(TTR("Change Light Radius"));
  541. ur->add_do_method(light, "set_parameter", Light::PARAM_SPOT_ANGLE, light->get_parameter(Light::PARAM_SPOT_ANGLE));
  542. ur->add_undo_method(light, "set_parameter", Light::PARAM_SPOT_ANGLE, p_restore);
  543. ur->commit_action();
  544. }
  545. }
  546. void LightSpatialGizmo::redraw() {
  547. if (light->cast_to<DirectionalLight>()) {
  548. const int arrow_points = 5;
  549. Vector3 arrow[arrow_points] = {
  550. Vector3(0, 0, 2),
  551. Vector3(1, 1, 2),
  552. Vector3(1, 1, -1),
  553. Vector3(2, 2, -1),
  554. Vector3(0, 0, -3)
  555. };
  556. int arrow_sides = 4;
  557. Vector<Vector3> lines;
  558. for (int i = 0; i < arrow_sides; i++) {
  559. Matrix3 ma(Vector3(0, 0, 1), Math_PI * 2 * float(i) / arrow_sides);
  560. Matrix3 mb(Vector3(0, 0, 1), Math_PI * 2 * float(i + 1) / arrow_sides);
  561. for (int j = 1; j < arrow_points - 1; j++) {
  562. if (j != 2) {
  563. lines.push_back(ma.xform(arrow[j]));
  564. lines.push_back(ma.xform(arrow[j + 1]));
  565. }
  566. if (j < arrow_points - 1) {
  567. lines.push_back(ma.xform(arrow[j]));
  568. lines.push_back(mb.xform(arrow[j]));
  569. }
  570. }
  571. }
  572. add_lines(lines, SpatialEditorGizmos::singleton->light_material);
  573. add_collision_segments(lines);
  574. add_unscaled_billboard(SpatialEditorGizmos::singleton->light_material_directional_icon, 0.05);
  575. }
  576. if (light->cast_to<OmniLight>()) {
  577. clear();
  578. OmniLight *on = light->cast_to<OmniLight>();
  579. float r = on->get_parameter(Light::PARAM_RADIUS);
  580. Vector<Vector3> points;
  581. for (int i = 0; i <= 360; i++) {
  582. float ra = Math::deg2rad(i);
  583. float rb = Math::deg2rad(i + 1);
  584. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  585. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  586. /*points.push_back(Vector3(a.x,0,a.y));
  587. points.push_back(Vector3(b.x,0,b.y));
  588. points.push_back(Vector3(0,a.x,a.y));
  589. points.push_back(Vector3(0,b.x,b.y));*/
  590. points.push_back(Vector3(a.x, a.y, 0));
  591. points.push_back(Vector3(b.x, b.y, 0));
  592. }
  593. add_lines(points, SpatialEditorGizmos::singleton->light_material, true);
  594. add_collision_segments(points);
  595. add_unscaled_billboard(SpatialEditorGizmos::singleton->light_material_omni_icon, 0.05);
  596. Vector<Vector3> handles;
  597. handles.push_back(Vector3(r, 0, 0));
  598. add_handles(handles, true);
  599. }
  600. if (light->cast_to<SpotLight>()) {
  601. clear();
  602. Vector<Vector3> points;
  603. SpotLight *on = light->cast_to<SpotLight>();
  604. float r = on->get_parameter(Light::PARAM_RADIUS);
  605. float w = r * Math::sin(Math::deg2rad(on->get_parameter(Light::PARAM_SPOT_ANGLE)));
  606. float d = r * Math::cos(Math::deg2rad(on->get_parameter(Light::PARAM_SPOT_ANGLE)));
  607. for (int i = 0; i < 360; i++) {
  608. float ra = Math::deg2rad(i);
  609. float rb = Math::deg2rad(i + 1);
  610. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;
  611. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;
  612. /*points.push_back(Vector3(a.x,0,a.y));
  613. points.push_back(Vector3(b.x,0,b.y));
  614. points.push_back(Vector3(0,a.x,a.y));
  615. points.push_back(Vector3(0,b.x,b.y));*/
  616. points.push_back(Vector3(a.x, a.y, -d));
  617. points.push_back(Vector3(b.x, b.y, -d));
  618. if (i % 90 == 0) {
  619. points.push_back(Vector3(a.x, a.y, -d));
  620. points.push_back(Vector3());
  621. }
  622. }
  623. points.push_back(Vector3(0, 0, -r));
  624. points.push_back(Vector3());
  625. add_lines(points, SpatialEditorGizmos::singleton->light_material);
  626. Vector<Vector3> handles;
  627. handles.push_back(Vector3(0, 0, -r));
  628. Vector<Vector3> collision_segments;
  629. for (int i = 0; i < 64; i++) {
  630. float ra = i * Math_PI * 2.0 / 64.0;
  631. float rb = (i + 1) * Math_PI * 2.0 / 64.0;
  632. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;
  633. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;
  634. collision_segments.push_back(Vector3(a.x, a.y, -d));
  635. collision_segments.push_back(Vector3(b.x, b.y, -d));
  636. if (i % 16 == 0) {
  637. collision_segments.push_back(Vector3(a.x, a.y, -d));
  638. collision_segments.push_back(Vector3());
  639. }
  640. if (i == 16) {
  641. handles.push_back(Vector3(a.x, a.y, -d));
  642. }
  643. }
  644. collision_segments.push_back(Vector3(0, 0, -r));
  645. collision_segments.push_back(Vector3());
  646. add_handles(handles);
  647. add_collision_segments(collision_segments);
  648. add_unscaled_billboard(SpatialEditorGizmos::singleton->light_material_omni_icon, 0.05);
  649. }
  650. }
  651. LightSpatialGizmo::LightSpatialGizmo(Light *p_light) {
  652. light = p_light;
  653. set_spatial_node(p_light);
  654. }
  655. //////
  656. void ListenerSpatialGizmo::redraw() {
  657. clear();
  658. add_unscaled_billboard(SpatialEditorGizmos::singleton->listener_icon, 0.05);
  659. add_mesh(SpatialEditorGizmos::singleton->listener_line_mesh);
  660. Vector<Vector3> cursor_points;
  661. cursor_points.push_back(Vector3(0, 0, 0));
  662. cursor_points.push_back(Vector3(0, 0, -1.0));
  663. add_collision_segments(cursor_points);
  664. }
  665. ListenerSpatialGizmo::ListenerSpatialGizmo(Listener *p_listener) {
  666. set_spatial_node(p_listener);
  667. listener = p_listener;
  668. }
  669. //////
  670. String CameraSpatialGizmo::get_handle_name(int p_idx) const {
  671. if (camera->get_projection() == Camera::PROJECTION_PERSPECTIVE) {
  672. return "FOV";
  673. } else {
  674. return "Size";
  675. }
  676. }
  677. Variant CameraSpatialGizmo::get_handle_value(int p_idx) const {
  678. if (camera->get_projection() == Camera::PROJECTION_PERSPECTIVE) {
  679. return camera->get_fov();
  680. } else {
  681. return camera->get_size();
  682. }
  683. }
  684. void CameraSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
  685. Transform gt = camera->get_global_transform();
  686. gt.orthonormalize();
  687. Transform gi = gt.affine_inverse();
  688. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  689. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  690. Vector3 s[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  691. if (camera->get_projection() == Camera::PROJECTION_PERSPECTIVE) {
  692. Transform gt = camera->get_global_transform();
  693. float a = _find_closest_angle_to_half_pi_arc(s[0], s[1], 1.0, gt);
  694. camera->set("fov", a);
  695. } else {
  696. Vector3 ra, rb;
  697. Geometry::get_closest_points_between_segments(Vector3(0, 0, -1), Vector3(4096, 0, -1), s[0], s[1], ra, rb);
  698. float d = ra.x * 2.0;
  699. if (d < 0)
  700. d = 0;
  701. camera->set("size", d);
  702. }
  703. }
  704. void CameraSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
  705. if (camera->get_projection() == Camera::PROJECTION_PERSPECTIVE) {
  706. if (p_cancel) {
  707. camera->set("fov", p_restore);
  708. } else {
  709. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  710. ur->create_action(TTR("Change Camera FOV"));
  711. ur->add_do_property(camera, "fov", camera->get_fov());
  712. ur->add_undo_property(camera, "fov", p_restore);
  713. ur->commit_action();
  714. }
  715. } else {
  716. if (p_cancel) {
  717. camera->set("size", p_restore);
  718. } else {
  719. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  720. ur->create_action(TTR("Change Camera Size"));
  721. ur->add_do_property(camera, "size", camera->get_size());
  722. ur->add_undo_property(camera, "size", p_restore);
  723. ur->commit_action();
  724. }
  725. }
  726. }
  727. void CameraSpatialGizmo::redraw() {
  728. clear();
  729. Vector<Vector3> lines;
  730. Vector<Vector3> handles;
  731. switch (camera->get_projection()) {
  732. case Camera::PROJECTION_PERSPECTIVE: {
  733. float fov = camera->get_fov();
  734. Vector3 side = Vector3(Math::sin(Math::deg2rad(fov)), 0, -Math::cos(Math::deg2rad(fov)));
  735. Vector3 nside = side;
  736. nside.x = -nside.x;
  737. Vector3 up = Vector3(0, side.x, 0);
  738. #define ADD_TRIANGLE(m_a, m_b, m_c) \
  739. { \
  740. lines.push_back(m_a); \
  741. lines.push_back(m_b); \
  742. lines.push_back(m_b); \
  743. lines.push_back(m_c); \
  744. lines.push_back(m_c); \
  745. lines.push_back(m_a); \
  746. }
  747. ADD_TRIANGLE(Vector3(), side + up, side - up);
  748. ADD_TRIANGLE(Vector3(), nside + up, nside - up);
  749. ADD_TRIANGLE(Vector3(), side + up, nside + up);
  750. ADD_TRIANGLE(Vector3(), side - up, nside - up);
  751. handles.push_back(side);
  752. side.x *= 0.25;
  753. nside.x *= 0.25;
  754. Vector3 tup(0, up.y * 3 / 2, side.z);
  755. ADD_TRIANGLE(tup, side + up, nside + up);
  756. } break;
  757. case Camera::PROJECTION_ORTHOGONAL: {
  758. #define ADD_QUAD(m_a, m_b, m_c, m_d) \
  759. { \
  760. lines.push_back(m_a); \
  761. lines.push_back(m_b); \
  762. lines.push_back(m_b); \
  763. lines.push_back(m_c); \
  764. lines.push_back(m_c); \
  765. lines.push_back(m_d); \
  766. lines.push_back(m_d); \
  767. lines.push_back(m_a); \
  768. }
  769. float size = camera->get_size();
  770. float hsize = size * 0.5;
  771. Vector3 right(hsize, 0, 0);
  772. Vector3 up(0, hsize, 0);
  773. Vector3 back(0, 0, -1.0);
  774. Vector3 front(0, 0, 0);
  775. ADD_QUAD(-up - right, -up + right, up + right, up - right);
  776. ADD_QUAD(-up - right + back, -up + right + back, up + right + back, up - right + back);
  777. ADD_QUAD(up + right, up + right + back, up - right + back, up - right);
  778. ADD_QUAD(-up + right, -up + right + back, -up - right + back, -up - right);
  779. handles.push_back(right + back);
  780. right.x *= 0.25;
  781. Vector3 tup(0, up.y * 3 / 2, back.z);
  782. ADD_TRIANGLE(tup, right + up + back, -right + up + back);
  783. } break;
  784. }
  785. add_lines(lines, SpatialEditorGizmos::singleton->camera_material);
  786. add_collision_segments(lines);
  787. add_handles(handles);
  788. }
  789. CameraSpatialGizmo::CameraSpatialGizmo(Camera *p_camera) {
  790. camera = p_camera;
  791. set_spatial_node(camera);
  792. }
  793. //////
  794. void MeshInstanceSpatialGizmo::redraw() {
  795. Ref<Mesh> m = mesh->get_mesh();
  796. if (!m.is_valid())
  797. return; //none
  798. Ref<TriangleMesh> tm = m->generate_triangle_mesh();
  799. if (tm.is_valid())
  800. add_collision_triangles(tm);
  801. }
  802. MeshInstanceSpatialGizmo::MeshInstanceSpatialGizmo(MeshInstance *p_mesh) {
  803. mesh = p_mesh;
  804. set_spatial_node(p_mesh);
  805. }
  806. /////
  807. void Position3DSpatialGizmo::redraw() {
  808. clear();
  809. add_mesh(SpatialEditorGizmos::singleton->pos3d_mesh);
  810. Vector<Vector3> cursor_points;
  811. float cs = 0.25;
  812. cursor_points.push_back(Vector3(+cs, 0, 0));
  813. cursor_points.push_back(Vector3(-cs, 0, 0));
  814. cursor_points.push_back(Vector3(0, +cs, 0));
  815. cursor_points.push_back(Vector3(0, -cs, 0));
  816. cursor_points.push_back(Vector3(0, 0, +cs));
  817. cursor_points.push_back(Vector3(0, 0, -cs));
  818. add_collision_segments(cursor_points);
  819. }
  820. Position3DSpatialGizmo::Position3DSpatialGizmo(Position3D *p_p3d) {
  821. p3d = p_p3d;
  822. set_spatial_node(p3d);
  823. }
  824. /////
  825. void SkeletonSpatialGizmo::redraw() {
  826. clear();
  827. Ref<SurfaceTool> surface_tool(memnew(SurfaceTool));
  828. surface_tool->begin(Mesh::PRIMITIVE_LINES);
  829. surface_tool->set_material(SpatialEditorGizmos::singleton->skeleton_material);
  830. Vector<Transform> grests;
  831. grests.resize(skel->get_bone_count());
  832. Vector<int> bones;
  833. Vector<float> weights;
  834. bones.resize(4);
  835. weights.resize(4);
  836. for (int i = 0; i < 4; i++) {
  837. bones[i] = 0;
  838. weights[i] = 0;
  839. }
  840. weights[0] = 1;
  841. AABB aabb;
  842. Color bonecolor = Color(1.0, 0.4, 0.4, 0.3);
  843. Color rootcolor = Color(0.4, 1.0, 0.4, 0.1);
  844. for (int i = 0; i < skel->get_bone_count(); i++) {
  845. int parent = skel->get_bone_parent(i);
  846. if (parent >= 0) {
  847. grests[i] = grests[parent] * skel->get_bone_rest(i);
  848. Vector3 v0 = grests[parent].origin;
  849. Vector3 v1 = grests[i].origin;
  850. Vector3 d = (v1 - v0).normalized();
  851. float dist = v0.distance_to(v1);
  852. //find closest axis
  853. int closest = -1;
  854. float closest_d = 0.0;
  855. for (int j = 0; j < 3; j++) {
  856. float dp = Math::abs(grests[parent].basis[j].normalized().dot(d));
  857. if (j == 0 || dp > closest_d)
  858. closest = j;
  859. }
  860. //find closest other
  861. Vector3 first;
  862. Vector3 points[4];
  863. int pointidx = 0;
  864. for (int j = 0; j < 3; j++) {
  865. bones[0] = parent;
  866. surface_tool->add_bones(bones);
  867. surface_tool->add_weights(weights);
  868. surface_tool->add_color(rootcolor);
  869. surface_tool->add_vertex(v0 - grests[parent].basis[j].normalized() * dist * 0.05);
  870. surface_tool->add_bones(bones);
  871. surface_tool->add_weights(weights);
  872. surface_tool->add_color(rootcolor);
  873. surface_tool->add_vertex(v0 + grests[parent].basis[j].normalized() * dist * 0.05);
  874. if (j == closest)
  875. continue;
  876. Vector3 axis;
  877. if (first == Vector3()) {
  878. axis = d.cross(d.cross(grests[parent].basis[j])).normalized();
  879. first = axis;
  880. } else {
  881. axis = d.cross(first).normalized();
  882. }
  883. for (int k = 0; k < 2; k++) {
  884. if (k == 1)
  885. axis = -axis;
  886. Vector3 point = v0 + d * dist * 0.2;
  887. point += axis * dist * 0.1;
  888. bones[0] = parent;
  889. surface_tool->add_bones(bones);
  890. surface_tool->add_weights(weights);
  891. surface_tool->add_color(bonecolor);
  892. surface_tool->add_vertex(v0);
  893. surface_tool->add_bones(bones);
  894. surface_tool->add_weights(weights);
  895. surface_tool->add_color(bonecolor);
  896. surface_tool->add_vertex(point);
  897. bones[0] = parent;
  898. surface_tool->add_bones(bones);
  899. surface_tool->add_weights(weights);
  900. surface_tool->add_color(bonecolor);
  901. surface_tool->add_vertex(point);
  902. bones[0] = i;
  903. surface_tool->add_bones(bones);
  904. surface_tool->add_weights(weights);
  905. surface_tool->add_color(bonecolor);
  906. surface_tool->add_vertex(v1);
  907. points[pointidx++] = point;
  908. }
  909. }
  910. SWAP(points[1], points[2]);
  911. for (int j = 0; j < 4; j++) {
  912. bones[0] = parent;
  913. surface_tool->add_bones(bones);
  914. surface_tool->add_weights(weights);
  915. surface_tool->add_color(bonecolor);
  916. surface_tool->add_vertex(points[j]);
  917. surface_tool->add_bones(bones);
  918. surface_tool->add_weights(weights);
  919. surface_tool->add_color(bonecolor);
  920. surface_tool->add_vertex(points[(j + 1) % 4]);
  921. }
  922. /*
  923. bones[0]=parent;
  924. surface_tool->add_bones(bones);
  925. surface_tool->add_weights(weights);
  926. surface_tool->add_color(Color(0.4,1,0.4,0.4));
  927. surface_tool->add_vertex(v0);
  928. bones[0]=i;
  929. surface_tool->add_bones(bones);
  930. surface_tool->add_weights(weights);
  931. surface_tool->add_color(Color(0.4,1,0.4,0.4));
  932. surface_tool->add_vertex(v1);
  933. */
  934. } else {
  935. grests[i] = skel->get_bone_rest(i);
  936. bones[0] = i;
  937. }
  938. /*
  939. Transform t = grests[i];
  940. t.orthonormalize();
  941. for (int i=0;i<6;i++) {
  942. Vector3 face_points[4];
  943. for (int j=0;j<4;j++) {
  944. float v[3];
  945. v[0]=1.0;
  946. v[1]=1-2*((j>>1)&1);
  947. v[2]=v[1]*(1-2*(j&1));
  948. for (int k=0;k<3;k++) {
  949. if (i<3)
  950. face_points[j][(i+k)%3]=v[k]*(i>=3?-1:1);
  951. else
  952. face_points[3-j][(i+k)%3]=v[k]*(i>=3?-1:1);
  953. }
  954. }
  955. for(int j=0;j<4;j++) {
  956. surface_tool->add_bones(bones);
  957. surface_tool->add_weights(weights);
  958. surface_tool->add_color(Color(1.0,0.4,0.4,0.4));
  959. surface_tool->add_vertex(t.xform(face_points[j]*0.04));
  960. surface_tool->add_bones(bones);
  961. surface_tool->add_weights(weights);
  962. surface_tool->add_color(Color(1.0,0.4,0.4,0.4));
  963. surface_tool->add_vertex(t.xform(face_points[(j+1)%4]*0.04));
  964. }
  965. }
  966. */
  967. }
  968. Ref<Mesh> m = surface_tool->commit();
  969. add_mesh(m, false, skel->get_skeleton());
  970. }
  971. SkeletonSpatialGizmo::SkeletonSpatialGizmo(Skeleton *p_skel) {
  972. skel = p_skel;
  973. set_spatial_node(p_skel);
  974. }
  975. /////
  976. void SpatialPlayerSpatialGizmo::redraw() {
  977. clear();
  978. if (splayer->cast_to<SpatialStreamPlayer>()) {
  979. add_unscaled_billboard(SpatialEditorGizmos::singleton->stream_player_icon, 0.05);
  980. } else if (splayer->cast_to<SpatialSamplePlayer>()) {
  981. add_unscaled_billboard(SpatialEditorGizmos::singleton->sample_player_icon, 0.05);
  982. }
  983. }
  984. SpatialPlayerSpatialGizmo::SpatialPlayerSpatialGizmo(SpatialPlayer *p_splayer) {
  985. set_spatial_node(p_splayer);
  986. splayer = p_splayer;
  987. }
  988. /////
  989. void RoomSpatialGizmo::redraw() {
  990. clear();
  991. Ref<RoomBounds> roomie = room->get_room();
  992. if (roomie.is_null())
  993. return;
  994. DVector<Face3> faces = roomie->get_geometry_hint();
  995. Vector<Vector3> lines;
  996. int fc = faces.size();
  997. DVector<Face3>::Read r = faces.read();
  998. Map<_EdgeKey, Vector3> edge_map;
  999. for (int i = 0; i < fc; i++) {
  1000. Vector3 fn = r[i].get_plane().normal;
  1001. for (int j = 0; j < 3; j++) {
  1002. _EdgeKey ek;
  1003. ek.from = r[i].vertex[j].snapped(CMP_EPSILON);
  1004. ek.to = r[i].vertex[(j + 1) % 3].snapped(CMP_EPSILON);
  1005. if (ek.from < ek.to)
  1006. SWAP(ek.from, ek.to);
  1007. Map<_EdgeKey, Vector3>::Element *E = edge_map.find(ek);
  1008. if (E) {
  1009. if (E->get().dot(fn) > 0.9) {
  1010. E->get() = Vector3();
  1011. }
  1012. } else {
  1013. edge_map[ek] = fn;
  1014. }
  1015. }
  1016. }
  1017. for (Map<_EdgeKey, Vector3>::Element *E = edge_map.front(); E; E = E->next()) {
  1018. if (E->get() != Vector3()) {
  1019. lines.push_back(E->key().from);
  1020. lines.push_back(E->key().to);
  1021. }
  1022. }
  1023. add_lines(lines, SpatialEditorGizmos::singleton->room_material);
  1024. add_collision_segments(lines);
  1025. }
  1026. RoomSpatialGizmo::RoomSpatialGizmo(Room *p_room) {
  1027. set_spatial_node(p_room);
  1028. room = p_room;
  1029. }
  1030. /////
  1031. void PortalSpatialGizmo::redraw() {
  1032. clear();
  1033. Vector<Point2> points = portal->get_shape();
  1034. if (points.size() == 0) {
  1035. return;
  1036. }
  1037. Vector<Vector3> lines;
  1038. Vector3 center;
  1039. for (int i = 0; i < points.size(); i++) {
  1040. Vector3 f;
  1041. f.x = points[i].x;
  1042. f.y = points[i].y;
  1043. Vector3 fn;
  1044. fn.x = points[(i + 1) % points.size()].x;
  1045. fn.y = points[(i + 1) % points.size()].y;
  1046. center += f;
  1047. lines.push_back(f);
  1048. lines.push_back(fn);
  1049. }
  1050. center /= points.size();
  1051. lines.push_back(center);
  1052. lines.push_back(center + Vector3(0, 0, 1));
  1053. add_lines(lines, SpatialEditorGizmos::singleton->portal_material);
  1054. add_collision_segments(lines);
  1055. }
  1056. PortalSpatialGizmo::PortalSpatialGizmo(Portal *p_portal) {
  1057. set_spatial_node(p_portal);
  1058. portal = p_portal;
  1059. }
  1060. /////
  1061. void RayCastSpatialGizmo::redraw() {
  1062. clear();
  1063. Vector<Vector3> lines;
  1064. lines.push_back(Vector3());
  1065. lines.push_back(raycast->get_cast_to());
  1066. add_lines(lines, SpatialEditorGizmos::singleton->raycast_material);
  1067. add_collision_segments(lines);
  1068. }
  1069. RayCastSpatialGizmo::RayCastSpatialGizmo(RayCast *p_raycast) {
  1070. set_spatial_node(p_raycast);
  1071. raycast = p_raycast;
  1072. }
  1073. /////
  1074. void VehicleWheelSpatialGizmo::redraw() {
  1075. clear();
  1076. Vector<Vector3> points;
  1077. float r = car_wheel->get_radius();
  1078. const int skip = 10;
  1079. for (int i = 0; i <= 360; i += skip) {
  1080. float ra = Math::deg2rad(i);
  1081. float rb = Math::deg2rad(i + skip);
  1082. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  1083. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  1084. points.push_back(Vector3(0, a.x, a.y));
  1085. points.push_back(Vector3(0, b.x, b.y));
  1086. const int springsec = 4;
  1087. for (int j = 0; j < springsec; j++) {
  1088. float t = car_wheel->get_suspension_rest_length() * 5;
  1089. points.push_back(Vector3(a.x, i / 360.0 * t / springsec + j * (t / springsec), a.y) * 0.2);
  1090. points.push_back(Vector3(b.x, (i + skip) / 360.0 * t / springsec + j * (t / springsec), b.y) * 0.2);
  1091. }
  1092. }
  1093. //travel
  1094. points.push_back(Vector3(0, 0, 0));
  1095. points.push_back(Vector3(0, car_wheel->get_suspension_rest_length(), 0));
  1096. //axis
  1097. points.push_back(Vector3(r * 0.2, car_wheel->get_suspension_rest_length(), 0));
  1098. points.push_back(Vector3(-r * 0.2, car_wheel->get_suspension_rest_length(), 0));
  1099. //axis
  1100. points.push_back(Vector3(r * 0.2, 0, 0));
  1101. points.push_back(Vector3(-r * 0.2, 0, 0));
  1102. //forward line
  1103. points.push_back(Vector3(0, -r, 0));
  1104. points.push_back(Vector3(0, -r, r * 2));
  1105. points.push_back(Vector3(0, -r, r * 2));
  1106. points.push_back(Vector3(r * 2 * 0.2, -r, r * 2 * 0.8));
  1107. points.push_back(Vector3(0, -r, r * 2));
  1108. points.push_back(Vector3(-r * 2 * 0.2, -r, r * 2 * 0.8));
  1109. add_lines(points, SpatialEditorGizmos::singleton->car_wheel_material);
  1110. add_collision_segments(points);
  1111. }
  1112. VehicleWheelSpatialGizmo::VehicleWheelSpatialGizmo(VehicleWheel *p_car_wheel) {
  1113. set_spatial_node(p_car_wheel);
  1114. car_wheel = p_car_wheel;
  1115. }
  1116. ///
  1117. void TestCubeSpatialGizmo::redraw() {
  1118. clear();
  1119. add_collision_triangles(SpatialEditorGizmos::singleton->test_cube_tm);
  1120. }
  1121. TestCubeSpatialGizmo::TestCubeSpatialGizmo(TestCube *p_tc) {
  1122. tc = p_tc;
  1123. set_spatial_node(p_tc);
  1124. }
  1125. ///////////
  1126. String CollisionShapeSpatialGizmo::get_handle_name(int p_idx) const {
  1127. Ref<Shape> s = cs->get_shape();
  1128. if (s.is_null())
  1129. return "";
  1130. if (s->cast_to<SphereShape>()) {
  1131. return "Radius";
  1132. }
  1133. if (s->cast_to<BoxShape>()) {
  1134. return "Extents";
  1135. }
  1136. if (s->cast_to<CapsuleShape>()) {
  1137. return p_idx == 0 ? "Radius" : "Height";
  1138. }
  1139. if (s->cast_to<RayShape>()) {
  1140. return "Length";
  1141. }
  1142. return "";
  1143. }
  1144. Variant CollisionShapeSpatialGizmo::get_handle_value(int p_idx) const {
  1145. Ref<Shape> s = cs->get_shape();
  1146. if (s.is_null())
  1147. return Variant();
  1148. if (s->cast_to<SphereShape>()) {
  1149. Ref<SphereShape> ss = s;
  1150. return ss->get_radius();
  1151. }
  1152. if (s->cast_to<BoxShape>()) {
  1153. Ref<BoxShape> bs = s;
  1154. return bs->get_extents();
  1155. }
  1156. if (s->cast_to<CapsuleShape>()) {
  1157. Ref<CapsuleShape> cs = s;
  1158. return p_idx == 0 ? cs->get_radius() : cs->get_height();
  1159. }
  1160. if (s->cast_to<RayShape>()) {
  1161. Ref<RayShape> cs = s;
  1162. return cs->get_length();
  1163. }
  1164. return Variant();
  1165. }
  1166. void CollisionShapeSpatialGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
  1167. Ref<Shape> s = cs->get_shape();
  1168. if (s.is_null())
  1169. return;
  1170. Transform gt = cs->get_global_transform();
  1171. gt.orthonormalize();
  1172. Transform gi = gt.affine_inverse();
  1173. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1174. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1175. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  1176. if (s->cast_to<SphereShape>()) {
  1177. Ref<SphereShape> ss = s;
  1178. Vector3 ra, rb;
  1179. Geometry::get_closest_points_between_segments(Vector3(), Vector3(4096, 0, 0), sg[0], sg[1], ra, rb);
  1180. float d = ra.x;
  1181. if (d < 0.001)
  1182. d = 0.001;
  1183. ss->set_radius(d);
  1184. }
  1185. if (s->cast_to<RayShape>()) {
  1186. Ref<RayShape> rs = s;
  1187. Vector3 ra, rb;
  1188. Geometry::get_closest_points_between_segments(Vector3(), Vector3(0, 0, 4096), sg[0], sg[1], ra, rb);
  1189. float d = ra.z;
  1190. if (d < 0.001)
  1191. d = 0.001;
  1192. rs->set_length(d);
  1193. }
  1194. if (s->cast_to<BoxShape>()) {
  1195. Vector3 axis;
  1196. axis[p_idx] = 1.0;
  1197. Ref<BoxShape> bs = s;
  1198. Vector3 ra, rb;
  1199. Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  1200. float d = ra[p_idx];
  1201. if (d < 0.001)
  1202. d = 0.001;
  1203. Vector3 he = bs->get_extents();
  1204. he[p_idx] = d;
  1205. bs->set_extents(he);
  1206. }
  1207. if (s->cast_to<CapsuleShape>()) {
  1208. Vector3 axis;
  1209. axis[p_idx == 0 ? 0 : 2] = 1.0;
  1210. Ref<CapsuleShape> cs = s;
  1211. Vector3 ra, rb;
  1212. Geometry::get_closest_points_between_segments(Vector3(), axis * 4096, sg[0], sg[1], ra, rb);
  1213. float d = axis.dot(ra);
  1214. if (p_idx == 1)
  1215. d -= cs->get_radius();
  1216. if (d < 0.001)
  1217. d = 0.001;
  1218. if (p_idx == 0)
  1219. cs->set_radius(d);
  1220. else if (p_idx == 1)
  1221. cs->set_height(d * 2.0);
  1222. }
  1223. }
  1224. void CollisionShapeSpatialGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
  1225. Ref<Shape> s = cs->get_shape();
  1226. if (s.is_null())
  1227. return;
  1228. if (s->cast_to<SphereShape>()) {
  1229. Ref<SphereShape> ss = s;
  1230. if (p_cancel) {
  1231. ss->set_radius(p_restore);
  1232. return;
  1233. }
  1234. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1235. ur->create_action(TTR("Change Sphere Shape Radius"));
  1236. ur->add_do_method(ss.ptr(), "set_radius", ss->get_radius());
  1237. ur->add_undo_method(ss.ptr(), "set_radius", p_restore);
  1238. ur->commit_action();
  1239. }
  1240. if (s->cast_to<BoxShape>()) {
  1241. Ref<BoxShape> ss = s;
  1242. if (p_cancel) {
  1243. ss->set_extents(p_restore);
  1244. return;
  1245. }
  1246. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1247. ur->create_action(TTR("Change Box Shape Extents"));
  1248. ur->add_do_method(ss.ptr(), "set_extents", ss->get_extents());
  1249. ur->add_undo_method(ss.ptr(), "set_extents", p_restore);
  1250. ur->commit_action();
  1251. }
  1252. if (s->cast_to<CapsuleShape>()) {
  1253. Ref<CapsuleShape> ss = s;
  1254. if (p_cancel) {
  1255. if (p_idx == 0)
  1256. ss->set_radius(p_restore);
  1257. else
  1258. ss->set_height(p_restore);
  1259. return;
  1260. }
  1261. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1262. if (p_idx == 0) {
  1263. ur->create_action(TTR("Change Capsule Shape Radius"));
  1264. ur->add_do_method(ss.ptr(), "set_radius", ss->get_radius());
  1265. ur->add_undo_method(ss.ptr(), "set_radius", p_restore);
  1266. } else {
  1267. ur->create_action(TTR("Change Capsule Shape Height"));
  1268. ur->add_do_method(ss.ptr(), "set_height", ss->get_height());
  1269. ur->add_undo_method(ss.ptr(), "set_height", p_restore);
  1270. }
  1271. ur->commit_action();
  1272. }
  1273. if (s->cast_to<RayShape>()) {
  1274. Ref<RayShape> ss = s;
  1275. if (p_cancel) {
  1276. ss->set_length(p_restore);
  1277. return;
  1278. }
  1279. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1280. ur->create_action(TTR("Change Ray Shape Length"));
  1281. ur->add_do_method(ss.ptr(), "set_length", ss->get_length());
  1282. ur->add_undo_method(ss.ptr(), "set_length", p_restore);
  1283. ur->commit_action();
  1284. }
  1285. }
  1286. void CollisionShapeSpatialGizmo::redraw() {
  1287. clear();
  1288. Ref<Shape> s = cs->get_shape();
  1289. if (s.is_null())
  1290. return;
  1291. if (s->cast_to<SphereShape>()) {
  1292. Ref<SphereShape> sp = s;
  1293. float r = sp->get_radius();
  1294. Vector<Vector3> points;
  1295. for (int i = 0; i <= 360; i++) {
  1296. float ra = Math::deg2rad(i);
  1297. float rb = Math::deg2rad(i + 1);
  1298. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  1299. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  1300. points.push_back(Vector3(a.x, 0, a.y));
  1301. points.push_back(Vector3(b.x, 0, b.y));
  1302. points.push_back(Vector3(0, a.x, a.y));
  1303. points.push_back(Vector3(0, b.x, b.y));
  1304. points.push_back(Vector3(a.x, a.y, 0));
  1305. points.push_back(Vector3(b.x, b.y, 0));
  1306. }
  1307. Vector<Vector3> collision_segments;
  1308. for (int i = 0; i < 64; i++) {
  1309. float ra = i * Math_PI * 2.0 / 64.0;
  1310. float rb = (i + 1) * Math_PI * 2.0 / 64.0;
  1311. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * r;
  1312. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * r;
  1313. collision_segments.push_back(Vector3(a.x, 0, a.y));
  1314. collision_segments.push_back(Vector3(b.x, 0, b.y));
  1315. collision_segments.push_back(Vector3(0, a.x, a.y));
  1316. collision_segments.push_back(Vector3(0, b.x, b.y));
  1317. collision_segments.push_back(Vector3(a.x, a.y, 0));
  1318. collision_segments.push_back(Vector3(b.x, b.y, 0));
  1319. }
  1320. add_lines(points, SpatialEditorGizmos::singleton->shape_material);
  1321. add_collision_segments(collision_segments);
  1322. Vector<Vector3> handles;
  1323. handles.push_back(Vector3(r, 0, 0));
  1324. add_handles(handles);
  1325. }
  1326. if (s->cast_to<BoxShape>()) {
  1327. Ref<BoxShape> bs = s;
  1328. Vector<Vector3> lines;
  1329. AABB aabb;
  1330. aabb.pos = -bs->get_extents();
  1331. aabb.size = aabb.pos * -2;
  1332. for (int i = 0; i < 12; i++) {
  1333. Vector3 a, b;
  1334. aabb.get_edge(i, a, b);
  1335. lines.push_back(a);
  1336. lines.push_back(b);
  1337. }
  1338. Vector<Vector3> handles;
  1339. for (int i = 0; i < 3; i++) {
  1340. Vector3 ax;
  1341. ax[i] = bs->get_extents()[i];
  1342. handles.push_back(ax);
  1343. }
  1344. add_lines(lines, SpatialEditorGizmos::singleton->shape_material);
  1345. add_collision_segments(lines);
  1346. add_handles(handles);
  1347. }
  1348. if (s->cast_to<CapsuleShape>()) {
  1349. Ref<CapsuleShape> cs = s;
  1350. float radius = cs->get_radius();
  1351. float height = cs->get_height();
  1352. Vector<Vector3> points;
  1353. Vector3 d(0, 0, height * 0.5);
  1354. for (int i = 0; i < 360; i++) {
  1355. float ra = Math::deg2rad(i);
  1356. float rb = Math::deg2rad(i + 1);
  1357. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
  1358. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;
  1359. points.push_back(Vector3(a.x, a.y, 0) + d);
  1360. points.push_back(Vector3(b.x, b.y, 0) + d);
  1361. points.push_back(Vector3(a.x, a.y, 0) - d);
  1362. points.push_back(Vector3(b.x, b.y, 0) - d);
  1363. if (i % 90 == 0) {
  1364. points.push_back(Vector3(a.x, a.y, 0) + d);
  1365. points.push_back(Vector3(a.x, a.y, 0) - d);
  1366. }
  1367. Vector3 dud = i < 180 ? d : -d;
  1368. points.push_back(Vector3(0, a.y, a.x) + dud);
  1369. points.push_back(Vector3(0, b.y, b.x) + dud);
  1370. points.push_back(Vector3(a.y, 0, a.x) + dud);
  1371. points.push_back(Vector3(b.y, 0, b.x) + dud);
  1372. }
  1373. add_lines(points, SpatialEditorGizmos::singleton->shape_material);
  1374. Vector<Vector3> collision_segments;
  1375. for (int i = 0; i < 64; i++) {
  1376. float ra = i * Math_PI * 2.0 / 64.0;
  1377. float rb = (i + 1) * Math_PI * 2.0 / 64.0;
  1378. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * radius;
  1379. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * radius;
  1380. collision_segments.push_back(Vector3(a.x, a.y, 0) + d);
  1381. collision_segments.push_back(Vector3(b.x, b.y, 0) + d);
  1382. collision_segments.push_back(Vector3(a.x, a.y, 0) - d);
  1383. collision_segments.push_back(Vector3(b.x, b.y, 0) - d);
  1384. if (i % 16 == 0) {
  1385. collision_segments.push_back(Vector3(a.x, a.y, 0) + d);
  1386. collision_segments.push_back(Vector3(a.x, a.y, 0) - d);
  1387. }
  1388. Vector3 dud = i < 32 ? d : -d;
  1389. collision_segments.push_back(Vector3(0, a.y, a.x) + dud);
  1390. collision_segments.push_back(Vector3(0, b.y, b.x) + dud);
  1391. collision_segments.push_back(Vector3(a.y, 0, a.x) + dud);
  1392. collision_segments.push_back(Vector3(b.y, 0, b.x) + dud);
  1393. }
  1394. add_collision_segments(collision_segments);
  1395. Vector<Vector3> handles;
  1396. handles.push_back(Vector3(cs->get_radius(), 0, 0));
  1397. handles.push_back(Vector3(0, 0, cs->get_height() * 0.5 + cs->get_radius()));
  1398. add_handles(handles);
  1399. }
  1400. if (s->cast_to<PlaneShape>()) {
  1401. Ref<PlaneShape> ps = s;
  1402. Plane p = ps->get_plane();
  1403. Vector<Vector3> points;
  1404. Vector3 n1 = p.get_any_perpendicular_normal();
  1405. Vector3 n2 = p.normal.cross(n1).normalized();
  1406. Vector3 pface[4] = {
  1407. p.normal * p.d + n1 * 10.0 + n2 * 10.0,
  1408. p.normal * p.d + n1 * 10.0 + n2 * -10.0,
  1409. p.normal * p.d + n1 * -10.0 + n2 * -10.0,
  1410. p.normal * p.d + n1 * -10.0 + n2 * 10.0,
  1411. };
  1412. points.push_back(pface[0]);
  1413. points.push_back(pface[1]);
  1414. points.push_back(pface[1]);
  1415. points.push_back(pface[2]);
  1416. points.push_back(pface[2]);
  1417. points.push_back(pface[3]);
  1418. points.push_back(pface[3]);
  1419. points.push_back(pface[0]);
  1420. points.push_back(p.normal * p.d);
  1421. points.push_back(p.normal * p.d + p.normal * 3);
  1422. add_lines(points, SpatialEditorGizmos::singleton->shape_material);
  1423. add_collision_segments(points);
  1424. }
  1425. if (s->cast_to<ConvexPolygonShape>()) {
  1426. DVector<Vector3> points = s->cast_to<ConvexPolygonShape>()->get_points();
  1427. if (points.size() > 3) {
  1428. QuickHull qh;
  1429. Vector<Vector3> varr = Variant(points);
  1430. Geometry::MeshData md;
  1431. Error err = qh.build(varr, md);
  1432. if (err == OK) {
  1433. Vector<Vector3> points;
  1434. points.resize(md.edges.size() * 2);
  1435. for (int i = 0; i < md.edges.size(); i++) {
  1436. points[i * 2 + 0] = md.vertices[md.edges[i].a];
  1437. points[i * 2 + 1] = md.vertices[md.edges[i].b];
  1438. }
  1439. add_lines(points, SpatialEditorGizmos::singleton->shape_material);
  1440. add_collision_segments(points);
  1441. }
  1442. }
  1443. }
  1444. if (s->cast_to<RayShape>()) {
  1445. Ref<RayShape> rs = s;
  1446. Vector<Vector3> points;
  1447. points.push_back(Vector3());
  1448. points.push_back(Vector3(0, 0, rs->get_length()));
  1449. add_lines(points, SpatialEditorGizmos::singleton->shape_material);
  1450. add_collision_segments(points);
  1451. Vector<Vector3> handles;
  1452. handles.push_back(Vector3(0, 0, rs->get_length()));
  1453. add_handles(handles);
  1454. }
  1455. }
  1456. CollisionShapeSpatialGizmo::CollisionShapeSpatialGizmo(CollisionShape *p_cs) {
  1457. cs = p_cs;
  1458. set_spatial_node(p_cs);
  1459. }
  1460. /////
  1461. void CollisionPolygonSpatialGizmo::redraw() {
  1462. clear();
  1463. Vector<Vector2> points = polygon->get_polygon();
  1464. float depth = polygon->get_depth() * 0.5;
  1465. Vector<Vector3> lines;
  1466. for (int i = 0; i < points.size(); i++) {
  1467. int n = (i + 1) % points.size();
  1468. lines.push_back(Vector3(points[i].x, points[i].y, depth));
  1469. lines.push_back(Vector3(points[n].x, points[n].y, depth));
  1470. lines.push_back(Vector3(points[i].x, points[i].y, -depth));
  1471. lines.push_back(Vector3(points[n].x, points[n].y, -depth));
  1472. lines.push_back(Vector3(points[i].x, points[i].y, depth));
  1473. lines.push_back(Vector3(points[i].x, points[i].y, -depth));
  1474. }
  1475. add_lines(lines, SpatialEditorGizmos::singleton->shape_material);
  1476. add_collision_segments(lines);
  1477. }
  1478. CollisionPolygonSpatialGizmo::CollisionPolygonSpatialGizmo(CollisionPolygon *p_polygon) {
  1479. set_spatial_node(p_polygon);
  1480. polygon = p_polygon;
  1481. }
  1482. ///
  1483. String VisibilityNotifierGizmo::get_handle_name(int p_idx) const {
  1484. switch (p_idx) {
  1485. case 0: return "X";
  1486. case 1: return "Y";
  1487. case 2: return "Z";
  1488. }
  1489. return "";
  1490. }
  1491. Variant VisibilityNotifierGizmo::get_handle_value(int p_idx) const {
  1492. return notifier->get_aabb();
  1493. }
  1494. void VisibilityNotifierGizmo::set_handle(int p_idx, Camera *p_camera, const Point2 &p_point) {
  1495. Transform gt = notifier->get_global_transform();
  1496. //gt.orthonormalize();
  1497. Transform gi = gt.affine_inverse();
  1498. AABB aabb = notifier->get_aabb();
  1499. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  1500. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  1501. Vector3 sg[2] = { gi.xform(ray_from), gi.xform(ray_from + ray_dir * 4096) };
  1502. Vector3 ofs = aabb.pos + aabb.size * 0.5;
  1503. Vector3 axis;
  1504. axis[p_idx] = 1.0;
  1505. Vector3 ra, rb;
  1506. Geometry::get_closest_points_between_segments(ofs, ofs + axis * 4096, sg[0], sg[1], ra, rb);
  1507. float d = ra[p_idx];
  1508. if (d < 0.001)
  1509. d = 0.001;
  1510. aabb.pos[p_idx] = (aabb.pos[p_idx] + aabb.size[p_idx] * 0.5) - d;
  1511. aabb.size[p_idx] = d * 2;
  1512. notifier->set_aabb(aabb);
  1513. }
  1514. void VisibilityNotifierGizmo::commit_handle(int p_idx, const Variant &p_restore, bool p_cancel) {
  1515. if (p_cancel) {
  1516. notifier->set_aabb(p_restore);
  1517. return;
  1518. }
  1519. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  1520. ur->create_action(TTR("Change Notifier Extents"));
  1521. ur->add_do_method(notifier, "set_aabb", notifier->get_aabb());
  1522. ur->add_undo_method(notifier, "set_aabb", p_restore);
  1523. ur->commit_action();
  1524. }
  1525. void VisibilityNotifierGizmo::redraw() {
  1526. clear();
  1527. Vector<Vector3> lines;
  1528. AABB aabb = notifier->get_aabb();
  1529. for (int i = 0; i < 12; i++) {
  1530. Vector3 a, b;
  1531. aabb.get_edge(i, a, b);
  1532. lines.push_back(a);
  1533. lines.push_back(b);
  1534. }
  1535. Vector<Vector3> handles;
  1536. for (int i = 0; i < 3; i++) {
  1537. Vector3 ax;
  1538. ax[i] = aabb.pos[i] + aabb.size[i];
  1539. handles.push_back(ax);
  1540. }
  1541. add_lines(lines, SpatialEditorGizmos::singleton->visibility_notifier_material);
  1542. //add_unscaled_billboard(SpatialEditorGizmos::singleton->visi,0.05);
  1543. add_collision_segments(lines);
  1544. add_handles(handles);
  1545. }
  1546. VisibilityNotifierGizmo::VisibilityNotifierGizmo(VisibilityNotifier *p_notifier) {
  1547. notifier = p_notifier;
  1548. set_spatial_node(p_notifier);
  1549. }
  1550. ////////
  1551. void NavigationMeshSpatialGizmo::redraw() {
  1552. clear();
  1553. Ref<NavigationMesh> navmeshie = navmesh->get_navigation_mesh();
  1554. if (navmeshie.is_null())
  1555. return;
  1556. DVector<Vector3> vertices = navmeshie->get_vertices();
  1557. DVector<Vector3>::Read vr = vertices.read();
  1558. List<Face3> faces;
  1559. for (int i = 0; i < navmeshie->get_polygon_count(); i++) {
  1560. Vector<int> p = navmeshie->get_polygon(i);
  1561. for (int j = 2; j < p.size(); j++) {
  1562. Face3 f;
  1563. f.vertex[0] = vr[p[0]];
  1564. f.vertex[1] = vr[p[j - 1]];
  1565. f.vertex[2] = vr[p[j]];
  1566. faces.push_back(f);
  1567. }
  1568. }
  1569. if (faces.empty())
  1570. return;
  1571. Map<_EdgeKey, bool> edge_map;
  1572. DVector<Vector3> tmeshfaces;
  1573. tmeshfaces.resize(faces.size() * 3);
  1574. {
  1575. DVector<Vector3>::Write tw = tmeshfaces.write();
  1576. int tidx = 0;
  1577. for (List<Face3>::Element *E = faces.front(); E; E = E->next()) {
  1578. const Face3 &f = E->get();
  1579. for (int j = 0; j < 3; j++) {
  1580. tw[tidx++] = f.vertex[j];
  1581. _EdgeKey ek;
  1582. ek.from = f.vertex[j].snapped(CMP_EPSILON);
  1583. ek.to = f.vertex[(j + 1) % 3].snapped(CMP_EPSILON);
  1584. if (ek.from < ek.to)
  1585. SWAP(ek.from, ek.to);
  1586. Map<_EdgeKey, bool>::Element *E = edge_map.find(ek);
  1587. if (E) {
  1588. E->get() = false;
  1589. } else {
  1590. edge_map[ek] = true;
  1591. }
  1592. }
  1593. }
  1594. }
  1595. Vector<Vector3> lines;
  1596. for (Map<_EdgeKey, bool>::Element *E = edge_map.front(); E; E = E->next()) {
  1597. if (E->get()) {
  1598. lines.push_back(E->key().from);
  1599. lines.push_back(E->key().to);
  1600. }
  1601. }
  1602. Ref<TriangleMesh> tmesh = memnew(TriangleMesh);
  1603. tmesh->create(tmeshfaces);
  1604. if (lines.size())
  1605. add_lines(lines, navmesh->is_enabled() ? SpatialEditorGizmos::singleton->navmesh_edge_material : SpatialEditorGizmos::singleton->navmesh_edge_material_disabled);
  1606. add_collision_triangles(tmesh);
  1607. Ref<Mesh> m = memnew(Mesh);
  1608. Array a;
  1609. a.resize(Mesh::ARRAY_MAX);
  1610. a[0] = tmeshfaces;
  1611. m->add_surface(Mesh::PRIMITIVE_TRIANGLES, a);
  1612. m->surface_set_material(0, navmesh->is_enabled() ? SpatialEditorGizmos::singleton->navmesh_solid_material : SpatialEditorGizmos::singleton->navmesh_solid_material_disabled);
  1613. add_mesh(m);
  1614. add_collision_segments(lines);
  1615. }
  1616. NavigationMeshSpatialGizmo::NavigationMeshSpatialGizmo(NavigationMeshInstance *p_navmesh) {
  1617. set_spatial_node(p_navmesh);
  1618. navmesh = p_navmesh;
  1619. }
  1620. //////
  1621. ///
  1622. ///
  1623. void PinJointSpatialGizmo::redraw() {
  1624. clear();
  1625. Vector<Vector3> cursor_points;
  1626. float cs = 0.25;
  1627. cursor_points.push_back(Vector3(+cs, 0, 0));
  1628. cursor_points.push_back(Vector3(-cs, 0, 0));
  1629. cursor_points.push_back(Vector3(0, +cs, 0));
  1630. cursor_points.push_back(Vector3(0, -cs, 0));
  1631. cursor_points.push_back(Vector3(0, 0, +cs));
  1632. cursor_points.push_back(Vector3(0, 0, -cs));
  1633. add_collision_segments(cursor_points);
  1634. add_lines(cursor_points, SpatialEditorGizmos::singleton->joint_material);
  1635. }
  1636. PinJointSpatialGizmo::PinJointSpatialGizmo(PinJoint *p_p3d) {
  1637. p3d = p_p3d;
  1638. set_spatial_node(p3d);
  1639. }
  1640. ////
  1641. void HingeJointSpatialGizmo::redraw() {
  1642. clear();
  1643. Vector<Vector3> cursor_points;
  1644. float cs = 0.25;
  1645. /*cursor_points.push_back(Vector3(+cs,0,0));
  1646. cursor_points.push_back(Vector3(-cs,0,0));
  1647. cursor_points.push_back(Vector3(0,+cs,0));
  1648. cursor_points.push_back(Vector3(0,-cs,0));*/
  1649. cursor_points.push_back(Vector3(0, 0, +cs * 2));
  1650. cursor_points.push_back(Vector3(0, 0, -cs * 2));
  1651. float ll = p3d->get_param(HingeJoint::PARAM_LIMIT_LOWER);
  1652. float ul = p3d->get_param(HingeJoint::PARAM_LIMIT_UPPER);
  1653. if (p3d->get_flag(HingeJoint::FLAG_USE_LIMIT) && ll < ul) {
  1654. const int points = 32;
  1655. for (int i = 0; i < points; i++) {
  1656. float s = ll + i * (ul - ll) / points;
  1657. float n = ll + (i + 1) * (ul - ll) / points;
  1658. Vector3 from = Vector3(-Math::sin(s), Math::cos(s), 0) * cs;
  1659. Vector3 to = Vector3(-Math::sin(n), Math::cos(n), 0) * cs;
  1660. if (i == points - 1) {
  1661. cursor_points.push_back(to);
  1662. cursor_points.push_back(Vector3());
  1663. }
  1664. if (i == 0) {
  1665. cursor_points.push_back(from);
  1666. cursor_points.push_back(Vector3());
  1667. }
  1668. cursor_points.push_back(from);
  1669. cursor_points.push_back(to);
  1670. }
  1671. cursor_points.push_back(Vector3(0, cs * 1.5, 0));
  1672. cursor_points.push_back(Vector3());
  1673. } else {
  1674. const int points = 32;
  1675. for (int i = 0; i < points; i++) {
  1676. float s = ll + i * (Math_PI * 2.0) / points;
  1677. float n = ll + (i + 1) * (Math_PI * 2.0) / points;
  1678. Vector3 from = Vector3(-Math::sin(s), Math::cos(s), 0) * cs;
  1679. Vector3 to = Vector3(-Math::sin(n), Math::cos(n), 0) * cs;
  1680. cursor_points.push_back(from);
  1681. cursor_points.push_back(to);
  1682. }
  1683. }
  1684. add_collision_segments(cursor_points);
  1685. add_lines(cursor_points, SpatialEditorGizmos::singleton->joint_material);
  1686. }
  1687. HingeJointSpatialGizmo::HingeJointSpatialGizmo(HingeJoint *p_p3d) {
  1688. p3d = p_p3d;
  1689. set_spatial_node(p3d);
  1690. }
  1691. ///////
  1692. ///
  1693. ////
  1694. void SliderJointSpatialGizmo::redraw() {
  1695. clear();
  1696. Vector<Vector3> cursor_points;
  1697. float cs = 0.25;
  1698. /*cursor_points.push_back(Vector3(+cs,0,0));
  1699. cursor_points.push_back(Vector3(-cs,0,0));
  1700. cursor_points.push_back(Vector3(0,+cs,0));
  1701. cursor_points.push_back(Vector3(0,-cs,0));*/
  1702. cursor_points.push_back(Vector3(0, 0, +cs * 2));
  1703. cursor_points.push_back(Vector3(0, 0, -cs * 2));
  1704. float ll = p3d->get_param(SliderJoint::PARAM_ANGULAR_LIMIT_LOWER);
  1705. float ul = p3d->get_param(SliderJoint::PARAM_ANGULAR_LIMIT_UPPER);
  1706. float lll = -p3d->get_param(SliderJoint::PARAM_LINEAR_LIMIT_LOWER);
  1707. float lul = -p3d->get_param(SliderJoint::PARAM_LINEAR_LIMIT_UPPER);
  1708. if (lll > lul) {
  1709. cursor_points.push_back(Vector3(lul, 0, 0));
  1710. cursor_points.push_back(Vector3(lll, 0, 0));
  1711. cursor_points.push_back(Vector3(lul, -cs, -cs));
  1712. cursor_points.push_back(Vector3(lul, -cs, cs));
  1713. cursor_points.push_back(Vector3(lul, -cs, cs));
  1714. cursor_points.push_back(Vector3(lul, cs, cs));
  1715. cursor_points.push_back(Vector3(lul, cs, cs));
  1716. cursor_points.push_back(Vector3(lul, cs, -cs));
  1717. cursor_points.push_back(Vector3(lul, cs, -cs));
  1718. cursor_points.push_back(Vector3(lul, -cs, -cs));
  1719. cursor_points.push_back(Vector3(lll, -cs, -cs));
  1720. cursor_points.push_back(Vector3(lll, -cs, cs));
  1721. cursor_points.push_back(Vector3(lll, -cs, cs));
  1722. cursor_points.push_back(Vector3(lll, cs, cs));
  1723. cursor_points.push_back(Vector3(lll, cs, cs));
  1724. cursor_points.push_back(Vector3(lll, cs, -cs));
  1725. cursor_points.push_back(Vector3(lll, cs, -cs));
  1726. cursor_points.push_back(Vector3(lll, -cs, -cs));
  1727. } else {
  1728. cursor_points.push_back(Vector3(+cs * 2, 0, 0));
  1729. cursor_points.push_back(Vector3(-cs * 2, 0, 0));
  1730. }
  1731. if (ll < ul) {
  1732. const int points = 32;
  1733. for (int i = 0; i < points; i++) {
  1734. float s = ll + i * (ul - ll) / points;
  1735. float n = ll + (i + 1) * (ul - ll) / points;
  1736. Vector3 from = Vector3(0, Math::cos(s), -Math::sin(s)) * cs;
  1737. Vector3 to = Vector3(0, Math::cos(n), -Math::sin(n)) * cs;
  1738. if (i == points - 1) {
  1739. cursor_points.push_back(to);
  1740. cursor_points.push_back(Vector3());
  1741. }
  1742. if (i == 0) {
  1743. cursor_points.push_back(from);
  1744. cursor_points.push_back(Vector3());
  1745. }
  1746. cursor_points.push_back(from);
  1747. cursor_points.push_back(to);
  1748. }
  1749. cursor_points.push_back(Vector3(0, cs * 1.5, 0));
  1750. cursor_points.push_back(Vector3());
  1751. } else {
  1752. const int points = 32;
  1753. for (int i = 0; i < points; i++) {
  1754. float s = ll + i * (Math_PI * 2.0) / points;
  1755. float n = ll + (i + 1) * (Math_PI * 2.0) / points;
  1756. Vector3 from = Vector3(0, Math::cos(s), -Math::sin(s)) * cs;
  1757. Vector3 to = Vector3(0, Math::cos(n), -Math::sin(n)) * cs;
  1758. cursor_points.push_back(from);
  1759. cursor_points.push_back(to);
  1760. }
  1761. }
  1762. add_collision_segments(cursor_points);
  1763. add_lines(cursor_points, SpatialEditorGizmos::singleton->joint_material);
  1764. }
  1765. SliderJointSpatialGizmo::SliderJointSpatialGizmo(SliderJoint *p_p3d) {
  1766. p3d = p_p3d;
  1767. set_spatial_node(p3d);
  1768. }
  1769. ///////
  1770. ///
  1771. ////
  1772. void ConeTwistJointSpatialGizmo::redraw() {
  1773. clear();
  1774. Vector<Vector3> points;
  1775. float r = 1.0;
  1776. float w = r * Math::sin(p3d->get_param(ConeTwistJoint::PARAM_SWING_SPAN));
  1777. float d = r * Math::cos(p3d->get_param(ConeTwistJoint::PARAM_SWING_SPAN));
  1778. //swing
  1779. for (int i = 0; i < 360; i += 10) {
  1780. float ra = Math::deg2rad(i);
  1781. float rb = Math::deg2rad(i + 10);
  1782. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w;
  1783. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w;
  1784. /*points.push_back(Vector3(a.x,0,a.y));
  1785. points.push_back(Vector3(b.x,0,b.y));
  1786. points.push_back(Vector3(0,a.x,a.y));
  1787. points.push_back(Vector3(0,b.x,b.y));*/
  1788. points.push_back(Vector3(d, a.x, a.y));
  1789. points.push_back(Vector3(d, b.x, b.y));
  1790. if (i % 90 == 0) {
  1791. points.push_back(Vector3(d, a.x, a.y));
  1792. points.push_back(Vector3());
  1793. }
  1794. }
  1795. points.push_back(Vector3());
  1796. points.push_back(Vector3(1, 0, 0));
  1797. //twist
  1798. /*
  1799. */
  1800. float ts = Math::rad2deg(p3d->get_param(ConeTwistJoint::PARAM_TWIST_SPAN));
  1801. ts = MIN(ts, 720);
  1802. for (int i = 0; i < int(ts); i += 5) {
  1803. float ra = Math::deg2rad(i);
  1804. float rb = Math::deg2rad(i + 5);
  1805. float c = i / 720.0;
  1806. float cn = (i + 5) / 720.0;
  1807. Point2 a = Vector2(Math::sin(ra), Math::cos(ra)) * w * c;
  1808. Point2 b = Vector2(Math::sin(rb), Math::cos(rb)) * w * cn;
  1809. /*points.push_back(Vector3(a.x,0,a.y));
  1810. points.push_back(Vector3(b.x,0,b.y));
  1811. points.push_back(Vector3(0,a.x,a.y));
  1812. points.push_back(Vector3(0,b.x,b.y));*/
  1813. points.push_back(Vector3(c, a.x, a.y));
  1814. points.push_back(Vector3(cn, b.x, b.y));
  1815. }
  1816. add_collision_segments(points);
  1817. add_lines(points, SpatialEditorGizmos::singleton->joint_material);
  1818. }
  1819. ConeTwistJointSpatialGizmo::ConeTwistJointSpatialGizmo(ConeTwistJoint *p_p3d) {
  1820. p3d = p_p3d;
  1821. set_spatial_node(p3d);
  1822. }
  1823. ////////
  1824. /// \brief SpatialEditorGizmos::singleton
  1825. ///
  1826. ///////
  1827. ///
  1828. ////
  1829. void Generic6DOFJointSpatialGizmo::redraw() {
  1830. clear();
  1831. Vector<Vector3> cursor_points;
  1832. float cs = 0.25;
  1833. for (int ax = 0; ax < 3; ax++) {
  1834. /*cursor_points.push_back(Vector3(+cs,0,0));
  1835. cursor_points.push_back(Vector3(-cs,0,0));
  1836. cursor_points.push_back(Vector3(0,+cs,0));
  1837. cursor_points.push_back(Vector3(0,-cs,0));
  1838. cursor_points.push_back(Vector3(0,0,+cs*2));
  1839. cursor_points.push_back(Vector3(0,0,-cs*2)); */
  1840. float ll;
  1841. float ul;
  1842. float lll;
  1843. float lul;
  1844. int a1, a2, a3;
  1845. bool enable_ang;
  1846. bool enable_lin;
  1847. switch (ax) {
  1848. case 0:
  1849. ll = p3d->get_param_x(Generic6DOFJoint::PARAM_ANGULAR_LOWER_LIMIT);
  1850. ul = p3d->get_param_x(Generic6DOFJoint::PARAM_ANGULAR_UPPER_LIMIT);
  1851. lll = -p3d->get_param_x(Generic6DOFJoint::PARAM_LINEAR_LOWER_LIMIT);
  1852. lul = -p3d->get_param_x(Generic6DOFJoint::PARAM_LINEAR_UPPER_LIMIT);
  1853. enable_ang = p3d->get_flag_x(Generic6DOFJoint::FLAG_ENABLE_ANGULAR_LIMIT);
  1854. enable_lin = p3d->get_flag_x(Generic6DOFJoint::FLAG_ENABLE_LINEAR_LIMIT);
  1855. a1 = 0;
  1856. a2 = 1;
  1857. a3 = 2;
  1858. break;
  1859. case 1:
  1860. ll = p3d->get_param_y(Generic6DOFJoint::PARAM_ANGULAR_LOWER_LIMIT);
  1861. ul = p3d->get_param_y(Generic6DOFJoint::PARAM_ANGULAR_UPPER_LIMIT);
  1862. lll = -p3d->get_param_y(Generic6DOFJoint::PARAM_LINEAR_LOWER_LIMIT);
  1863. lul = -p3d->get_param_y(Generic6DOFJoint::PARAM_LINEAR_UPPER_LIMIT);
  1864. enable_ang = p3d->get_flag_y(Generic6DOFJoint::FLAG_ENABLE_ANGULAR_LIMIT);
  1865. enable_lin = p3d->get_flag_y(Generic6DOFJoint::FLAG_ENABLE_LINEAR_LIMIT);
  1866. a1 = 2;
  1867. a2 = 0;
  1868. a3 = 1;
  1869. break;
  1870. case 2:
  1871. ll = p3d->get_param_z(Generic6DOFJoint::PARAM_ANGULAR_LOWER_LIMIT);
  1872. ul = p3d->get_param_z(Generic6DOFJoint::PARAM_ANGULAR_UPPER_LIMIT);
  1873. lll = -p3d->get_param_z(Generic6DOFJoint::PARAM_LINEAR_LOWER_LIMIT);
  1874. lul = -p3d->get_param_z(Generic6DOFJoint::PARAM_LINEAR_UPPER_LIMIT);
  1875. enable_ang = p3d->get_flag_z(Generic6DOFJoint::FLAG_ENABLE_ANGULAR_LIMIT);
  1876. enable_lin = p3d->get_flag_z(Generic6DOFJoint::FLAG_ENABLE_LINEAR_LIMIT);
  1877. a1 = 1;
  1878. a2 = 2;
  1879. a3 = 0;
  1880. break;
  1881. }
  1882. #define ADD_VTX(x, y, z) \
  1883. { \
  1884. Vector3 v; \
  1885. v[a1] = (x); \
  1886. v[a2] = (y); \
  1887. v[a3] = (z); \
  1888. cursor_points.push_back(v); \
  1889. }
  1890. #define SET_VTX(what, x, y, z) \
  1891. { \
  1892. Vector3 v; \
  1893. v[a1] = (x); \
  1894. v[a2] = (y); \
  1895. v[a3] = (z); \
  1896. what = v; \
  1897. }
  1898. if (enable_lin && lll >= lul) {
  1899. ADD_VTX(lul, 0, 0);
  1900. ADD_VTX(lll, 0, 0);
  1901. ADD_VTX(lul, -cs, -cs);
  1902. ADD_VTX(lul, -cs, cs);
  1903. ADD_VTX(lul, -cs, cs);
  1904. ADD_VTX(lul, cs, cs);
  1905. ADD_VTX(lul, cs, cs);
  1906. ADD_VTX(lul, cs, -cs);
  1907. ADD_VTX(lul, cs, -cs);
  1908. ADD_VTX(lul, -cs, -cs);
  1909. ADD_VTX(lll, -cs, -cs);
  1910. ADD_VTX(lll, -cs, cs);
  1911. ADD_VTX(lll, -cs, cs);
  1912. ADD_VTX(lll, cs, cs);
  1913. ADD_VTX(lll, cs, cs);
  1914. ADD_VTX(lll, cs, -cs);
  1915. ADD_VTX(lll, cs, -cs);
  1916. ADD_VTX(lll, -cs, -cs);
  1917. } else {
  1918. ADD_VTX(+cs * 2, 0, 0);
  1919. ADD_VTX(-cs * 2, 0, 0);
  1920. }
  1921. if (enable_ang && ll <= ul) {
  1922. const int points = 32;
  1923. for (int i = 0; i < points; i++) {
  1924. float s = ll + i * (ul - ll) / points;
  1925. float n = ll + (i + 1) * (ul - ll) / points;
  1926. Vector3 from;
  1927. SET_VTX(from, 0, Math::cos(s), -Math::sin(s));
  1928. from *= cs;
  1929. Vector3 to;
  1930. SET_VTX(to, 0, Math::cos(n), -Math::sin(n));
  1931. to *= cs;
  1932. if (i == points - 1) {
  1933. cursor_points.push_back(to);
  1934. cursor_points.push_back(Vector3());
  1935. }
  1936. if (i == 0) {
  1937. cursor_points.push_back(from);
  1938. cursor_points.push_back(Vector3());
  1939. }
  1940. cursor_points.push_back(from);
  1941. cursor_points.push_back(to);
  1942. }
  1943. ADD_VTX(0, cs * 1.5, 0);
  1944. cursor_points.push_back(Vector3());
  1945. } else {
  1946. const int points = 32;
  1947. for (int i = 0; i < points; i++) {
  1948. float s = ll + i * (Math_PI * 2.0) / points;
  1949. float n = ll + (i + 1) * (Math_PI * 2.0) / points;
  1950. // Vector3 from=Vector3(0,Math::cos(s),-Math::sin(s) )*cs;
  1951. // Vector3 to=Vector3( 0,Math::cos(n),-Math::sin(n) )*cs;
  1952. Vector3 from;
  1953. SET_VTX(from, 0, Math::cos(s), -Math::sin(s));
  1954. from *= cs;
  1955. Vector3 to;
  1956. SET_VTX(to, 0, Math::cos(n), -Math::sin(n));
  1957. to *= cs;
  1958. cursor_points.push_back(from);
  1959. cursor_points.push_back(to);
  1960. }
  1961. }
  1962. }
  1963. #undef ADD_VTX
  1964. #undef SET_VTX
  1965. add_collision_segments(cursor_points);
  1966. add_lines(cursor_points, SpatialEditorGizmos::singleton->joint_material);
  1967. }
  1968. Generic6DOFJointSpatialGizmo::Generic6DOFJointSpatialGizmo(Generic6DOFJoint *p_p3d) {
  1969. p3d = p_p3d;
  1970. set_spatial_node(p3d);
  1971. }
  1972. ///////
  1973. ///
  1974. ////
  1975. SpatialEditorGizmos *SpatialEditorGizmos::singleton = NULL;
  1976. Ref<SpatialEditorGizmo> SpatialEditorGizmos::get_gizmo(Spatial *p_spatial) {
  1977. if (p_spatial->cast_to<Light>()) {
  1978. Ref<LightSpatialGizmo> lsg = memnew(LightSpatialGizmo(p_spatial->cast_to<Light>()));
  1979. return lsg;
  1980. }
  1981. if (p_spatial->cast_to<Listener>()) {
  1982. Ref<ListenerSpatialGizmo> misg = memnew(ListenerSpatialGizmo(p_spatial->cast_to<Listener>()));
  1983. return misg;
  1984. }
  1985. if (p_spatial->cast_to<Camera>()) {
  1986. Ref<CameraSpatialGizmo> lsg = memnew(CameraSpatialGizmo(p_spatial->cast_to<Camera>()));
  1987. return lsg;
  1988. }
  1989. if (p_spatial->cast_to<Skeleton>()) {
  1990. Ref<SkeletonSpatialGizmo> lsg = memnew(SkeletonSpatialGizmo(p_spatial->cast_to<Skeleton>()));
  1991. return lsg;
  1992. }
  1993. if (p_spatial->cast_to<Position3D>()) {
  1994. Ref<Position3DSpatialGizmo> lsg = memnew(Position3DSpatialGizmo(p_spatial->cast_to<Position3D>()));
  1995. return lsg;
  1996. }
  1997. if (p_spatial->cast_to<MeshInstance>()) {
  1998. Ref<MeshInstanceSpatialGizmo> misg = memnew(MeshInstanceSpatialGizmo(p_spatial->cast_to<MeshInstance>()));
  1999. return misg;
  2000. }
  2001. if (p_spatial->cast_to<Room>()) {
  2002. Ref<RoomSpatialGizmo> misg = memnew(RoomSpatialGizmo(p_spatial->cast_to<Room>()));
  2003. return misg;
  2004. }
  2005. if (p_spatial->cast_to<NavigationMeshInstance>()) {
  2006. Ref<NavigationMeshSpatialGizmo> misg = memnew(NavigationMeshSpatialGizmo(p_spatial->cast_to<NavigationMeshInstance>()));
  2007. return misg;
  2008. }
  2009. if (p_spatial->cast_to<RayCast>()) {
  2010. Ref<RayCastSpatialGizmo> misg = memnew(RayCastSpatialGizmo(p_spatial->cast_to<RayCast>()));
  2011. return misg;
  2012. }
  2013. if (p_spatial->cast_to<Portal>()) {
  2014. Ref<PortalSpatialGizmo> misg = memnew(PortalSpatialGizmo(p_spatial->cast_to<Portal>()));
  2015. return misg;
  2016. }
  2017. if (p_spatial->cast_to<TestCube>()) {
  2018. Ref<TestCubeSpatialGizmo> misg = memnew(TestCubeSpatialGizmo(p_spatial->cast_to<TestCube>()));
  2019. return misg;
  2020. }
  2021. if (p_spatial->cast_to<SpatialPlayer>()) {
  2022. Ref<SpatialPlayerSpatialGizmo> misg = memnew(SpatialPlayerSpatialGizmo(p_spatial->cast_to<SpatialPlayer>()));
  2023. return misg;
  2024. }
  2025. if (p_spatial->cast_to<CollisionShape>()) {
  2026. Ref<CollisionShapeSpatialGizmo> misg = memnew(CollisionShapeSpatialGizmo(p_spatial->cast_to<CollisionShape>()));
  2027. return misg;
  2028. }
  2029. if (p_spatial->cast_to<VisibilityNotifier>()) {
  2030. Ref<VisibilityNotifierGizmo> misg = memnew(VisibilityNotifierGizmo(p_spatial->cast_to<VisibilityNotifier>()));
  2031. return misg;
  2032. }
  2033. if (p_spatial->cast_to<VehicleWheel>()) {
  2034. Ref<VehicleWheelSpatialGizmo> misg = memnew(VehicleWheelSpatialGizmo(p_spatial->cast_to<VehicleWheel>()));
  2035. return misg;
  2036. }
  2037. if (p_spatial->cast_to<PinJoint>()) {
  2038. Ref<PinJointSpatialGizmo> misg = memnew(PinJointSpatialGizmo(p_spatial->cast_to<PinJoint>()));
  2039. return misg;
  2040. }
  2041. if (p_spatial->cast_to<HingeJoint>()) {
  2042. Ref<HingeJointSpatialGizmo> misg = memnew(HingeJointSpatialGizmo(p_spatial->cast_to<HingeJoint>()));
  2043. return misg;
  2044. }
  2045. if (p_spatial->cast_to<SliderJoint>()) {
  2046. Ref<SliderJointSpatialGizmo> misg = memnew(SliderJointSpatialGizmo(p_spatial->cast_to<SliderJoint>()));
  2047. return misg;
  2048. }
  2049. if (p_spatial->cast_to<ConeTwistJoint>()) {
  2050. Ref<ConeTwistJointSpatialGizmo> misg = memnew(ConeTwistJointSpatialGizmo(p_spatial->cast_to<ConeTwistJoint>()));
  2051. return misg;
  2052. }
  2053. if (p_spatial->cast_to<Generic6DOFJoint>()) {
  2054. Ref<Generic6DOFJointSpatialGizmo> misg = memnew(Generic6DOFJointSpatialGizmo(p_spatial->cast_to<Generic6DOFJoint>()));
  2055. return misg;
  2056. }
  2057. if (p_spatial->cast_to<CollisionPolygon>()) {
  2058. Ref<CollisionPolygonSpatialGizmo> misg = memnew(CollisionPolygonSpatialGizmo(p_spatial->cast_to<CollisionPolygon>()));
  2059. return misg;
  2060. }
  2061. return Ref<SpatialEditorGizmo>();
  2062. }
  2063. Ref<FixedMaterial> SpatialEditorGizmos::create_line_material(const Color &p_base_color) {
  2064. Ref<FixedMaterial> line_material = Ref<FixedMaterial>(memnew(FixedMaterial));
  2065. line_material->set_flag(Material::FLAG_UNSHADED, true);
  2066. line_material->set_line_width(3.0);
  2067. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2068. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, true);
  2069. line_material->set_parameter(FixedMaterial::PARAM_DIFFUSE, p_base_color);
  2070. return line_material;
  2071. }
  2072. Ref<FixedMaterial> SpatialEditorGizmos::create_solid_material(const Color &p_base_color) {
  2073. Ref<FixedMaterial> line_material = Ref<FixedMaterial>(memnew(FixedMaterial));
  2074. line_material->set_flag(Material::FLAG_UNSHADED, true);
  2075. line_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2076. line_material->set_parameter(FixedMaterial::PARAM_DIFFUSE, p_base_color);
  2077. return line_material;
  2078. }
  2079. SpatialEditorGizmos::SpatialEditorGizmos() {
  2080. singleton = this;
  2081. handle_material = Ref<FixedMaterial>(memnew(FixedMaterial));
  2082. handle_material->set_flag(Material::FLAG_UNSHADED, true);
  2083. handle_material->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(0.8, 0.8, 0.8));
  2084. handle2_material = Ref<FixedMaterial>(memnew(FixedMaterial));
  2085. handle2_material->set_flag(Material::FLAG_UNSHADED, true);
  2086. handle2_material->set_fixed_flag(FixedMaterial::FLAG_USE_POINT_SIZE, true);
  2087. handle_t = SpatialEditor::get_singleton()->get_icon("Editor3DHandle", "EditorIcons");
  2088. handle2_material->set_point_size(handle_t->get_width());
  2089. handle2_material->set_texture(FixedMaterial::PARAM_DIFFUSE, handle_t);
  2090. handle2_material->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(1, 1, 1));
  2091. handle2_material->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2092. handle2_material->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, true);
  2093. light_material = create_line_material(Color(1, 1, 0.2));
  2094. light_material_omni_icon = Ref<FixedMaterial>(memnew(FixedMaterial));
  2095. light_material_omni_icon->set_flag(Material::FLAG_UNSHADED, true);
  2096. light_material_omni_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  2097. light_material_omni_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  2098. light_material_omni_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2099. light_material_omni_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(1, 1, 1, 0.9));
  2100. light_material_omni_icon->set_texture(FixedMaterial::PARAM_DIFFUSE, SpatialEditor::get_singleton()->get_icon("GizmoLight", "EditorIcons"));
  2101. light_material_directional_icon = Ref<FixedMaterial>(memnew(FixedMaterial));
  2102. light_material_directional_icon->set_flag(Material::FLAG_UNSHADED, true);
  2103. light_material_directional_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  2104. light_material_directional_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  2105. light_material_directional_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2106. light_material_directional_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(1, 1, 1, 0.9));
  2107. light_material_directional_icon->set_texture(FixedMaterial::PARAM_DIFFUSE, SpatialEditor::get_singleton()->get_icon("GizmoDirectionalLight", "EditorIcons"));
  2108. camera_material = create_line_material(Color(1.0, 0.5, 1.0));
  2109. navmesh_edge_material = create_line_material(Color(0.1, 0.8, 1.0));
  2110. navmesh_solid_material = create_solid_material(Color(0.1, 0.8, 1.0, 0.4));
  2111. navmesh_edge_material->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, false);
  2112. navmesh_solid_material->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  2113. navmesh_edge_material_disabled = create_line_material(Color(1.0, 0.8, 0.1));
  2114. navmesh_solid_material_disabled = create_solid_material(Color(1.0, 0.8, 0.1, 0.4));
  2115. navmesh_edge_material_disabled->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, false);
  2116. navmesh_solid_material_disabled->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  2117. skeleton_material = create_line_material(Color(0.6, 1.0, 0.3));
  2118. skeleton_material->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  2119. skeleton_material->set_flag(Material::FLAG_UNSHADED, true);
  2120. skeleton_material->set_flag(Material::FLAG_ONTOP, true);
  2121. skeleton_material->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  2122. //position 3D Shared mesh
  2123. pos3d_mesh = Ref<Mesh>(memnew(Mesh));
  2124. {
  2125. DVector<Vector3> cursor_points;
  2126. DVector<Color> cursor_colors;
  2127. float cs = 0.25;
  2128. cursor_points.push_back(Vector3(+cs, 0, 0));
  2129. cursor_points.push_back(Vector3(-cs, 0, 0));
  2130. cursor_points.push_back(Vector3(0, +cs, 0));
  2131. cursor_points.push_back(Vector3(0, -cs, 0));
  2132. cursor_points.push_back(Vector3(0, 0, +cs));
  2133. cursor_points.push_back(Vector3(0, 0, -cs));
  2134. cursor_colors.push_back(Color(1, 0.5, 0.5, 0.7));
  2135. cursor_colors.push_back(Color(1, 0.5, 0.5, 0.7));
  2136. cursor_colors.push_back(Color(0.5, 1, 0.5, 0.7));
  2137. cursor_colors.push_back(Color(0.5, 1, 0.5, 0.7));
  2138. cursor_colors.push_back(Color(0.5, 0.5, 1, 0.7));
  2139. cursor_colors.push_back(Color(0.5, 0.5, 1, 0.7));
  2140. Ref<FixedMaterial> mat = memnew(FixedMaterial);
  2141. mat->set_flag(Material::FLAG_UNSHADED, true);
  2142. mat->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, true);
  2143. mat->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2144. mat->set_line_width(3);
  2145. Array d;
  2146. d.resize(VS::ARRAY_MAX);
  2147. d[Mesh::ARRAY_VERTEX] = cursor_points;
  2148. d[Mesh::ARRAY_COLOR] = cursor_colors;
  2149. pos3d_mesh->add_surface(Mesh::PRIMITIVE_LINES, d);
  2150. pos3d_mesh->surface_set_material(0, mat);
  2151. }
  2152. listener_line_mesh = Ref<Mesh>(memnew(Mesh));
  2153. {
  2154. DVector<Vector3> cursor_points;
  2155. DVector<Color> cursor_colors;
  2156. cursor_points.push_back(Vector3(0, 0, 0));
  2157. cursor_points.push_back(Vector3(0, 0, -1.0));
  2158. cursor_colors.push_back(Color(0.5, 0.5, 0.5, 0.7));
  2159. cursor_colors.push_back(Color(0.5, 0.5, 0.5, 0.7));
  2160. Ref<FixedMaterial> mat = memnew(FixedMaterial);
  2161. mat->set_flag(Material::FLAG_UNSHADED, true);
  2162. mat->set_fixed_flag(FixedMaterial::FLAG_USE_COLOR_ARRAY, true);
  2163. mat->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2164. mat->set_line_width(3);
  2165. Array d;
  2166. d.resize(VS::ARRAY_MAX);
  2167. d[Mesh::ARRAY_VERTEX] = cursor_points;
  2168. d[Mesh::ARRAY_COLOR] = cursor_colors;
  2169. listener_line_mesh->add_surface(Mesh::PRIMITIVE_LINES, d);
  2170. listener_line_mesh->surface_set_material(0, mat);
  2171. }
  2172. sample_player_icon = Ref<FixedMaterial>(memnew(FixedMaterial));
  2173. sample_player_icon->set_flag(Material::FLAG_UNSHADED, true);
  2174. sample_player_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  2175. sample_player_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  2176. sample_player_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2177. sample_player_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(1, 1, 1, 0.9));
  2178. sample_player_icon->set_texture(FixedMaterial::PARAM_DIFFUSE, SpatialEditor::get_singleton()->get_icon("GizmoSpatialSamplePlayer", "EditorIcons"));
  2179. room_material = create_line_material(Color(1.0, 0.6, 0.9));
  2180. portal_material = create_line_material(Color(1.0, 0.8, 0.6));
  2181. raycast_material = create_line_material(Color(1.0, 0.8, 0.6));
  2182. car_wheel_material = create_line_material(Color(0.6, 0.8, 1.0));
  2183. visibility_notifier_material = create_line_material(Color(1.0, 0.5, 1.0));
  2184. joint_material = create_line_material(Color(0.6, 0.8, 1.0));
  2185. stream_player_icon = Ref<FixedMaterial>(memnew(FixedMaterial));
  2186. stream_player_icon->set_flag(Material::FLAG_UNSHADED, true);
  2187. stream_player_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  2188. stream_player_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  2189. stream_player_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2190. stream_player_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(1, 1, 1, 0.9));
  2191. stream_player_icon->set_texture(FixedMaterial::PARAM_DIFFUSE, SpatialEditor::get_singleton()->get_icon("GizmoSpatialStreamPlayer", "EditorIcons"));
  2192. visibility_notifier_icon = Ref<FixedMaterial>(memnew(FixedMaterial));
  2193. visibility_notifier_icon->set_flag(Material::FLAG_UNSHADED, true);
  2194. visibility_notifier_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  2195. visibility_notifier_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  2196. visibility_notifier_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2197. visibility_notifier_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(1, 1, 1, 0.9));
  2198. visibility_notifier_icon->set_texture(FixedMaterial::PARAM_DIFFUSE, SpatialEditor::get_singleton()->get_icon("Visible", "EditorIcons"));
  2199. listener_icon = Ref<FixedMaterial>(memnew(FixedMaterial));
  2200. listener_icon->set_flag(Material::FLAG_UNSHADED, true);
  2201. listener_icon->set_flag(Material::FLAG_DOUBLE_SIDED, true);
  2202. listener_icon->set_depth_draw_mode(Material::DEPTH_DRAW_NEVER);
  2203. listener_icon->set_fixed_flag(FixedMaterial::FLAG_USE_ALPHA, true);
  2204. listener_icon->set_parameter(FixedMaterial::PARAM_DIFFUSE, Color(1, 1, 1, 0.9));
  2205. listener_icon->set_texture(FixedMaterial::PARAM_DIFFUSE, SpatialEditor::get_singleton()->get_icon("GizmoListener", "EditorIcons"));
  2206. {
  2207. DVector<Vector3> vertices;
  2208. #undef ADD_VTX
  2209. #define ADD_VTX(m_idx) \
  2210. vertices.push_back(face_points[m_idx]);
  2211. for (int i = 0; i < 6; i++) {
  2212. Vector3 face_points[4];
  2213. for (int j = 0; j < 4; j++) {
  2214. float v[3];
  2215. v[0] = 1.0;
  2216. v[1] = 1 - 2 * ((j >> 1) & 1);
  2217. v[2] = v[1] * (1 - 2 * (j & 1));
  2218. for (int k = 0; k < 3; k++) {
  2219. if (i < 3)
  2220. face_points[j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  2221. else
  2222. face_points[3 - j][(i + k) % 3] = v[k] * (i >= 3 ? -1 : 1);
  2223. }
  2224. }
  2225. //tri 1
  2226. ADD_VTX(0);
  2227. ADD_VTX(1);
  2228. ADD_VTX(2);
  2229. //tri 2
  2230. ADD_VTX(2);
  2231. ADD_VTX(3);
  2232. ADD_VTX(0);
  2233. }
  2234. test_cube_tm = Ref<TriangleMesh>(memnew(TriangleMesh));
  2235. test_cube_tm->create(vertices);
  2236. }
  2237. shape_material = create_line_material(Color(0.2, 1, 1.0));
  2238. }