HugeObjectCount.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #include <Urho3D/Core/CoreEvents.h>
  4. #include <Urho3D/Core/Profiler.h>
  5. #include <Urho3D/Engine/Engine.h>
  6. #include <Urho3D/Graphics/Camera.h>
  7. #include <Urho3D/Graphics/Graphics.h>
  8. #include <Urho3D/Graphics/Material.h>
  9. #include <Urho3D/Graphics/Model.h>
  10. #include <Urho3D/Graphics/Octree.h>
  11. #include <Urho3D/Graphics/Renderer.h>
  12. #include <Urho3D/Graphics/StaticModelGroup.h>
  13. #include <Urho3D/Graphics/Zone.h>
  14. #include <Urho3D/Input/Input.h>
  15. #include <Urho3D/Resource/ResourceCache.h>
  16. #include <Urho3D/Scene/Scene.h>
  17. #include <Urho3D/UI/Font.h>
  18. #include <Urho3D/UI/Text.h>
  19. #include <Urho3D/UI/UI.h>
  20. #include "HugeObjectCount.h"
  21. #include <Urho3D/DebugNew.h>
  22. URHO3D_DEFINE_APPLICATION_MAIN(HugeObjectCount)
  23. HugeObjectCount::HugeObjectCount(Context* context) :
  24. Sample(context),
  25. animate_(false),
  26. useGroups_(false)
  27. {
  28. }
  29. void HugeObjectCount::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 HugeObjectCount::CreateScene()
  45. {
  46. auto* cache = GetSubsystem<ResourceCache>();
  47. if (!scene_)
  48. scene_ = new Scene(context_);
  49. else
  50. {
  51. scene_->Clear();
  52. boxNodes_.Clear();
  53. }
  54. // Create the Octree component to the scene so that drawable objects can be rendered. Use default volume
  55. // (-1000, -1000, -1000) to (1000, 1000, 1000)
  56. scene_->CreateComponent<Octree>();
  57. // Create a Zone for ambient light & fog control
  58. Node* zoneNode = scene_->CreateChild("Zone");
  59. auto* zone = zoneNode->CreateComponent<Zone>();
  60. zone->SetBoundingBox(BoundingBox(-1000.0f, 1000.0f));
  61. zone->SetFogColor(Color(0.2f, 0.2f, 0.2f));
  62. zone->SetFogStart(200.0f);
  63. zone->SetFogEnd(300.0f);
  64. // Create a directional light
  65. Node* lightNode = scene_->CreateChild("DirectionalLight");
  66. lightNode->SetDirection(Vector3(-0.6f, -1.0f, -0.8f)); // The direction vector does not need to be normalized
  67. auto* light = lightNode->CreateComponent<Light>();
  68. light->SetLightType(LIGHT_DIRECTIONAL);
  69. if (!useGroups_)
  70. {
  71. light->SetColor(Color(0.7f, 0.35f, 0.0f));
  72. // Create individual box StaticModels in the scene
  73. for (int y = -125; y < 125; ++y)
  74. {
  75. for (int x = -125; x < 125; ++x)
  76. {
  77. Node* boxNode = scene_->CreateChild("Box");
  78. boxNode->SetPosition(Vector3(x * 0.3f, 0.0f, y * 0.3f));
  79. boxNode->SetScale(0.25f);
  80. auto* boxObject = boxNode->CreateComponent<StaticModel>();
  81. boxObject->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  82. boxNodes_.Push(SharedPtr<Node>(boxNode));
  83. }
  84. }
  85. }
  86. else
  87. {
  88. light->SetColor(Color(0.6f, 0.6f, 0.6f));
  89. light->SetSpecularIntensity(1.5f);
  90. // Create StaticModelGroups in the scene
  91. StaticModelGroup* lastGroup = nullptr;
  92. for (int y = -125; y < 125; ++y)
  93. {
  94. for (int x = -125; x < 125; ++x)
  95. {
  96. // Create new group if no group yet, or the group has already "enough" objects. The tradeoff is between culling
  97. // accuracy and the amount of CPU processing needed for all the objects. Note that the group's own transform
  98. // does not matter, and it does not render anything if instance nodes are not added to it
  99. if (!lastGroup || lastGroup->GetNumInstanceNodes() >= 25 * 25)
  100. {
  101. Node* boxGroupNode = scene_->CreateChild("BoxGroup");
  102. lastGroup = boxGroupNode->CreateComponent<StaticModelGroup>();
  103. lastGroup->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  104. }
  105. Node* boxNode = scene_->CreateChild("Box");
  106. boxNode->SetPosition(Vector3(x * 0.3f, 0.0f, y * 0.3f));
  107. boxNode->SetScale(0.25f);
  108. boxNodes_.Push(SharedPtr<Node>(boxNode));
  109. lastGroup->AddInstanceNode(boxNode);
  110. }
  111. }
  112. }
  113. // Create the camera. Create it outside the scene so that we can clear the whole scene without affecting it
  114. if (!cameraNode_)
  115. {
  116. cameraNode_ = new Node(context_);
  117. cameraNode_->SetPosition(Vector3(0.0f, 10.0f, -100.0f));
  118. auto* camera = cameraNode_->CreateComponent<Camera>();
  119. camera->SetFarClip(300.0f);
  120. }
  121. }
  122. void HugeObjectCount::CreateInstructions()
  123. {
  124. auto* cache = GetSubsystem<ResourceCache>();
  125. auto* ui = GetSubsystem<UI>();
  126. // Construct new Text object, set string to display and font to use
  127. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  128. instructionText->SetText(
  129. "Use WASD keys and mouse/touch to move\n"
  130. "Space to toggle animation\n"
  131. "G to toggle object group optimization"
  132. );
  133. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  134. // The text has multiple rows. Center them in relation to each other
  135. instructionText->SetTextAlignment(HA_CENTER);
  136. // Position the text relative to the screen center
  137. instructionText->SetHorizontalAlignment(HA_CENTER);
  138. instructionText->SetVerticalAlignment(VA_CENTER);
  139. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  140. }
  141. void HugeObjectCount::SetupViewport()
  142. {
  143. auto* renderer = GetSubsystem<Renderer>();
  144. // Set up a viewport to the Renderer subsystem so that the 3D scene can be seen
  145. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  146. renderer->SetViewport(0, viewport);
  147. }
  148. void HugeObjectCount::SubscribeToEvents()
  149. {
  150. // Subscribe HandleUpdate() function for processing update events
  151. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(HugeObjectCount, HandleUpdate));
  152. }
  153. void HugeObjectCount::MoveCamera(float timeStep)
  154. {
  155. // Do not move if the UI has a focused element (the console)
  156. if (GetSubsystem<UI>()->GetFocusElement())
  157. return;
  158. auto* input = GetSubsystem<Input>();
  159. // Movement speed as world units per second
  160. const float MOVE_SPEED = 20.0f;
  161. // Mouse sensitivity as degrees per pixel
  162. const float MOUSE_SENSITIVITY = 0.1f;
  163. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  164. IntVector2 mouseMove = input->GetMouseMove();
  165. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  166. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  167. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  168. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  169. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  170. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  171. if (input->GetKeyDown(KEY_W))
  172. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  173. if (input->GetKeyDown(KEY_S))
  174. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  175. if (input->GetKeyDown(KEY_A))
  176. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  177. if (input->GetKeyDown(KEY_D))
  178. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  179. }
  180. void HugeObjectCount::AnimateObjects(float timeStep)
  181. {
  182. URHO3D_PROFILE(AnimateObjects);
  183. const float ROTATE_SPEED = 15.0f;
  184. // Rotate about the Z axis (roll)
  185. Quaternion rotateQuat(ROTATE_SPEED * timeStep, Vector3::FORWARD);
  186. for (const SharedPtr<Node>& boxNode : boxNodes_)
  187. boxNode->Rotate(rotateQuat);
  188. }
  189. void HugeObjectCount::HandleUpdate(StringHash eventType, VariantMap& eventData)
  190. {
  191. using namespace Update;
  192. // Take the frame time step, which is stored as a float
  193. float timeStep = eventData[P_TIMESTEP].GetFloat();
  194. // Toggle animation with space
  195. auto* input = GetSubsystem<Input>();
  196. if (input->GetKeyPress(KEY_SPACE))
  197. animate_ = !animate_;
  198. // Toggle grouped / ungrouped mode
  199. if (input->GetKeyPress(KEY_G))
  200. {
  201. useGroups_ = !useGroups_;
  202. CreateScene();
  203. }
  204. // Move the camera, scale movement with time step
  205. MoveCamera(timeStep);
  206. // Animate scene if enabled
  207. if (animate_)
  208. AnimateObjects(timeStep);
  209. }