SceneView3D.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. UIView3D(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. delegate_->SetIsFocusable(true);
  66. }
  67. SceneView3D::~SceneView3D()
  68. {
  69. }
  70. void SceneView3D::Enable()
  71. {
  72. if (enabled_)
  73. return;
  74. enabled_ = true;
  75. view3DWidget_->SetVisibilility(WIDGET_VISIBILITY_VISIBLE);
  76. }
  77. void SceneView3D::Disable()
  78. {
  79. if (!enabled_)
  80. return;
  81. enabled_ = false;
  82. view3DWidget_->SetVisibilility(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. TBRect rect = GetWidgetDelegate()->GetRect();
  135. if (!rect.w || !rect.h)
  136. return camRay;
  137. GetWidgetDelegate()->ConvertToRoot(rect.x, rect.y);
  138. return camera_->GetScreenRay(float(cpos.x_ - rect.x) / rect.w,
  139. float(cpos.y_ - rect.y) /rect.h);
  140. }
  141. void SceneView3D::DrawNodeDebug(Node* node, DebugRenderer* debug, bool drawNode)
  142. {
  143. if (drawNode)
  144. debug->AddNode(node, 1.0, false);
  145. // Exception for the scene to avoid bringing the editor to its knees: drawing either the whole hierarchy or the subsystem-
  146. // components can have a large performance hit. Also do not draw terrain child nodes due to their large amount
  147. // (TerrainPatch component itself draws nothing as debug geometry)
  148. if (node != scene_ && !node->GetComponent<Terrain>())
  149. {
  150. const Vector<SharedPtr<Component> >& components = node->GetComponents();
  151. for (unsigned j = 0; j < components.Size(); ++j)
  152. components[j]->DrawDebugGeometry(debug, false);
  153. // To avoid cluttering the view, do not draw the node axes for child nodes
  154. for (unsigned k = 0; k < node->GetNumChildren(); ++k)
  155. DrawNodeDebug(node->GetChild(k), debug, false);
  156. }
  157. }
  158. bool SceneView3D::MouseInView()
  159. {
  160. Input* input = GetSubsystem<Input>();
  161. IntVector2 pos = input->GetMousePosition();
  162. TBRect rect = GetWidgetDelegate()->GetRect();
  163. GetWidgetDelegate()->ConvertToRoot(rect.x, rect.y);
  164. return rect.Contains(TBPoint(pos.x_, pos.y_));
  165. }
  166. void SceneView3D::HandlePostRenderUpdate(StringHash eventType, VariantMap& eventData)
  167. {
  168. // Visualize the currently selected nodes
  169. if (selectedNode_.NotNull())
  170. {
  171. DrawNodeDebug(selectedNode_, debugRenderer_);
  172. }
  173. if (!MouseInView())
  174. return;
  175. Input* input = GetSubsystem<Input>();
  176. mouseLeftDown_ = false;
  177. if (input->GetMouseButtonPress(MOUSEB_LEFT))
  178. {
  179. if (!mouseMoved_ && !sceneEditor_->GetGizmo()->Selected())
  180. {
  181. Ray camRay = GetCameraRay();
  182. PODVector<RayQueryResult> result;
  183. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  184. octree_->RaycastSingle(query);
  185. if (query.result_.Size())
  186. {
  187. const RayQueryResult& r = result[0];
  188. if (r.drawable_)
  189. {
  190. VariantMap neventData;
  191. neventData[EditorActiveNodeChange::P_NODE] = r.drawable_->GetNode();
  192. SendEvent(E_EDITORACTIVENODECHANGE, neventData);
  193. }
  194. }
  195. }
  196. mouseMoved_ = false;
  197. }
  198. else if (!input->GetMouseButtonDown(MOUSEB_LEFT))
  199. {
  200. Ray camRay = GetCameraRay();
  201. PODVector<RayQueryResult> result;
  202. mouseMoved_ = false;
  203. /*
  204. Array<int> pickModeDrawableFlags = {
  205. DRAWABLE_GEOMETRY,
  206. DRAWABLE_LIGHT,
  207. DRAWABLE_ZONE
  208. };
  209. */
  210. RayOctreeQuery query(result, camRay, RAY_TRIANGLE, camera_->GetFarClip(), DRAWABLE_GEOMETRY, 0x7fffffff);
  211. octree_->RaycastSingle(query);
  212. if (query.result_.Size())
  213. {
  214. const RayQueryResult& r = result[0];
  215. if (r.drawable_)
  216. {
  217. debugRenderer_->AddNode(r.drawable_->GetNode(), 1.0, false);
  218. r.drawable_->DrawDebugGeometry(debugRenderer_, false);
  219. }
  220. }
  221. }
  222. else
  223. {
  224. mouseLeftDown_ = true;
  225. if (Abs(input->GetMouseMoveX() > 3 || input->GetMouseMoveY() > 3))
  226. {
  227. mouseMoved_ = true;
  228. }
  229. }
  230. }
  231. void SceneView3D::SelectNode(Node* node)
  232. {
  233. selectedNode_ = node;
  234. }
  235. bool SceneView3D::OnEvent(const TBWidgetEvent &ev)
  236. {
  237. return sceneEditor_->OnEvent(ev);
  238. }
  239. void SceneView3D::HandleUpdate(StringHash eventType, VariantMap& eventData)
  240. {
  241. // Timestep parameter is same no matter what event is being listened to
  242. float timeStep = eventData[Update::P_TIMESTEP].GetFloat();
  243. if (MouseInView())
  244. MoveCamera(timeStep);
  245. QueueUpdate();
  246. }
  247. void SceneView3D::HandleEditorActiveNodeChange(StringHash eventType, VariantMap& eventData)
  248. {
  249. Node* node = (Node*) (eventData[EditorActiveNodeChange::P_NODE].GetPtr());
  250. SelectNode(node);
  251. }
  252. }