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. namespace AtomicEditor
  25. {
  26. SceneView3D ::SceneView3D(Context* context, SceneEditor3D *sceneEditor) :
  27. UISceneView(context),
  28. yaw_(0.0f),
  29. pitch_(0.0f),
  30. mouseLeftDown_(false),
  31. mouseMoved_(false),
  32. enabled_(true)
  33. {
  34. sceneEditor_ = sceneEditor;
  35. ResourceCache* cache = GetSubsystem<ResourceCache>();
  36. scene_ = sceneEditor->GetScene();
  37. debugRenderer_ = scene_->GetComponent<DebugRenderer>();
  38. if (debugRenderer_.Null())
  39. {
  40. debugRenderer_ = scene_->CreateComponent<DebugRenderer>();
  41. }
  42. octree_ = scene_->GetComponent<Octree>();
  43. if (octree_.Null())
  44. {
  45. LOGWARNING("Scene without an octree loaded");
  46. octree_ = scene_->CreateComponent<Octree>();
  47. }
  48. cameraNode_ = scene_->CreateChild("Camera");
  49. cameraNode_->SetTemporary(true);
  50. camera_ = cameraNode_->CreateComponent<Camera>();
  51. debugRenderer_ = scene_->GetComponent<DebugRenderer>();
  52. assert(debugRenderer_.NotNull());
  53. octree_ = scene_->GetComponent<Octree>();
  54. assert(octree_.NotNull());
  55. cameraNode_->SetPosition(Vector3(0, 0, -10));
  56. SetView(scene_, camera_);
  57. SetAutoUpdate(false);
  58. SubscribeToEvent(E_UPDATE, HANDLER(SceneView3D, HandleUpdate));
  59. SubscribeToEvent(E_EDITORACTIVENODECHANGE, HANDLER(SceneView3D, HandleEditorActiveNodeChange));
  60. SubscribeToEvent(E_POSTRENDERUPDATE, HANDLER(SceneView3D, HandlePostRenderUpdate));
  61. // TODO: generate this event properly
  62. VariantMap eventData;
  63. eventData[EditorActiveSceneChange::P_SCENE] = scene_;
  64. SendEvent(E_EDITORACTIVESCENECHANGE, eventData);
  65. SetIsFocusable(true);
  66. }
  67. SceneView3D::~SceneView3D()
  68. {
  69. }
  70. void SceneView3D::Enable()
  71. {
  72. if (enabled_)
  73. return;
  74. enabled_ = true;
  75. SetVisibility(WIDGET_VISIBILITY_VISIBLE);
  76. }
  77. void SceneView3D::Disable()
  78. {
  79. if (!enabled_)
  80. return;
  81. enabled_ = false;
  82. SetVisibility(WIDGET_VISIBILITY_INVISIBLE);
  83. }
  84. void SceneView3D::MoveCamera(float timeStep)
  85. {
  86. if (!enabled_)
  87. return;
  88. Input* input = GetSubsystem<Input>();
  89. // Movement speed as world units per second
  90. float MOVE_SPEED = 20.0f;
  91. // Mouse sensitivity as degrees per pixel
  92. const float MOUSE_SENSITIVITY = 0.2f;
  93. if (input->GetKeyDown(KEY_LSHIFT) || input->GetKeyDown(KEY_RSHIFT))
  94. MOVE_SPEED *= 3.0f;
  95. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  96. if (input->GetMouseButtonDown(MOUSEB_RIGHT))
  97. {
  98. IntVector2 mouseMove = input->GetMouseMove();
  99. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  100. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  101. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  102. // Not working on OSX
  103. //input->SetMouseMode(MM_RELATIVE);
  104. }
  105. else
  106. {
  107. // Not working on OSX
  108. /*
  109. if (input->GetMouseMode() != MM_ABSOLUTE)
  110. input->SetMouseMode(MM_ABSOLUTE);
  111. */
  112. }
  113. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  114. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  115. //Vector3 pos = cameraNode_->GetWorldPosition();
  116. //Quaternion q = cameraNode_->GetWorldRotation();
  117. //LOGINFOF("%f %f %f : %f %f %f %f", pos.x_, pos.y_, pos.z_, q.x_, q.y_, q.z_, q.w_ );
  118. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  119. // Use the Translate() function (default local space) to move relative to the node's orientation.
  120. if (input->GetKeyDown('W'))
  121. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  122. if (input->GetKeyDown('S'))
  123. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  124. if (input->GetKeyDown('A'))
  125. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  126. if (input->GetKeyDown('D'))
  127. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  128. }
  129. Ray SceneView3D::GetCameraRay()
  130. {
  131. Ray camRay;
  132. Input* input = GetSubsystem<Input>();
  133. IntVector2 cpos = input->GetMousePosition();
  134. IntRect rect = GetRect();
  135. if (!rect.Width() || !rect.Height())
  136. return camRay;
  137. int x = rect.left_;
  138. int y = rect.top_;
  139. GetInternalWidget()->ConvertToRoot(x, y);
  140. return camera_->GetScreenRay(float(cpos.x_ - x) / rect.Width(),
  141. float(cpos.y_ - y) / rect.Height());
  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. Input* input = GetSubsystem<Input>();
  163. IntVector2 pos = input->GetMousePosition();
  164. IntRect rect = GetRect();
  165. GetInternalWidget()->ConvertToRoot(rect.left_, rect.top_);
  166. GetInternalWidget()->ConvertToRoot(rect.right_, rect.bottom_);
  167. return rect.IsInside(pos);
  168. }
  169. void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  170. {
  171. // Visualize the currently selected nodes
  172. if (selectedNode_.NotNull())
  173. {
  174. DrawNodeDebug(selectedNode_, debugRenderer_);
  175. }
  176. if (!MouseInView())
  177. return;
  178. Input* input = GetSubsystem<Input>();
  179. mouseLeftDown_ = false;
  180. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  181. {
  182. if (!mouseMoved_ && !sceneEditor_->GetGizmo()->Selected())
  183. {
  184. Ray camRay = GetCameraRay();
  185. PODVector<RayQueryResult> result;
  186. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  187. octree_->RaycastSingle(query);
  188. if (query.result_.Size())
  189. {
  190. const RayQueryResult& r = result[0];
  191. if (r.drawable_)
  192. {
  193. VariantMap neventData;
  194. neventData[EditorActiveNodeChange::P_NODE] = r.drawable_->GetNode();
  195. SendEvent(E_EDITORACTIVENODECHANGE, neventData);
  196. }
  197. }
  198. }
  199. mouseMoved_ = false;
  200. }
  201. else if (!input->GetMouseButtonDown(MOUSEB_LEFT))
  202. {
  203. Ray camRay = GetCameraRay();
  204. PODVector<RayQueryResult> result;
  205. mouseMoved_ = false;
  206. /*
  207. Array<int> pickModeDrawableFlags = {
  208. DRAWABLE_GEOMETRY,
  209. DRAWABLE_LIGHT,
  210. DRAWABLE_ZONE
  211. };
  212. */
  213. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  214. octree_->RaycastSingle(query);
  215. if (query.result_.Size())
  216. {
  217. const RayQueryResult& r = result[0];
  218. if (r.drawable_)
  219. {
  220. debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false);
  221. r.drawable_->DrawDebugGeometry(debugRenderer_, false);
  222. }
  223. }
  224. }
  225. else
  226. {
  227. mouseLeftDown_ = true;
  228. if (Abs(input->GetMouseMoveX() > 3 || input->GetMouseMoveY() > 3))
  229. {
  230. mouseMoved_ = true;
  231. }
  232. }
  233. }
  234. void SceneView3D::SelectNode(Node* node)
  235. {
  236. selectedNode_ = node;
  237. }
  238. bool SceneView3D::OnEvent(const TBWidgetEvent &ev)
  239. {
  240. return sceneEditor_->OnEvent(ev);
  241. }
  242. void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  243. {
  244. // Timestep parameter is same no matter what event is being listened to
  245. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  246. if (MouseInView())
  247. MoveCamera(timeStep);
  248. QueueUpdate();
  249. }
  250. void SceneView3D::HandleEditorActiveNodeChange(StringHash eventType, VariantMap& eventData)
  251. {
  252. Node* node = (Node*) (eventData[EditorActiveNodeChange::P_NODE].GetPtr());
  253. SelectNode(node);
  254. }
  255. }