LightAnimation.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. //
  2. // Copyright (c) 2008-2016 the Urho3D project.
  3. // Copyright (c) 2014-2016, THUNDERBEAST GAMES LLC All rights reserved
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include <Atomic/Core/CoreEvents.h>
  24. #include <Atomic/Engine/Engine.h>
  25. #include <Atomic/Graphics/Camera.h>
  26. #include <Atomic/Graphics/Graphics.h>
  27. #include <Atomic/Graphics/Material.h>
  28. #include <Atomic/Graphics/Model.h>
  29. #include <Atomic/Graphics/Octree.h>
  30. #include <Atomic/Graphics/Renderer.h>
  31. #include <Atomic/Graphics/StaticModel.h>
  32. #include <Atomic/Input/Input.h>
  33. #include <Atomic/Resource/ResourceCache.h>
  34. #include <Atomic/Scene/ObjectAnimation.h>
  35. #include <Atomic/Scene/Scene.h>
  36. #include <Atomic/Scene/ValueAnimation.h>
  37. #include <Atomic/UI/UI.h>
  38. #include "LightAnimation.h"
  39. #include <Atomic/DebugNew.h>
  40. LightAnimation::LightAnimation(Context* context) :
  41. Sample(context)
  42. {
  43. }
  44. void LightAnimation::Start()
  45. {
  46. // Execute base class startup
  47. Sample::Start();
  48. // Create the UI content
  49. CreateInstructions();
  50. // Create the scene content
  51. CreateScene();
  52. // Setup the viewport for displaying the scene
  53. SetupViewport();
  54. // Hook up to the frame update events
  55. SubscribeToEvents();
  56. // Set the mouse mode to use in the sample
  57. Sample::InitMouseMode(MM_RELATIVE);
  58. }
  59. void LightAnimation::CreateScene()
  60. {
  61. ResourceCache* cache = GetSubsystem<ResourceCache>();
  62. scene_ = new Scene(context_);
  63. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  64. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  65. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  66. // optimizing manner
  67. scene_->CreateComponent<Octree>();
  68. // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  69. // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  70. // (100 x 100 world units)
  71. Node* planeNode = scene_->CreateChild("Plane");
  72. planeNode->SetScale(Vector3(100.0f, 1.0f, 100.0f));
  73. StaticModel* planeObject = planeNode->CreateComponent<StaticModel>();
  74. planeObject->SetModel(cache->GetResource<Model>("Models/Plane.mdl"));
  75. planeObject->SetMaterial(cache->GetResource<Material>("Materials/StoneTiled.xml"));
  76. // Create a point light to the world so that we can see something.
  77. Node* lightNode = scene_->CreateChild("PointLight");
  78. Light* light = lightNode->CreateComponent<Light>();
  79. light->SetLightType(LIGHT_POINT);
  80. light->SetRange(10.0f);
  81. // Create light animation
  82. SharedPtr<ObjectAnimation> lightAnimation(new ObjectAnimation(context_));
  83. // Create light position animation
  84. SharedPtr<ValueAnimation> positionAnimation(new ValueAnimation(context_));
  85. // Use spline interpolation method
  86. positionAnimation->SetInterpolationMethod(IM_SPLINE);
  87. // Set spline tension
  88. positionAnimation->SetSplineTension(0.7f);
  89. positionAnimation->SetKeyFrame(0.0f, Vector3(-30.0f, 5.0f, -30.0f));
  90. positionAnimation->SetKeyFrame(1.0f, Vector3( 30.0f, 5.0f, -30.0f));
  91. positionAnimation->SetKeyFrame(2.0f, Vector3( 30.0f, 5.0f, 30.0f));
  92. positionAnimation->SetKeyFrame(3.0f, Vector3(-30.0f, 5.0f, 30.0f));
  93. positionAnimation->SetKeyFrame(4.0f, Vector3(-30.0f, 5.0f, -30.0f));
  94. // Set position animation
  95. lightAnimation->AddAttributeAnimation("Position", positionAnimation);
  96. // Create light color animation
  97. SharedPtr<ValueAnimation> colorAnimation(new ValueAnimation(context_));
  98. colorAnimation->SetKeyFrame(0.0f, Color::WHITE);
  99. colorAnimation->SetKeyFrame(1.0f, Color::RED);
  100. colorAnimation->SetKeyFrame(2.0f, Color::YELLOW);
  101. colorAnimation->SetKeyFrame(3.0f, Color::GREEN);
  102. colorAnimation->SetKeyFrame(4.0f, Color::WHITE);
  103. // Set Light component's color animation
  104. lightAnimation->AddAttributeAnimation("@Light/Color", colorAnimation);
  105. // Apply light animation to light node
  106. lightNode->SetObjectAnimation(lightAnimation);
  107. // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
  108. // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
  109. // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
  110. // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
  111. // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
  112. // scene.
  113. const unsigned NUM_OBJECTS = 200;
  114. for (unsigned i = 0; i < NUM_OBJECTS; ++i)
  115. {
  116. Node* mushroomNode = scene_->CreateChild("Mushroom");
  117. mushroomNode->SetPosition(Vector3(Random(90.0f) - 45.0f, 0.0f, Random(90.0f) - 45.0f));
  118. mushroomNode->SetRotation(Quaternion(0.0f, Random(360.0f), 0.0f));
  119. mushroomNode->SetScale(0.5f + Random(2.0f));
  120. StaticModel* mushroomObject = mushroomNode->CreateComponent<StaticModel>();
  121. mushroomObject->SetModel(cache->GetResource<Model>("Models/Mushroom.mdl"));
  122. mushroomObject->SetMaterial(cache->GetResource<Material>("Materials/Mushroom.xml"));
  123. }
  124. // Create a scene node for the camera, which we will move around
  125. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  126. cameraNode_ = scene_->CreateChild("Camera");
  127. cameraNode_->CreateComponent<Camera>();
  128. // Set an initial position for the camera scene node above the plane
  129. cameraNode_->SetPosition(Vector3(0.0f, 5.0f, 0.0f));
  130. }
  131. void LightAnimation::CreateInstructions()
  132. {
  133. SimpleCreateInstructions("Use WASD keys and mouse/touch to move");
  134. }
  135. void LightAnimation::SetupViewport()
  136. {
  137. Renderer* renderer = GetSubsystem<Renderer>();
  138. // 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
  139. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  140. // use, but now we just use full screen and default render path configured in the engine command line options
  141. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  142. renderer->SetViewport(0, viewport);
  143. }
  144. void LightAnimation::MoveCamera(float timeStep)
  145. {
  146. Input* input = GetSubsystem<Input>();
  147. // Movement speed as world units per second
  148. const float MOVE_SPEED = 20.0f;
  149. // Mouse sensitivity as degrees per pixel
  150. const float MOUSE_SENSITIVITY = 0.1f;
  151. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  152. IntVector2 mouseMove = input->GetMouseMove();
  153. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  154. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  155. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  156. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  157. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  158. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  159. // Use the Translate() function (default local space) to move relative to the node's orientation.
  160. if (input->GetKeyDown(KEY_W))
  161. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  162. if (input->GetKeyDown(KEY_S))
  163. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  164. if (input->GetKeyDown(KEY_A))
  165. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  166. if (input->GetKeyDown(KEY_D))
  167. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  168. }
  169. void LightAnimation::SubscribeToEvents()
  170. {
  171. // Subscribe HandleUpdate() function for processing update events
  172. SubscribeToEvent(E_UPDATE, ATOMIC_HANDLER(LightAnimation, HandleUpdate));
  173. }
  174. void LightAnimation::HandleUpdate(StringHash eventType, VariantMap& eventData)
  175. {
  176. using namespace Update;
  177. // Take the frame time step, which is stored as a float
  178. float timeStep = eventData[P_TIMESTEP].GetFloat();
  179. // Move the camera, scale movement with time step
  180. MoveCamera(timeStep);
  181. }