Gizmo3D.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. // Portions Copyright (c) 2008-2015 the Urho3D project.
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. // Please see LICENSE.md in repository root for license information
  4. // https://github.com/AtomicGameEngine/AtomicGameEngine
  5. #include <Atomic/Atomic3D/Model.h>
  6. #include <Atomic/Graphics/Material.h>
  7. #include <Atomic/Graphics/Octree.h>
  8. #include <Atomic/Resource/ResourceCache.h>
  9. #include <Atomic/Input/Input.h>
  10. #include "Gizmo3D.h"
  11. namespace AtomicEditor
  12. {
  13. Gizmo3D::Gizmo3D(Context* context) : Object(context)
  14. {
  15. ResourceCache* cache = GetSubsystem<ResourceCache>();
  16. gizmoNode_ = new Node(context_);
  17. gizmo_ = gizmoNode_->CreateComponent<StaticModel>();
  18. gizmo_->SetModel(cache->GetResource<Model>("AtomicEditor/Models/Axes.mdl"));
  19. gizmo_->SetEnabled(false);
  20. gizmo_->SetViewMask(0x80000000); // Editor raycasts use viewmask 0x7fffffff
  21. gizmo_->SetOccludee(false);
  22. axisMode_ = AXIS_LOCAL;
  23. gizmoAxisX_.lastSelected_ = false;
  24. gizmoAxisY_.lastSelected_ = false;
  25. gizmoAxisZ_.lastSelected_ = false;
  26. editMode_ = EDIT_MOVE;
  27. lastEditMode_ = EDIT_SELECT;
  28. }
  29. Gizmo3D::~Gizmo3D()
  30. {
  31. }
  32. void Gizmo3D::SetView(SceneView3D* view3D)
  33. {
  34. view3D_ = view3D;
  35. scene_ = view3D->GetScene();
  36. camera_ = view3D->GetCameraNode()->GetComponent<Camera>();
  37. assert(camera_.NotNull());
  38. }
  39. void Gizmo3D::Position()
  40. {
  41. Vector3 center(0, 0, 0);
  42. bool containsScene = false;
  43. for (unsigned i = 0; i < editNodes_->Size(); ++i)
  44. {
  45. // Scene's transform should not be edited, so hide gizmo if it is included
  46. if (editNodes_->At(i) == scene_)
  47. {
  48. containsScene = true;
  49. break;
  50. }
  51. center += editNodes_->At(i)->GetWorldPosition();
  52. }
  53. if (editNodes_->Empty() || containsScene)
  54. {
  55. Hide();
  56. return;
  57. }
  58. center /= editNodes_->Size();
  59. gizmoNode_->SetPosition(center);
  60. if (axisMode_ == AXIS_WORLD || editNodes_->Size() > 1)
  61. gizmoNode_->SetRotation(Quaternion());
  62. else
  63. gizmoNode_->SetRotation(editNodes_->At(0)->GetWorldRotation());
  64. ResourceCache* cache = GetSubsystem<ResourceCache>();
  65. if (editMode_ != lastEditMode_)
  66. {
  67. switch (editMode_)
  68. {
  69. case EDIT_MOVE:
  70. gizmo_->SetModel(cache->GetResource<Model>("AtomicEditor/Models/Axes.mdl"));
  71. break;
  72. case EDIT_ROTATE:
  73. gizmo_->SetModel(cache->GetResource<Model>("AtomicEditor/Models/RotateAxes.mdl"));
  74. break;
  75. case EDIT_SCALE:
  76. gizmo_->SetModel(cache->GetResource<Model>("AtomicEditor/Models/ScaleAxes.mdl"));
  77. break;
  78. default:
  79. break;
  80. }
  81. lastEditMode_ = editMode_;
  82. }
  83. bool orbiting = false;
  84. if ((editMode_ != EDIT_SELECT && !orbiting) && !gizmo_->IsEnabled())
  85. Show();
  86. else if ((editMode_ == EDIT_SELECT || orbiting) && gizmo_->IsEnabled())
  87. Hide();
  88. if (gizmo_->IsEnabled())
  89. {
  90. float scale = 0.1f / camera_->GetZoom();
  91. if (camera_->IsOrthographic())
  92. scale *= camera_->GetOrthoSize();
  93. else
  94. scale *= (camera_->GetView() * gizmoNode_->GetPosition()).z_;
  95. gizmoNode_->SetScale(Vector3(scale, scale, scale));
  96. }
  97. }
  98. void Gizmo3D::Update(Vector<Node *> &editNodes)
  99. {
  100. editNodes_ = &editNodes;
  101. Use();
  102. Position();
  103. }
  104. void Gizmo3D::CalculateGizmoAxes()
  105. {
  106. gizmoAxisX_.axisRay_ = Ray(gizmoNode_->GetPosition(), gizmoNode_->GetRotation() * Vector3(1, 0, 0));
  107. gizmoAxisY_.axisRay_ = Ray(gizmoNode_->GetPosition(), gizmoNode_->GetRotation() * Vector3(0, 1, 0));
  108. gizmoAxisZ_.axisRay_ = Ray(gizmoNode_->GetPosition(), gizmoNode_->GetRotation() * Vector3(0, 0, 1));
  109. }
  110. void Gizmo3D::Use()
  111. {
  112. if (gizmo_.Null() || !gizmo_->IsEnabled() || editMode_ == EDIT_SELECT)
  113. {
  114. //StoreGizmoEditActions();
  115. //previousGizmoDrag = false;
  116. return;
  117. }
  118. ResourceCache* cache = GetSubsystem<ResourceCache>();
  119. Input* input = GetSubsystem<Input>();
  120. Ray cameraRay = view3D_->GetCameraRay();
  121. float scale = gizmoNode_->GetScale().x_;
  122. // Recalculate axes only when not left-dragging
  123. bool drag = input->GetMouseButtonDown(MOUSEB_LEFT);// && (Abs(input->GetMouseMoveX()) > 3 || Abs(input->GetMouseMoveY()) > 3);
  124. if (!drag)
  125. CalculateGizmoAxes();
  126. gizmoAxisX_.Update(cameraRay, scale, drag, camera_->GetNode());
  127. gizmoAxisY_.Update(cameraRay, scale, drag, camera_->GetNode());
  128. gizmoAxisZ_.Update(cameraRay, scale, drag, camera_->GetNode());
  129. if (!editNodes_->Size() || editNodes_->At(0) == scene_)
  130. {
  131. gizmoAxisX_.selected_ = gizmoAxisY_.selected_ = gizmoAxisZ_.selected_ = false;
  132. // this just forces an update
  133. gizmoAxisX_.lastSelected_ = gizmoAxisY_.lastSelected_ = gizmoAxisZ_.lastSelected_ = true;
  134. }
  135. if (gizmoAxisX_.selected_ != gizmoAxisX_.lastSelected_)
  136. {
  137. gizmo_->SetMaterial(0, cache->GetResource<Material>(
  138. gizmoAxisX_.selected_ ?
  139. "AtomicEditor/Materials/BrightRedUnlit.xml" : "AtomicEditor/Materials/RedUnlit.xml"));
  140. gizmoAxisX_.lastSelected_ = gizmoAxisX_.selected_;
  141. }
  142. if (gizmoAxisY_.selected_ != gizmoAxisY_.lastSelected_)
  143. {
  144. gizmo_->SetMaterial(1, cache->GetResource<Material>(
  145. gizmoAxisY_.selected_ ?
  146. "AtomicEditor/Materials/BrightGreenUnlit.xml" : "AtomicEditor/Materials/GreenUnlit.xml"));
  147. gizmoAxisY_.lastSelected_ = gizmoAxisY_.selected_;
  148. }
  149. if (gizmoAxisZ_.selected_ != gizmoAxisZ_.lastSelected_)
  150. {
  151. gizmo_->SetMaterial(2, cache->GetResource<Material>(
  152. gizmoAxisZ_.selected_ ?
  153. "AtomicEditor/Materials/BrightBlueUnlit.xml" : "AtomicEditor/Materials/BlueUnlit.xml"));
  154. gizmoAxisZ_.lastSelected_ = gizmoAxisZ_.selected_;
  155. }
  156. if (drag)
  157. Drag();
  158. }
  159. bool Gizmo3D::MoveEditNodes(Vector3 adjust)
  160. {
  161. bool moved = false;
  162. if (adjust.Length() > M_EPSILON)
  163. {
  164. for (unsigned i = 0; i < editNodes_->Size(); ++i)
  165. {
  166. /*
  167. if (moveSnap)
  168. {
  169. float moveStepScaled = moveStep * snapScale;
  170. adjust.x = Floor(adjust.x / moveStepScaled + 0.5) * moveStepScaled;
  171. adjust.y = Floor(adjust.y / moveStepScaled + 0.5) * moveStepScaled;
  172. adjust.z = Floor(adjust.z / moveStepScaled + 0.5) * moveStepScaled;
  173. }
  174. */
  175. Node* node = editNodes_->At(i);
  176. Vector3 nodeAdjust = adjust;
  177. if (axisMode_ == AXIS_LOCAL && editNodes_->Size() == 1)
  178. nodeAdjust = node->GetWorldRotation() * nodeAdjust;
  179. Vector3 worldPos = node->GetWorldPosition();
  180. Vector3 oldPos = node->GetPosition();
  181. worldPos += nodeAdjust;
  182. if (!node->GetParent())
  183. node->SetPosition(worldPos);
  184. else
  185. node->SetPosition(node->GetParent()->WorldToLocal(worldPos));
  186. if (node->GetPosition() != oldPos)
  187. moved = true;
  188. }
  189. }
  190. return moved;
  191. }
  192. bool Gizmo3D::RotateEditNodes(Vector3 adjust)
  193. {
  194. bool moved = false;
  195. /*
  196. if (rotateSnap)
  197. {
  198. float rotateStepScaled = rotateStep * snapScale;
  199. adjust.x = Floor(adjust.x / rotateStepScaled + 0.5) * rotateStepScaled;
  200. adjust.y = Floor(adjust.y / rotateStepScaled + 0.5) * rotateStepScaled;
  201. adjust.z = Floor(adjust.z / rotateStepScaled + 0.5) * rotateStepScaled;
  202. }
  203. */
  204. if (adjust.Length() > M_EPSILON)
  205. {
  206. moved = true;
  207. for (unsigned i = 0; i < editNodes_->Size(); ++i)
  208. {
  209. Node* node = editNodes_->At(i);
  210. Quaternion rotQuat(adjust.x_, adjust.y_, adjust.z_);
  211. if (axisMode_ == AXIS_LOCAL && editNodes_->Size() == 1)
  212. node->SetRotation(node->GetRotation() * rotQuat);
  213. else
  214. {
  215. Vector3 offset = node->GetWorldPosition() - gizmoAxisX_.axisRay_.origin_;
  216. if (node->GetParent() && node->GetParent()->GetWorldRotation() != Quaternion(1, 0, 0, 0))
  217. rotQuat = node->GetParent()->GetWorldRotation().Inverse() * rotQuat * node->GetParent()->GetWorldRotation();
  218. node->SetRotation(rotQuat * node->GetRotation());
  219. Vector3 newPosition = gizmoAxisX_.axisRay_.origin_ + rotQuat * offset;
  220. if (node->GetParent())
  221. newPosition = node->GetParent()->WorldToLocal(newPosition);
  222. node->SetPosition(newPosition);
  223. }
  224. }
  225. }
  226. return moved;
  227. }
  228. bool Gizmo3D::ScaleEditNodes(Vector3 adjust)
  229. {
  230. bool moved = false;
  231. if (adjust.Length() > M_EPSILON)
  232. {
  233. for (unsigned i = 0; i < editNodes_->Size(); ++i)
  234. {
  235. Node* node = editNodes_->At(i);
  236. Vector3 scale = node->GetScale();
  237. Vector3 oldScale = scale;
  238. if (true)//!scaleSnap)
  239. scale += adjust;
  240. else
  241. {
  242. /*
  243. float scaleStepScaled = scaleStep * snapScale;
  244. if (adjust.x != 0)
  245. {
  246. scale.x += adjust.x * scaleStepScaled;
  247. scale.x = Floor(scale.x / scaleStepScaled + 0.5) * scaleStepScaled;
  248. }
  249. if (adjust.y != 0)
  250. {
  251. scale.y += adjust.y * scaleStepScaled;
  252. scale.y = Floor(scale.y / scaleStepScaled + 0.5) * scaleStepScaled;
  253. }
  254. if (adjust.z != 0)
  255. {
  256. scale.z += adjust.z * scaleStepScaled;
  257. scale.z = Floor(scale.z / scaleStepScaled + 0.5) * scaleStepScaled;
  258. }
  259. */
  260. }
  261. if (scale != oldScale)
  262. moved = true;
  263. node->SetScale(scale);
  264. }
  265. }
  266. return moved;
  267. }
  268. void Gizmo3D::Moved()
  269. {
  270. gizmoAxisX_.Moved();
  271. gizmoAxisY_.Moved();
  272. gizmoAxisZ_.Moved();
  273. }
  274. void Gizmo3D::Drag()
  275. {
  276. bool moved = false;
  277. float scale = gizmoNode_->GetScale().x_;
  278. if (editMode_ == EDIT_MOVE)
  279. {
  280. Vector3 adjust(0, 0, 0);
  281. if (gizmoAxisX_.selected_)
  282. adjust += Vector3(1, 0, 0) * (gizmoAxisX_.t_ - gizmoAxisX_.lastT_);
  283. if (gizmoAxisY_.selected_)
  284. adjust += Vector3(0, 1, 0) * (gizmoAxisY_.t_ - gizmoAxisY_.lastT_);
  285. if (gizmoAxisZ_.selected_)
  286. adjust += Vector3(0, 0, 1) * (gizmoAxisZ_.t_ - gizmoAxisZ_.lastT_);
  287. moved = MoveEditNodes(adjust);
  288. }
  289. else if (editMode_ == EDIT_ROTATE)
  290. {
  291. const float rotSensitivity = 50.0;
  292. Vector3 adjust(0, 0, 0);
  293. if (gizmoAxisX_.selected_)
  294. adjust.x_ = (gizmoAxisX_.d_ - gizmoAxisX_.lastD_) * rotSensitivity / scale;
  295. if (gizmoAxisY_.selected_)
  296. adjust.y_ = -(gizmoAxisY_.d_ - gizmoAxisY_.lastD_) * rotSensitivity / scale;
  297. if (gizmoAxisZ_.selected_)
  298. adjust.z_ = (gizmoAxisZ_.d_ - gizmoAxisZ_.lastD_) * rotSensitivity / scale;
  299. moved = RotateEditNodes(adjust);
  300. }
  301. else if (editMode_ == EDIT_SCALE)
  302. {
  303. Vector3 adjust(0, 0, 0);
  304. if (gizmoAxisX_.selected_)
  305. adjust += Vector3(1, 0, 0) * (gizmoAxisX_.t_ - gizmoAxisX_.lastT_);
  306. if (gizmoAxisY_.selected_)
  307. adjust += Vector3(0, 1, 0) * (gizmoAxisY_.t_ - gizmoAxisY_.lastT_);
  308. if (gizmoAxisZ_.selected_)
  309. adjust += Vector3(0, 0, 1) * (gizmoAxisZ_.t_ - gizmoAxisZ_.lastT_);
  310. // Special handling for uniform scale: use the unmodified X-axis movement only
  311. if (editMode_ == EDIT_SCALE && gizmoAxisX_.selected_ && gizmoAxisY_.selected_ && gizmoAxisZ_.selected_)
  312. {
  313. float x = gizmoAxisX_.t_ - gizmoAxisX_.lastT_;
  314. adjust = Vector3(x, x, x);
  315. }
  316. moved = ScaleEditNodes(adjust);
  317. }
  318. if (moved)
  319. {
  320. Moved();
  321. //UpdateNodeAttributes();
  322. //needGizmoUndo = true;
  323. }
  324. }
  325. void Gizmo3D::SetEditMode(EditMode mode)
  326. {
  327. editMode_ = mode;
  328. }
  329. void Gizmo3D::Hide()
  330. {
  331. gizmo_->SetEnabled(false);
  332. }
  333. void Gizmo3D::Show()
  334. {
  335. if (scene_.Null())
  336. return;
  337. gizmo_->SetEnabled(true);
  338. Octree* octree = scene_->GetComponent<Octree>();
  339. if (!octree)
  340. return;
  341. octree->AddManualDrawable(gizmo_);
  342. }
  343. }