StaticScene.cpp 7.7 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/Input/Input.h>
  13. #include <Urho3D/Resource/ResourceCache.h>
  14. #include <Urho3D/Scene/Scene.h>
  15. #include <Urho3D/UI/Font.h>
  16. #include <Urho3D/UI/Text.h>
  17. #include <Urho3D/UI/UI.h>
  18. #include "StaticScene.h"
  19. #include <Urho3D/DebugNew.h>
  20. URHO3D_DEFINE_APPLICATION_MAIN(StaticScene)
  21. StaticScene::StaticScene(Context* context) :
  22. Sample(context)
  23. {
  24. }
  25. void StaticScene::Start()
  26. {
  27. // Execute base class startup
  28. Sample::Start();
  29. // Create the scene content
  30. CreateScene();
  31. // Create the UI content
  32. CreateInstructions();
  33. // Setup the viewport for displaying the scene
  34. SetupViewport();
  35. // Hook up to the frame update events
  36. SubscribeToEvents();
  37. // Set the mouse mode to use in the sample
  38. Sample::InitMouseMode(MM_RELATIVE);
  39. }
  40. void StaticScene::CreateScene()
  41. {
  42. auto* cache = GetSubsystem<ResourceCache>();
  43. scene_ = new Scene(context_);
  44. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  45. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  46. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  47. // optimizing manner
  48. scene_->CreateComponent<Octree>();
  49. // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  50. // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  51. // (100 x 100 world units)
  52. Node* planeNode = scene_->CreateChild("Plane");
  53. planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f));
  54. auto* planeObject = planeNode->CreateComponent<StaticModel>();
  55. planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl"));
  56. planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));
  57. // Create a directional light to the world so that we can see something. The light scene node's orientation controls the
  58. // light direction; we will use the SetDirection() function which calculates the orientation from a forward direction vector.
  59. // The light will use default settings (white light, no shadows)
  60. Node* lightNode = scene_->CreateChild("DirectionalLight");
  61. lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); // The direction vector does not need to be normalized
  62. auto* light = lightNode->CreateComponent<Light>();
  63. light->SetLightType(LIGHT_DIRECTIONAL);
  64. // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
  65. // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
  66. // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
  67. // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
  68. // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
  69. // scene.
  70. const unsigned NUM_OBJECTS = 200;
  71. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  72. {
  73. Node* mushroomNode = scene_->CreateChild("Mushroom");
  74. mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f));
  75. mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f));
  76. mushroomNode->SetScale(0.5f + Random(2.0f));
  77. auto* mushroomObject = mushroomNode->CreateComponent<StaticModel>();
  78. mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl"));
  79. mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml"));
  80. }
  81. // Create a scene node for the camera, which we will move around
  82. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  83. cameraNode_ = scene_->CreateChild("Camera");
  84. cameraNode_->CreateComponent<Camera>();
  85. // Set an initial position for the camera scene node above the plane
  86. cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f));
  87. }
  88. void StaticScene::CreateInstructions()
  89. {
  90. auto* cache = GetSubsystem<ResourceCache>();
  91. auto* ui = GetSubsystem<UI>();
  92. // Construct new Text object, set string to display and font to use
  93. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  94. instructionText->SetText("Use WASD keys and mouse/touch to move");
  95. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  96. // Position the text relative to the screen center
  97. instructionText->SetHorizontalAlignment(HA_CENTER);
  98. instructionText->SetVerticalAlignment(VA_CENTER);
  99. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  100. }
  101. void StaticScene::SetupViewport()
  102. {
  103. auto* renderer = GetSubsystem<Renderer>();
  104. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen. We need to define the scene and the camera
  105. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  106. // use, but now we just use full screen and default render path configured in the engine command line options
  107. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  108. renderer->SetViewport(0, viewport);
  109. }
  110. void StaticScene::MoveCamera(float timeStep)
  111. {
  112. // Do not move if the UI has a focused element (the console)
  113. if (GetSubsystem<UI>()->GetFocusElement())
  114. return;
  115. auto* input = GetSubsystem<Input>();
  116. // Movement speed as world units per second
  117. const float MOVE_SPEED = 20.0f;
  118. // Mouse sensitivity as degrees per pixel
  119. const float MOUSE_SENSITIVITY = 0.1f;
  120. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  121. IntVector2 mouseMove = input->GetMouseMove();
  122. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  123. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  124. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  125. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  126. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  127. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  128. // Use the Translate() function (default local space) to move relative to the node's orientation.
  129. if (input->GetKeyDown(KEY_W))
  130. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  131. if (input->GetKeyDown(KEY_S))
  132. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  133. if (input->GetKeyDown(KEY_A))
  134. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  135. if (input->GetKeyDown(KEY_D))
  136. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  137. }
  138. void StaticScene::SubscribeToEvents()
  139. {
  140. // Subscribe HandleUpdate() function for processing update events
  141. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(StaticScene, HandleUpdate));
  142. }
  143. void StaticScene::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. }