AnimatingScene.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Engine/Engine.h>
  5. #include <Urho3D/Graphics/Camera.h>
  6. #include <Urho3D/Graphics/Graphics.h>
  7. #include <Urho3D/Graphics/Material.h>
  8. #include <Urho3D/Graphics/Model.h>
  9. #include <Urho3D/Graphics/Octree.h>
  10. #include <Urho3D/Graphics/Renderer.h>
  11. #include <Urho3D/Graphics/StaticModel.h>
  12. #include <Urho3D/Graphics/Zone.h>
  13. #include <Urho3D/Input/Input.h>
  14. #include <Urho3D/Resource/ResourceCache.h>
  15. #include <Urho3D/Scene/Scene.h>
  16. #include <Urho3D/UI/Font.h>
  17. #include <Urho3D/UI/Text.h>
  18. #include <Urho3D/UI/UI.h>
  19. #include "AnimatingScene.h"
  20. #include "Rotator.h"
  21. #include <Urho3D/DebugNew.h>
  22. URHO3D_DEFINE_APPLICATION_MAIN(AnimatingScene)
  23. AnimatingScene::AnimatingScene(Context* context) :
  24. Sample(context)
  25. {
  26. // Register an object factory for our custom Rotator component so that we can create them to scene nodes
  27. context->RegisterFactory<Rotator>();
  28. }
  29. void AnimatingScene::Start()
  30. {
  31. // Execute base class startup
  32. Sample::Start();
  33. // Create the scene content
  34. CreateScene();
  35. // Create the UI content
  36. CreateInstructions();
  37. // Setup the viewport for displaying the scene
  38. SetupViewport();
  39. // Hook up to the frame update events
  40. SubscribeToEvents();
  41. // Set the mouse mode to use in the sample
  42. Sample::InitMouseMode(MM_RELATIVE);
  43. }
  44. void AnimatingScene::CreateScene()
  45. {
  46. auto* cache = GetSubsystem<ResourceCache>();
  47. scene_ = new Scene(context_);
  48. // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
  49. // (-1000, -1000, -1000) to (1000, 1000, 1000)
  50. scene_->CreateComponent<Octree>();
  51. // Create a Zone component into a child scene node. The Zone controls ambient lighting and fog settings. Like the Octree,
  52. // it also defines its volume with a bounding box, but can be rotated (so it does not need to be aligned to the world X, Y
  53. // and Z axes.) Drawable objects "pick up" the zone they belong to and use it when rendering; several zones can exist
  54. Node* zoneNode = scene_->CreateChild("Zone");
  55. auto* zone = zoneNode->CreateComponent<Zone>();
  56. // Set same volume as the Octree, set a close bluish fog and some ambient light
  57. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  58. zone->SetAmbientColor(Color(0.05f, 0.1f, 0.15f));
  59. zone->SetFogColor(Color(0.1f, 0.2f, 0.3f));
  60. zone->SetFogStart(10.0f);
  61. zone->SetFogEnd(100.0f);
  62. // Create randomly positioned and oriented box StaticModels in the scene
  63. const unsigned NUM_OBJECTS = 2000;
  64. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  65. {
  66. Node* boxNode = scene_->CreateChild("Box");
  67. boxNode->SetPosition(Vector3(Random(200.0f) - 100.0f, Random(200.0f) - 100.0f, Random(200.0f) - 100.0f));
  68. // Orient using random pitch, yaw and roll Euler angles
  69. boxNode->SetRotation(Quaternion(Random(360.0f), Random(360.0f), Random(360.0f)));
  70. auto* boxObject = boxNode->CreateComponent<StaticModel>();
  71. boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  72. boxObject->SetMaterial(cache->GetResource<Material>("Materials/Stone.xml"));
  73. // Add our custom Rotator component which will rotate the scene node each frame, when the scene sends its update event.
  74. // The Rotator component derives from the base class LogicComponent, which has convenience functionality to subscribe
  75. // to the various update events, and forward them to virtual functions that can be implemented by subclasses. This way
  76. // writing logic/update components in C++ becomes similar to scripting.
  77. // Now we simply set same rotation speed for all objects
  78. auto* rotator = boxNode->CreateComponent<Rotator>();
  79. rotator->SetRotationSpeed(Vector3(10.0f, 20.0f, 30.0f));
  80. }
  81. // Create the camera. Let the starting position be at the world origin. As the fog limits maximum visible distance, we can
  82. // bring the far clip plane closer for more effective culling of distant objects
  83. cameraNode_ = scene_->CreateChild("Camera");
  84. auto* camera = cameraNode_->CreateComponent<Camera>();
  85. camera->SetFarClip(100.0f);
  86. // Create a point light to the camera scene node
  87. auto* light = cameraNode_->CreateComponent<Light>();
  88. light->SetLightType(LIGHT_POINT);
  89. light->SetRange(30.0f);
  90. }
  91. void AnimatingScene::CreateInstructions()
  92. {
  93. auto* cache = GetSubsystem<ResourceCache>();
  94. auto* ui = GetSubsystem<UI>();
  95. // Construct new Text object, set string to display and font to use
  96. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  97. instructionText->SetText("Use WASD keys and mouse/touch to move");
  98. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  99. // Position the text relative to the screen center
  100. instructionText->SetHorizontalAlignment(HA_CENTER);
  101. instructionText->SetVerticalAlignment(VA_CENTER);
  102. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  103. }
  104. void AnimatingScene::SetupViewport()
  105. {
  106. auto* renderer = GetSubsystem<Renderer>();
  107. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  108. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  109. renderer->SetViewport(0, viewport);
  110. }
  111. void AnimatingScene::SubscribeToEvents()
  112. {
  113. // Subscribe HandleUpdate() function for processing update events
  114. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(AnimatingScene, HandleUpdate));
  115. }
  116. void AnimatingScene::MoveCamera(float timeStep)
  117. {
  118. // Do not move if the UI has a focused element (the console)
  119. if (GetSubsystem<UI>()->GetFocusElement())
  120. return;
  121. auto* input = GetSubsystem<Input>();
  122. // Movement speed as world units per second
  123. const float MOVE_SPEED = 20.0f;
  124. // Mouse sensitivity as degrees per pixel
  125. const float MOUSE_SENSITIVITY = 0.1f;
  126. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  127. IntVector2 mouseMove = input->GetMouseMove();
  128. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  129. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  130. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  131. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  132. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  133. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  134. if (input->GetKeyDown(KEY_W))
  135. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  136. if (input->GetKeyDown(KEY_S))
  137. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  138. if (input->GetKeyDown(KEY_A))
  139. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  140. if (input->GetKeyDown(KEY_D))
  141. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  142. }
  143. void AnimatingScene::HandleUpdate(StringHash eventType, VariantMap& eventData)
  144. {
  145. using namespace Update;
  146. // Take the frame time step, which is stored as a float
  147. float timeStep = eventData[P_TIMESTEP].GetFloat();
  148. // Move the camera, scale movement with time step
  149. MoveCamera(timeStep);
  150. }