SceneView3D.cpp 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "AtomicEditor.h"
  6. #include <Atomic/IO/Log.h>
  7. #include <Atomic/Core/CoreEvents.h>
  8. #include <Atomic/Scene/Scene.h>
  9. #include <Atomic/Graphics/Camera.h>
  10. #include <Atomic/Graphics/Graphics.h>
  11. #include <Atomic/Graphics/DebugRenderer.h>
  12. #include <Atomic/Graphics/Viewport.h>
  13. #include <Atomic/Graphics/Octree.h>
  14. #include <Atomic/Atomic3D/Terrain.h>
  15. #include <Atomic/Input/Input.h>
  16. #include <Atomic/IO/FileSystem.h>
  17. #include <Atomic/Resource/ResourceCache.h>
  18. #include <Atomic/Physics/PhysicsWorld.h>
  19. #include "AEEditor.h"
  20. #include "AEEvents.h"
  21. #include "SceneView3D.h"
  22. #include "SceneEditor3D.h"
  23. #include <Atomic/UI/UI.h>
  24. #include <Atomic/UI/TBUI.h>
  25. namespace AtomicEditor
  26. {
  27. SceneView3D ::SceneView3D(Context* context, SceneEditor3D *sceneEditor) :
  28. UIView3D(context),
  29. yaw_(0.0f),
  30. pitch_(0.0f),
  31. mouseLeftDown_(false),
  32. mouseMoved_(false),
  33. enabled_(true)
  34. {
  35. sceneEditor_ = sceneEditor;
  36. ResourceCache* cache = GetSubsystem<ResourceCache>();
  37. scene_ = sceneEditor->GetScene();
  38. debugRenderer_ = scene_->GetComponent<DebugRenderer>();
  39. if (debugRenderer_.Null())
  40. {
  41. debugRenderer_ = scene_->CreateComponent<DebugRenderer>();
  42. }
  43. octree_ = scene_->GetComponent<Octree>();
  44. if (octree_.Null())
  45. {
  46. LOGWARNING("Scene without an octree loaded");
  47. octree_ = scene_->CreateComponent<Octree>();
  48. }
  49. cameraNode_ = scene_->CreateChild("Camera");
  50. cameraNode_->SetTemporary(true);
  51. camera_ = cameraNode_->CreateComponent<Camera>();
  52. debugRenderer_ = scene_->GetComponent<DebugRenderer>();
  53. assert(debugRenderer_.NotNull());
  54. octree_ = scene_->GetComponent<Octree>();
  55. assert(octree_.NotNull());
  56. cameraNode_->SetPosition(Vector3(0, 0, -10));
  57. SetView(scene_, camera_);
  58. SetAutoUpdate(false);
  59. SubscribeToEvent(E_UPDATE, HANDLER(SceneView3D, HandleUpdate));
  60. SubscribeToEvent(E_EDITORACTIVENODECHANGE, HANDLER(SceneView3D, HandleEditorActiveNodeChange));
  61. SubscribeToEvent(E_POSTRENDERUPDATE, HANDLER(SceneView3D, HandlePostRenderUpdate));
  62. // TODO: generate this event properly
  63. VariantMap eventData;
  64. eventData[EditorActiveSceneChange::P_SCENE] = scene_;
  65. SendEvent(E_EDITORACTIVESCENECHANGE, eventData);
  66. delegate_->SetIsFocusable(true);
  67. }
  68. SceneView3D::~SceneView3D()
  69. {
  70. }
  71. void SceneView3D::Enable()
  72. {
  73. if (enabled_)
  74. return;
  75. enabled_ = true;
  76. view3DWidget_->SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
  77. }
  78. void SceneView3D::Disable()
  79. {
  80. if (!enabled_)
  81. return;
  82. enabled_ = false;
  83. view3DWidget_->SetVisibilility(WIDGET_VISIBILITY_INVISIBLE);
  84. }
  85. void SceneView3D::MoveCamera(float timeStep)
  86. {
  87. // Do not move if the UI has a focused element (the console)
  88. if (GetSubsystem<UI>()->GetFocusElement() || !enabled_)
  89. return;
  90. Input* input = GetSubsystem<Input>();
  91. // Movement speed as world units per second
  92. float MOVE_SPEED = 20.0f;
  93. // Mouse sensitivity as degrees per pixel
  94. const float MOUSE_SENSITIVITY = 0.2f;
  95. if (input->GetKeyDown(KEY_LSHIFT) || input->GetKeyDown(KEY_RSHIFT))
  96. MOVE_SPEED *= 3.0f;
  97. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  98. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  99. {
  100. IntVector2 mouseMove = input->GetMouseMove();
  101. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  102. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  103. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  104. // Not working on OSX
  105. //input->SetMouseMode(MM_RELATIVE);
  106. }
  107. else
  108. {
  109. // Not working on OSX
  110. /*
  111. if (input->GetMouseMode() != MM_ABSOLUTE)
  112. input->SetMouseMode(MM_ABSOLUTE);
  113. */
  114. }
  115. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  116. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  117. //Vector3 pos = cameraNode_->GetWorldPosition();
  118. //Quaternion q = cameraNode_->GetWorldRotation();
  119. //LOGINFOF("%f %f %f : %f %f %f %f", pos.x_, pos.y_, pos.z_, q.x_, q.y_, q.z_, q.w_ );
  120. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  121. // Use the Translate() function (default local space) to move relative to the node's orientation.
  122. if (input->GetKeyDown('W'))
  123. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  124. if (input->GetKeyDown('S'))
  125. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  126. if (input->GetKeyDown('A'))
  127. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  128. if (input->GetKeyDown('D'))
  129. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  130. }
  131. Ray SceneView3D::GetCameraRay()
  132. {
  133. Ray camRay;
  134. UI* ui = GetSubsystem<UI>();
  135. IntVector2 cpos = ui->GetCursorPosition();
  136. TBRect rect = GetWidgetDelegate()->GetRect();
  137. if (!rect.w || !rect.h)
  138. return camRay;
  139. GetWidgetDelegate()->ConvertToRoot(rect.x, rect.y);
  140. return camera_->GetScreenRay(float(cpos.x_ - rect.x) / rect.w,
  141. float(cpos.y_ - rect.y) /rect.h);
  142. }
  143. void SceneView3D::DrawNodeDebug(Node* node, DebugRenderer* debug, bool drawNode)
  144. {
  145. if (drawNode)
  146. debug->AddNode(node, 1.0, false);
  147. // Exception for the scene to avoid bringing the editor to its knees: drawing either the whole hierarchy or the subsystem-
  148. // components can have a large performance hit. Also do not draw terrain child nodes due to their large amount
  149. // (TerrainPatch component itself draws nothing as debug geometry)
  150. if (node != scene_ && !node->GetComponent<Terrain>())
  151. {
  152. const Vector<SharedPtr<Component> >& components = node->GetComponents();
  153. for (unsigned j = 0; j < components.Size(); ++j)
  154. components[j]->DrawDebugGeometry(debug, false);
  155. // To avoid cluttering the view, do not draw the node axes for child nodes
  156. for (unsigned k = 0; k < node->GetNumChildren(); ++k)
  157. DrawNodeDebug(node->GetChild(k), debug, false);
  158. }
  159. }
  160. bool SceneView3D::MouseInView()
  161. {
  162. UI* ui = GetSubsystem<UI>();
  163. IntVector2 pos = ui->GetCursorPosition();
  164. TBRect rect = GetWidgetDelegate()->GetRect();
  165. GetWidgetDelegate()->ConvertToRoot(rect.x, rect.y);
  166. return rect.Contains(TBPoint(pos.x_, pos.y_));
  167. }
  168. void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  169. {
  170. // Visualize the currently selected nodes
  171. if (selectedNode_.NotNull())
  172. {
  173. DrawNodeDebug(selectedNode_, debugRenderer_);
  174. }
  175. if (!MouseInView())
  176. return;
  177. Input* input = GetSubsystem<Input>();
  178. mouseLeftDown_ = false;
  179. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  180. {
  181. if (!mouseMoved_ && !sceneEditor_->GetGizmo()->Selected())
  182. {
  183. Ray camRay = GetCameraRay();
  184. PODVector<RayQueryResult> result;
  185. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  186. octree_->RaycastSingle(query);
  187. if (query.result_.Size())
  188. {
  189. const RayQueryResult& r = result[0];
  190. if (r.drawable_)
  191. {
  192. VariantMap neventData;
  193. neventData[EditorActiveNodeChange::P_NODE] = r.drawable_->GetNode();
  194. SendEvent(E_EDITORACTIVENODECHANGE, neventData);
  195. }
  196. }
  197. }
  198. mouseMoved_ = false;
  199. }
  200. else if (!input->GetMouseButtonDown(MOUSEB_LEFT))
  201. {
  202. Ray camRay = GetCameraRay();
  203. PODVector<RayQueryResult> result;
  204. mouseMoved_ = false;
  205. /*
  206. Array<int> pickModeDrawableFlags = {
  207. DRAWABLE_GEOMETRY,
  208. DRAWABLE_LIGHT,
  209. DRAWABLE_ZONE
  210. };
  211. */
  212. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  213. octree_->RaycastSingle(query);
  214. if (query.result_.Size())
  215. {
  216. const RayQueryResult& r = result[0];
  217. if (r.drawable_)
  218. {
  219. debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false);
  220. r.drawable_->DrawDebugGeometry(debugRenderer_, false);
  221. }
  222. }
  223. }
  224. else
  225. {
  226. mouseLeftDown_ = true;
  227. if (Abs(input->GetMouseMoveX() > 3 || input->GetMouseMoveY() > 3))
  228. {
  229. mouseMoved_ = true;
  230. }
  231. }
  232. }
  233. void SceneView3D::SelectNode(Node* node)
  234. {
  235. selectedNode_ = node;
  236. }
  237. bool SceneView3D::OnEvent(const TBWidgetEvent &ev)
  238. {
  239. return sceneEditor_->OnEvent(ev);
  240. }
  241. void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  242. {
  243. // Timestep parameter is same no matter what event is being listened to
  244. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  245. if (MouseInView())
  246. MoveCamera(timeStep);
  247. QueueUpdate();
  248. }
  249. void SceneView3D::HandleEditorActiveNodeChange(StringHash eventType, VariantMap& eventData)
  250. {
  251. Node* node = (Node*) (eventData[EditorActiveNodeChange::P_NODE].GetPtr());
  252. SelectNode(node);
  253. }
  254. }