LightAnimation.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/ObjectAnimation.h>
  15. #include <Urho3D/Scene/Scene.h>
  16. #include <Urho3D/Scene/ValueAnimation.h>
  17. #include <Urho3D/UI/Font.h>
  18. #include <Urho3D/UI/Sprite.h>
  19. #include <Urho3D/UI/Text.h>
  20. #include <Urho3D/UI/UI.h>
  21. #include "LightAnimation.h"
  22. #include <Urho3D/DebugNew.h>
  23. URHO3D_DEFINE_APPLICATION_MAIN(LightAnimation)
  24. LightAnimation::LightAnimation(Context* context) :
  25. Sample(context)
  26. {
  27. }
  28. void LightAnimation::Start()
  29. {
  30. // Execute base class startup
  31. Sample::Start();
  32. // Create the UI content
  33. CreateInstructions();
  34. // Create the scene content
  35. CreateScene();
  36. // Setup the viewport for displaying the scene
  37. SetupViewport();
  38. // Hook up to the frame update events
  39. SubscribeToEvents();
  40. // Set the mouse mode to use in the sample
  41. Sample::InitMouseMode(MM_RELATIVE);
  42. }
  43. void LightAnimation::CreateScene()
  44. {
  45. auto* cache = GetSubsystem<ResourceCache>();
  46. scene_ = new Scene(context_);
  47. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  48. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  49. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  50. // optimizing manner
  51. scene_->CreateComponent<Octree>();
  52. // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  53. // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  54. // (100 x 100 world units)
  55. Node* planeNode = scene_->CreateChild("Plane");
  56. planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f));
  57. auto* planeObject = planeNode->CreateComponent<StaticModel>();
  58. planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl"));
  59. planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));
  60. // Create a point light to the world so that we can see something.
  61. Node* lightNode = scene_->CreateChild("PointLight");
  62. auto* light = lightNode->CreateComponent<Light>();
  63. light->SetLightType(LIGHT_POINT);
  64. light->SetRange(10.0f);
  65. // Create light animation
  66. SharedPtr<ObjectAnimation> lightAnimation(new ObjectAnimation(context_));
  67. // Create light position animation
  68. SharedPtr<ValueAnimation> positionAnimation(new ValueAnimation(context_));
  69. // Use spline interpolation method
  70. positionAnimation->SetInterpolationMethod(IM_SPLINE);
  71. // Set spline tension
  72. positionAnimation->SetSplineTension(0.7f);
  73. positionAnimation->SetKeyFrame(0.0f, Vector3(-30.0f, 5.0f, -30.0f));
  74. positionAnimation->SetKeyFrame(1.0f, Vector3( 30.0f, 5.0f, -30.0f));
  75. positionAnimation->SetKeyFrame(2.0f, Vector3( 30.0f, 5.0f, 30.0f));
  76. positionAnimation->SetKeyFrame(3.0f, Vector3(-30.0f, 5.0f, 30.0f));
  77. positionAnimation->SetKeyFrame(4.0f, Vector3(-30.0f, 5.0f, -30.0f));
  78. // Set position animation
  79. lightAnimation->AddAttributeAnimation("Position", positionAnimation);
  80. // Create text animation
  81. SharedPtr<ValueAnimation> textAnimation(new ValueAnimation(context_));
  82. textAnimation->SetKeyFrame(0.0f, "WHITE");
  83. textAnimation->SetKeyFrame(1.0f, "RED");
  84. textAnimation->SetKeyFrame(2.0f, "YELLOW");
  85. textAnimation->SetKeyFrame(3.0f, "GREEN");
  86. textAnimation->SetKeyFrame(4.0f, "WHITE");
  87. GetSubsystem<UI>()->GetRoot()->GetChild(String("animatingText"))->SetAttributeAnimation("Text", textAnimation);
  88. // Create UI element animation
  89. // (note: a spritesheet and "Image Rect" attribute should be used in real use cases for better performance)
  90. SharedPtr<ValueAnimation> spriteAnimation(new ValueAnimation(context_));
  91. spriteAnimation->SetKeyFrame(0.0f, ResourceRef("Texture2D", "Urho2D/GoldIcon/1.png"));
  92. spriteAnimation->SetKeyFrame(0.1f, ResourceRef("Texture2D", "Urho2D/GoldIcon/2.png"));
  93. spriteAnimation->SetKeyFrame(0.2f, ResourceRef("Texture2D", "Urho2D/GoldIcon/3.png"));
  94. spriteAnimation->SetKeyFrame(0.3f, ResourceRef("Texture2D", "Urho2D/GoldIcon/4.png"));
  95. spriteAnimation->SetKeyFrame(0.4f, ResourceRef("Texture2D", "Urho2D/GoldIcon/5.png"));
  96. spriteAnimation->SetKeyFrame(0.5f, ResourceRef("Texture2D", "Urho2D/GoldIcon/1.png"));
  97. GetSubsystem<UI>()->GetRoot()->GetChild(String("animatingSprite"))->SetAttributeAnimation("Texture", spriteAnimation);
  98. // Create light color animation
  99. SharedPtr<ValueAnimation> colorAnimation(new ValueAnimation(context_));
  100. colorAnimation->SetKeyFrame(0.0f, Color::WHITE);
  101. colorAnimation->SetKeyFrame(1.0f, Color::RED);
  102. colorAnimation->SetKeyFrame(2.0f, Color::YELLOW);
  103. colorAnimation->SetKeyFrame(3.0f, Color::GREEN);
  104. colorAnimation->SetKeyFrame(4.0f, Color::WHITE);
  105. // Set Light component's color animation
  106. lightAnimation->AddAttributeAnimation("@Light/Color", colorAnimation);
  107. // Apply light animation to light node
  108. lightNode->SetObjectAnimation(lightAnimation);
  109. // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
  110. // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
  111. // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
  112. // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
  113. // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
  114. // scene.
  115. const unsigned NUM_OBJECTS = 200;
  116. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  117. {
  118. Node* mushroomNode = scene_->CreateChild("Mushroom");
  119. mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f));
  120. mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f));
  121. mushroomNode->SetScale(0.5f + Random(2.0f));
  122. auto* mushroomObject = mushroomNode->CreateComponent<StaticModel>();
  123. mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl"));
  124. mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml"));
  125. }
  126. // Create a scene node for the camera, which we will move around
  127. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  128. cameraNode_ = scene_->CreateChild("Camera");
  129. cameraNode_->CreateComponent<Camera>();
  130. // Set an initial position for the camera scene node above the plane
  131. cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f));
  132. }
  133. void LightAnimation::CreateInstructions()
  134. {
  135. auto* cache = GetSubsystem<ResourceCache>();
  136. auto* ui = GetSubsystem<UI>();
  137. // Construct new Text object, set string to display and font to use
  138. auto* instructionText = ui->GetRoot()->CreateChild<Text>();
  139. instructionText->SetText("Use WASD keys and mouse/touch to move");
  140. auto* font = cache->GetResource<Font>("Fonts/Anonymous Pro.ttf");
  141. instructionText->SetFont(font, 15);
  142. // Position the text relative to the screen center
  143. instructionText->SetHorizontalAlignment(HA_CENTER);
  144. instructionText->SetVerticalAlignment(VA_CENTER);
  145. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  146. // Animating text
  147. auto* text = ui->GetRoot()->CreateChild<Text>("animatingText");
  148. text->SetFont(font, 15);
  149. text->SetHorizontalAlignment(HA_CENTER);
  150. text->SetVerticalAlignment(VA_CENTER);
  151. text->SetPosition(0, ui->GetRoot()->GetHeight() / 4 + 20);
  152. // Animating sprite in the top left corner
  153. auto* sprite = ui->GetRoot()->CreateChild<Sprite>("animatingSprite");
  154. sprite->SetPosition(8, 8);
  155. sprite->SetSize(64, 64);
  156. }
  157. void LightAnimation::SetupViewport()
  158. {
  159. auto* renderer = GetSubsystem<Renderer>();
  160. // 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
  161. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  162. // use, but now we just use full screen and default render path configured in the engine command line options
  163. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  164. renderer->SetViewport(0, viewport);
  165. }
  166. void LightAnimation::MoveCamera(float timeStep)
  167. {
  168. // Do not move if the UI has a focused element (the console)
  169. if (GetSubsystem<UI>()->GetFocusElement())
  170. return;
  171. auto* input = GetSubsystem<Input>();
  172. // Movement speed as world units per second
  173. const float MOVE_SPEED = 20.0f;
  174. // Mouse sensitivity as degrees per pixel
  175. const float MOUSE_SENSITIVITY = 0.1f;
  176. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  177. IntVector2 mouseMove = input->GetMouseMove();
  178. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  179. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  180. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  181. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  182. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  183. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  184. // Use the Translate() function (default local space) to move relative to the node's orientation.
  185. if (input->GetKeyDown(KEY_W))
  186. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  187. if (input->GetKeyDown(KEY_S))
  188. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  189. if (input->GetKeyDown(KEY_A))
  190. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  191. if (input->GetKeyDown(KEY_D))
  192. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  193. }
  194. void LightAnimation::SubscribeToEvents()
  195. {
  196. // Subscribe HandleUpdate() function for processing update events
  197. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(LightAnimation, HandleUpdate));
  198. }
  199. void LightAnimation::HandleUpdate(StringHash eventType, VariantMap& eventData)
  200. {
  201. using namespace Update;
  202. // Take the frame time step, which is stored as a float
  203. float timeStep = eventData[P_TIMESTEP].GetFloat();
  204. // Move the camera, scale movement with time step
  205. MoveCamera(timeStep);
  206. }