spatial_editor_gizmos.cpp 80 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965
  1. /*************************************************************************/
  2. /* spatial_editor_gizmos.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "spatial_editor_gizmos.h"
  31. #include "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. }