editor_shape_gizmos.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /*************************************************************************/
  2. /* editor_shape_gizmos.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "editor_shape_gizmos.h"
  30. String EditableShapeSpatialGizmo::get_handle_name(int p_idx) const {
  31. if (es->cast_to<EditableSphere>()) {
  32. return "Radius";
  33. }
  34. #if 0
  35. if (es->cast_to<EditableBox>()) {
  36. return "Extents";
  37. }
  38. if (es->cast_to<EditableCapsule>()) {
  39. return p_idx==0?"Radius":"Height";
  40. }
  41. if (es->cast_to<EditableRay>()) {
  42. return "Length";
  43. }
  44. #endif
  45. return "";
  46. }
  47. Variant EditableShapeSpatialGizmo::get_handle_value(int p_idx) const{
  48. if (es->cast_to<EditableSphere>()) {
  49. EditableSphere *ss = es->cast_to<EditableSphere>();
  50. return ss->get_radius();
  51. }
  52. #if 0
  53. if (es->cast_to<EditableBox>()) {
  54. EditableBox *bs = es->cast_to<EditableBox>();
  55. return bs->get_extents();
  56. }
  57. if (es->cast_to<EditableCapsule>()) {
  58. EditableCapsule *cs = es->cast_to<EditableCapsule>();
  59. return p_idx==0?es->get_radius():es->get_height();
  60. }
  61. if (es->cast_to<EditableRay>()) {
  62. EditableRay* cs = es->cast_to<EditableRay>();
  63. return es->get_length();
  64. }
  65. #endif
  66. return Variant();
  67. }
  68. void EditableShapeSpatialGizmo::set_handle(int p_idx,Camera *p_camera, const Point2& p_point){
  69. Transform gt = es->get_global_transform();
  70. gt.orthonormalize();
  71. Transform gi = gt.affine_inverse();
  72. Vector3 ray_from = p_camera->project_ray_origin(p_point);
  73. Vector3 ray_dir = p_camera->project_ray_normal(p_point);
  74. Vector3 sg[2]={gi.xform(ray_from),gi.xform(ray_from+ray_dir*4096)};
  75. if (es->cast_to<EditableSphere>()) {
  76. EditableSphere *ss = es->cast_to<EditableSphere>();
  77. Vector3 ra,rb;
  78. Geometry::get_closest_points_between_segments(Vector3(),Vector3(4096,0,0),sg[0],sg[1],ra,rb);
  79. float d = ra.x;
  80. if (d<0.001)
  81. d=0.001;
  82. ss->set_radius(d);
  83. }
  84. #if 0
  85. if (es->cast_to<EditableRay>()) {
  86. EditableRay*cs = es->cast_to<EditableRay>();
  87. Vector3 ra,rb;
  88. Geometry::get_closest_points_between_segments(Vector3(),Vector3(0,0,4096),sg[0],sg[1],ra,rb);
  89. float d = ra.z;
  90. if (d<0.001)
  91. d=0.001;
  92. rs->set_length(d);
  93. }
  94. if (es->cast_to<EditableBox>()) {
  95. Vector3 axis;
  96. axis[p_idx]=1.0;
  97. EditableBox *bs = es->cast_to<EditableBox>();
  98. Vector3 ra,rb;
  99. Geometry::get_closest_points_between_segments(Vector3(),axis*4096,sg[0],sg[1],ra,rb);
  100. float d = ra[p_idx];
  101. if (d<0.001)
  102. d=0.001;
  103. Vector3 he = bs->get_extents();
  104. he[p_idx]=d;
  105. bs->set_extents(he);
  106. }
  107. if (es->cast_to<EditableCapsule>()) {
  108. Vector3 axis;
  109. axis[p_idx]=1.0;
  110. EditableCapsule *cs = es->cast_to<EditableCapsule>();
  111. Vector3 ra,rb;
  112. Geometry::get_closest_points_between_segments(Vector3(),axis*4096,sg[0],sg[1],ra,rb);
  113. float d = ra[p_idx];
  114. if (p_idx==1)
  115. d-=es->get_radius();
  116. if (d<0.001)
  117. d=0.001;
  118. if (p_idx==0)
  119. es->set_radius(d);
  120. else if (p_idx==1)
  121. es->set_height(d*2.0);
  122. }
  123. #endif
  124. }
  125. void EditableShapeSpatialGizmo::commit_handle(int p_idx,const Variant& p_restore,bool p_cancel){
  126. if (es->cast_to<EditableSphere>()) {
  127. EditableSphere *ss = es->cast_to<EditableSphere>();
  128. if (p_cancel) {
  129. ss->set_radius(p_restore);
  130. return;
  131. }
  132. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  133. ur->create_action("Change Sphere Shape Radius");
  134. ur->add_do_method(ss,"set_radius",ss->get_radius());
  135. ur->add_undo_method(ss,"set_radius",p_restore);
  136. ur->commit_action();
  137. }
  138. #if 0
  139. if (es->cast_to<EditableBox>()) {
  140. EditableBox *ss = es->cast_to<EditableBox>();
  141. if (p_cancel) {
  142. ss->set_extents(p_restore);
  143. return;
  144. }
  145. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  146. ur->create_action("Change Box Shape Extents");
  147. ur->add_do_method(ss,"set_extents",ss->get_extents());
  148. ur->add_undo_method(ss,"set_extents",p_restore);
  149. ur->commit_action();
  150. }
  151. if (es->cast_to<EditableCapsule>()) {
  152. EditableCapsule *cs = es->cast_to<EditableCapsule>();
  153. if (p_cancel) {
  154. if (p_idx==0)
  155. ss->set_radius(p_restore);
  156. else
  157. ss->set_height(p_restore);
  158. return;
  159. }
  160. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  161. if (p_idx==0) {
  162. ur->create_action("Change Capsule Shape Radius");
  163. ur->add_do_method(ss,"set_radius",ss->get_radius());
  164. ur->add_undo_method(ss,"set_radius",p_restore);
  165. } else {
  166. ur->create_action("Change Capsule Shape Height");
  167. ur->add_do_method(ss,"set_height",ss->get_height());
  168. ur->add_undo_method(ss,"set_height",p_restore);
  169. }
  170. ur->commit_action();
  171. }
  172. if (es->cast_to<EditableRay>()) {
  173. EditableRay*rs = es->cast_to<EditableRay>()
  174. if (p_cancel) {
  175. ss->set_length(p_restore);
  176. return;
  177. }
  178. UndoRedo *ur = SpatialEditor::get_singleton()->get_undo_redo();
  179. ur->create_action("Change Ray Shape Length");
  180. ur->add_do_method(ss,"set_length",ss->get_length());
  181. ur->add_undo_method(ss,"set_length",p_restore);
  182. ur->commit_action();
  183. }
  184. #endif
  185. }
  186. void EditableShapeSpatialGizmo::redraw(){
  187. clear();
  188. if (es->cast_to<EditableSphere>()) {
  189. EditableSphere* sp= es->cast_to<EditableSphere>();
  190. float r=sp->get_radius();
  191. Vector<Vector3> points;
  192. for(int i=0;i<=360;i++) {
  193. float ra=Math::deg2rad(i);
  194. float rb=Math::deg2rad(i+1);
  195. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*r;
  196. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*r;
  197. points.push_back(Vector3(a.x,0,a.y));
  198. points.push_back(Vector3(b.x,0,b.y));
  199. points.push_back(Vector3(0,a.x,a.y));
  200. points.push_back(Vector3(0,b.x,b.y));
  201. points.push_back(Vector3(a.x,a.y,0));
  202. points.push_back(Vector3(b.x,b.y,0));
  203. }
  204. Vector<Vector3> collision_segments;
  205. for(int i=0;i<64;i++) {
  206. float ra=i*Math_PI*2.0/64.0;
  207. float rb=(i+1)*Math_PI*2.0/64.0;
  208. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*r;
  209. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*r;
  210. collision_segments.push_back(Vector3(a.x,0,a.y));
  211. collision_segments.push_back(Vector3(b.x,0,b.y));
  212. collision_segments.push_back(Vector3(0,a.x,a.y));
  213. collision_segments.push_back(Vector3(0,b.x,b.y));
  214. collision_segments.push_back(Vector3(a.x,a.y,0));
  215. collision_segments.push_back(Vector3(b.x,b.y,0));
  216. }
  217. add_lines(points,SpatialEditorGizmos::singleton->shape_material);
  218. add_collision_segments(collision_segments);
  219. Vector<Vector3> handles;
  220. handles.push_back(Vector3(r,0,0));
  221. add_handles(handles);
  222. }
  223. #if 0
  224. if (es->cast_to<EditableBox>()) {
  225. EditableBox*bs = es->cast_to<EditableBox>();
  226. Vector<Vector3> lines;
  227. AABB aabb;
  228. aabb.pos=-bs->get_extents();
  229. aabb.size=aabb.pos*-2;
  230. for(int i=0;i<12;i++) {
  231. Vector3 a,b;
  232. aabb.get_edge(i,a,b);
  233. lines.push_back(a);
  234. lines.push_back(b);
  235. }
  236. Vector<Vector3> handles;
  237. for(int i=0;i<3;i++) {
  238. Vector3 ax;
  239. ax[i]=bs->get_extents()[i];
  240. handles.push_back(ax);
  241. }
  242. add_lines(lines,SpatialEditorGizmos::singleton->shape_material);
  243. add_collision_segments(lines);
  244. add_handles(handles);
  245. }
  246. if (es->cast_to<EditableCapsule>()) {
  247. EditableCapsule *cs = es->cast_to<EditableCapsule>();
  248. float radius = es->get_radius();
  249. float height = es->get_height();
  250. Vector<Vector3> points;
  251. Vector3 d(0,height*0.5,0);
  252. for(int i=0;i<360;i++) {
  253. float ra=Math::deg2rad(i);
  254. float rb=Math::deg2rad(i+1);
  255. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*radius;
  256. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*radius;
  257. points.push_back(Vector3(a.x,0,a.y)+d);
  258. points.push_back(Vector3(b.x,0,b.y)+d);
  259. points.push_back(Vector3(a.x,0,a.y)-d);
  260. points.push_back(Vector3(b.x,0,b.y)-d);
  261. if (i%90==0) {
  262. points.push_back(Vector3(a.x,0,a.y)+d);
  263. points.push_back(Vector3(a.x,0,a.y)-d);
  264. }
  265. Vector3 dud = i<180?d:-d;
  266. points.push_back(Vector3(0,a.x,a.y)+dud);
  267. points.push_back(Vector3(0,b.x,b.y)+dud);
  268. points.push_back(Vector3(a.y,a.x,0)+dud);
  269. points.push_back(Vector3(b.y,b.x,0)+dud);
  270. }
  271. add_lines(points,SpatialEditorGizmos::singleton->shape_material);
  272. Vector<Vector3> collision_segments;
  273. for(int i=0;i<64;i++) {
  274. float ra=i*Math_PI*2.0/64.0;
  275. float rb=(i+1)*Math_PI*2.0/64.0;
  276. Point2 a = Vector2(Math::sin(ra),Math::cos(ra))*radius;
  277. Point2 b = Vector2(Math::sin(rb),Math::cos(rb))*radius;
  278. collision_segments.push_back(Vector3(a.x,0,a.y)+d);
  279. collision_segments.push_back(Vector3(b.x,0,b.y)+d);
  280. collision_segments.push_back(Vector3(a.x,0,a.y)-d);
  281. collision_segments.push_back(Vector3(b.x,0,b.y)-d);
  282. if (i%16==0) {
  283. collision_segments.push_back(Vector3(a.x,0,a.y)+d);
  284. collision_segments.push_back(Vector3(a.x,0,a.y)-d);
  285. }
  286. Vector3 dud = i<32?d:-d;
  287. collision_segments.push_back(Vector3(0,a.x,a.y)+dud);
  288. collision_segments.push_back(Vector3(0,b.x,b.y)+dud);
  289. collision_segments.push_back(Vector3(a.y,a.x,0)+dud);
  290. collision_segments.push_back(Vector3(b.y,b.x,0)+dud);
  291. }
  292. add_collision_segments(collision_segments);
  293. Vector<Vector3> handles;
  294. handles.push_back(Vector3(es->get_radius(),0,0));
  295. handles.push_back(Vector3(0,es->get_height()*0.5+es->get_radius(),0));
  296. add_handles(handles);
  297. }
  298. if (es->cast_to<EditablePlane>()) {
  299. EditablePlane* ps=es->cast_to<EditablePlane();
  300. Plane p = ps->get_plane();
  301. Vector<Vector3> points;
  302. Vector3 n1 = p.get_any_perpendicular_normal();
  303. Vector3 n2 = p.normal.cross(n1).normalized();
  304. Vector3 pface[4]={
  305. p.normal*p.d+n1*10.0+n2*10.0,
  306. p.normal*p.d+n1*10.0+n2*-10.0,
  307. p.normal*p.d+n1*-10.0+n2*-10.0,
  308. p.normal*p.d+n1*-10.0+n2*10.0,
  309. };
  310. points.push_back(pface[0]);
  311. points.push_back(pface[1]);
  312. points.push_back(pface[1]);
  313. points.push_back(pface[2]);
  314. points.push_back(pface[2]);
  315. points.push_back(pface[3]);
  316. points.push_back(pface[3]);
  317. points.push_back(pface[0]);
  318. points.push_back(p.normal*p.d);
  319. points.push_back(p.normal*p.d+p.normal*3);
  320. add_lines(points,SpatialEditorGizmos::singleton->shape_material);
  321. add_collision_segments(points);
  322. }
  323. if (es->cast_to<EditableRay>()) {
  324. EditableRay*cs = es->cast_to<EditableRay>();
  325. Vector<Vector3> points;
  326. points.push_back(Vector3());
  327. points.push_back(Vector3(0,0,rs->get_length()));
  328. add_lines(points,SpatialEditorGizmos::singleton->shape_material);
  329. add_collision_segments(points);
  330. Vector<Vector3> handles;
  331. handles.push_back(Vector3(0,0,rs->get_length()));
  332. add_handles(handles);
  333. }
  334. #endif
  335. }
  336. EditableShapeSpatialGizmo::EditableShapeSpatialGizmo(EditableShape* p_cs) {
  337. es=p_cs;
  338. set_spatial_node(p_cs);
  339. }
  340. EditorShapeGizmos::EditorShapeGizmos()
  341. {
  342. }