RibbonTrailDemo.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // Copyright (c) 2008-2017 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include <Urho3D/Core/CoreEvents.h>
  23. #include <Urho3D/Engine/Engine.h>
  24. #include <Urho3D/Graphics/AnimatedModel.h>
  25. #include <Urho3D/Graphics/AnimationController.h>
  26. #include <Urho3D/Graphics/Camera.h>
  27. #include <Urho3D/Graphics/Graphics.h>
  28. #include <Urho3D/Graphics/Material.h>
  29. #include <Urho3D/Graphics/Octree.h>
  30. #include <Urho3D/Graphics/Renderer.h>
  31. #include <Urho3D/Graphics/RibbonTrail.h>
  32. #include <Urho3D/Input/Input.h>
  33. #include <Urho3D/Resource/ResourceCache.h>
  34. #include <Urho3D/Scene/Scene.h>
  35. #include <Urho3D/UI/Font.h>
  36. #include <Urho3D/UI/Text.h>
  37. #include <Urho3D/UI/UI.h>
  38. #include <Urho3D/UI/Text3D.h>
  39. #include "RibbonTrailDemo.h"
  40. #include <Urho3D/DebugNew.h>
  41. URHO3D_DEFINE_APPLICATION_MAIN(RibbonTrailDemo)
  42. RibbonTrailDemo::RibbonTrailDemo(Context* context) :
  43. Sample(context),
  44. swordTrailStartTime_(0.2f),
  45. swordTrailEndTime_(0.46f),
  46. timeStepSum_(0.0f)
  47. {
  48. }
  49. void RibbonTrailDemo::Start()
  50. {
  51. // Execute base class startup
  52. Sample::Start();
  53. // Create the scene content
  54. CreateScene();
  55. // Create the UI content
  56. CreateInstructions();
  57. // Setup the viewport for displaying the scene
  58. SetupViewport();
  59. // Hook up to the frame update events
  60. SubscribeToEvents();
  61. // Set the mouse mode to use in the sample
  62. Sample::InitMouseMode(MM_RELATIVE);
  63. }
  64. void RibbonTrailDemo::CreateScene()
  65. {
  66. ResourceCache* cache = GetSubsystem<ResourceCache>();
  67. scene_ = new Scene(context_);
  68. // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  69. scene_->CreateComponent<Octree>();
  70. // Create scene node & StaticModel component for showing a static plane
  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 directional light to the world.
  77. Node* lightNode = scene_->CreateChild("DirectionalLight");
  78. lightNode->SetDirection(Vector3(0.6f, -1.0f, 0.8f)); // The direction vector does not need to be normalized
  79. Light* light = lightNode->CreateComponent<Light>();
  80. light->SetLightType(LIGHT_DIRECTIONAL);
  81. light->SetCastShadows(true);
  82. light->SetShadowBias(BiasParameters(0.00005f, 0.5f));
  83. // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
  84. light->SetShadowCascade(CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f));
  85. // Create first box for face camera trail demo with 1 column.
  86. boxNode1_ = scene_->CreateChild("Box1");
  87. StaticModel* box1 = boxNode1_->CreateComponent<StaticModel>();
  88. box1->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  89. box1->SetCastShadows(true);
  90. RibbonTrail* boxTrail1 = boxNode1_->CreateComponent<RibbonTrail>();
  91. boxTrail1->SetMaterial(cache->GetResource<Material>("Materials/RibbonTrail.xml"));
  92. boxTrail1->SetStartColor(Color(1.0f, 0.5f, 0.0f, 1.0f));
  93. boxTrail1->SetEndColor(Color(1.0f, 1.0f, 0.0f, 0.0f));
  94. boxTrail1->SetWidth(0.5f);
  95. boxTrail1->SetUpdateInvisible(true);
  96. // Create second box for face camera trail demo with 4 column.
  97. // This will produce less distortion than first trail.
  98. boxNode2_ = scene_->CreateChild("Box2");
  99. StaticModel* box2 = boxNode2_->CreateComponent<StaticModel>();
  100. box2->SetModel(cache->GetResource<Model>("Models/Box.mdl"));
  101. box2->SetCastShadows(true);
  102. RibbonTrail* boxTrail2 = boxNode2_->CreateComponent<RibbonTrail>();
  103. boxTrail2->SetMaterial(cache->GetResource<Material>("Materials/RibbonTrail.xml"));
  104. boxTrail2->SetStartColor(Color(1.0f, 0.5f, 0.0f, 1.0f));
  105. boxTrail2->SetEndColor(Color(1.0f, 1.0f, 0.0f, 0.0f));
  106. boxTrail2->SetWidth(0.5f);
  107. boxTrail2->SetTailColumn(4);
  108. boxTrail2->SetUpdateInvisible(true);
  109. // Load ninja animated model for bone trail demo.
  110. Node* ninjaNode = scene_->CreateChild("Ninja");
  111. ninjaNode->SetPosition(Vector3(5.0f, 0.0f, 0.0f));
  112. ninjaNode->SetRotation(Quaternion(0.0f, 180.0f, 0.0f));
  113. AnimatedModel* ninja = ninjaNode->CreateComponent<AnimatedModel>();
  114. ninja->SetModel(cache->GetResource<Model>("Models/NinjaSnowWar/Ninja.mdl"));
  115. ninja->SetMaterial(cache->GetResource<Material>("Materials/NinjaSnowWar/Ninja.xml"));
  116. ninja->SetCastShadows(true);
  117. // Create animation controller and play attack animation.
  118. ninjaAnimCtrl_ = ninjaNode->CreateComponent<AnimationController>();
  119. ninjaAnimCtrl_->PlayExclusive("Models/NinjaSnowWar/Ninja_Attack3.ani", 0, true, 0.0f);
  120. // Add ribbon trail to tip of sword.
  121. Node* swordTip = ninjaNode->GetChild("Joint29", true);
  122. swordTrail_ = swordTip->CreateComponent<RibbonTrail>();
  123. // Set sword trail type to bone and set other parameters.
  124. swordTrail_->SetTrailType(TT_BONE);
  125. swordTrail_->SetMaterial(cache->GetResource<Material>("Materials/SlashTrail.xml"));
  126. swordTrail_->SetLifetime(0.22f);
  127. swordTrail_->SetStartColor(Color(1.0f, 1.0f, 1.0f, 0.75f));
  128. swordTrail_->SetEndColor(Color(0.2f, 0.5f, 1.0f, 0.0f));
  129. swordTrail_->SetTailColumn(4);
  130. swordTrail_->SetUpdateInvisible(true);
  131. // Add floating text for info.
  132. Node* boxTextNode1 = scene_->CreateChild("BoxText1");
  133. boxTextNode1->SetPosition(Vector3(-1.0f, 2.0f, 0.0f));
  134. Text3D* boxText1 = boxTextNode1->CreateComponent<Text3D>();
  135. boxText1->SetText(String("Face Camera Trail (4 Column)"));
  136. boxText1->SetFont(cache->GetResource<Font>("Fonts/BlueHighway.sdf"), 24);
  137. Node* boxTextNode2 = scene_->CreateChild("BoxText2");
  138. boxTextNode2->SetPosition(Vector3(-6.0f, 2.0f, 0.0f));
  139. Text3D* boxText2 = boxTextNode2->CreateComponent<Text3D>();
  140. boxText2->SetText(String("Face Camera Trail (1 Column)"));
  141. boxText2->SetFont(cache->GetResource<Font>("Fonts/BlueHighway.sdf"), 24);
  142. Node* ninjaTextNode2 = scene_->CreateChild("NinjaText");
  143. ninjaTextNode2->SetPosition(Vector3(4.0f, 2.5f, 0.0f));
  144. Text3D* ninjaText = ninjaTextNode2->CreateComponent<Text3D>();
  145. ninjaText->SetText(String("Bone Trail (4 Column)"));
  146. ninjaText->SetFont(cache->GetResource<Font>("Fonts/BlueHighway.sdf"), 24);
  147. // Create the camera.
  148. cameraNode_ = scene_->CreateChild("Camera");
  149. cameraNode_->CreateComponent<Camera>();
  150. // Set an initial position for the camera scene node above the plane
  151. cameraNode_->SetPosition(Vector3(0.0f, 2.0f, -14.0f));
  152. }
  153. void RibbonTrailDemo::CreateInstructions()
  154. {
  155. ResourceCache* cache = GetSubsystem<ResourceCache>();
  156. UI* ui = GetSubsystem<UI>();
  157. // Construct new Text object, set string to display and font to use
  158. Text* instructionText = ui->GetRoot()->CreateChild<Text>();
  159. instructionText->SetText("Use WASD keys and mouse/touch to move");
  160. instructionText->SetFont(cache->GetResource<Font>("Fonts/Anonymous Pro.ttf"), 15);
  161. // Position the text relative to the screen center
  162. instructionText->SetHorizontalAlignment(HA_CENTER);
  163. instructionText->SetVerticalAlignment(VA_CENTER);
  164. instructionText->SetPosition(0, ui->GetRoot()->GetHeight() / 4);
  165. }
  166. void RibbonTrailDemo::SetupViewport()
  167. {
  168. Renderer* renderer = GetSubsystem<Renderer>();
  169. // 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
  170. // at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  171. // use, but now we just use full screen and default render path configured in the engine command line options
  172. SharedPtr<Viewport> viewport(new Viewport(context_, scene_, cameraNode_->GetComponent<Camera>()));
  173. renderer->SetViewport(0, viewport);
  174. }
  175. void RibbonTrailDemo::MoveCamera(float timeStep)
  176. {
  177. // Do not move if the UI has a focused element (the console)
  178. if (GetSubsystem<UI>()->GetFocusElement())
  179. return;
  180. Input* input = GetSubsystem<Input>();
  181. // Movement speed as world units per second
  182. const float MOVE_SPEED = 20.0f;
  183. // Mouse sensitivity as degrees per pixel
  184. const float MOUSE_SENSITIVITY = 0.1f;
  185. // Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  186. IntVector2 mouseMove = input->GetMouseMove();
  187. yaw_ += MOUSE_SENSITIVITY * mouseMove.x_;
  188. pitch_ += MOUSE_SENSITIVITY * mouseMove.y_;
  189. pitch_ = Clamp(pitch_, -90.0f, 90.0f);
  190. // Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  191. cameraNode_->SetRotation(Quaternion(pitch_, yaw_, 0.0f));
  192. // Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  193. // Use the Translate() function (default local space) to move relative to the node's orientation.
  194. if (input->GetKeyDown(KEY_W))
  195. cameraNode_->Translate(Vector3::FORWARD * MOVE_SPEED * timeStep);
  196. if (input->GetKeyDown(KEY_S))
  197. cameraNode_->Translate(Vector3::BACK * MOVE_SPEED * timeStep);
  198. if (input->GetKeyDown(KEY_A))
  199. cameraNode_->Translate(Vector3::LEFT * MOVE_SPEED * timeStep);
  200. if (input->GetKeyDown(KEY_D))
  201. cameraNode_->Translate(Vector3::RIGHT * MOVE_SPEED * timeStep);
  202. }
  203. void RibbonTrailDemo::SubscribeToEvents()
  204. {
  205. // Subscribe HandleUpdate() function for processing update events
  206. SubscribeToEvent(E_UPDATE, URHO3D_HANDLER(RibbonTrailDemo, HandleUpdate));
  207. }
  208. void RibbonTrailDemo::HandleUpdate(StringHash eventType, VariantMap& eventData)
  209. {
  210. using namespace Update;
  211. // Take the frame time step, which is stored as a float
  212. float timeStep = eventData[P_TIMESTEP].GetFloat();
  213. // Move the camera, scale movement with time step
  214. MoveCamera(timeStep);
  215. // Sum of timesteps.
  216. timeStepSum_ += timeStep;
  217. // Move first box with pattern.
  218. boxNode1_->SetTransform(Vector3(-4.0f + 3.0f * Cos(100.0f * timeStepSum_), 0.5f, -2.0f * Cos(400.0f * timeStepSum_)),
  219. Quaternion());
  220. // Move second box with pattern.
  221. boxNode2_->SetTransform(Vector3(0.0f + 3.0f * Cos(100.0f * timeStepSum_), 0.5f, -2.0f * Cos(400.0f * timeStepSum_)),
  222. Quaternion());
  223. // Get elapsed attack animation time.
  224. float swordAnimTime = ninjaAnimCtrl_->GetAnimationState(String("Models/NinjaSnowWar/Ninja_Attack3.ani"))->GetTime();
  225. // Stop emitting trail when sword is finished slashing.
  226. if (!swordTrail_->IsEmitting() && swordAnimTime > swordTrailStartTime_ && swordAnimTime < swordTrailEndTime_)
  227. swordTrail_->SetEmitting(true);
  228. else if (swordTrail_->IsEmitting() && swordAnimTime >= swordTrailEndTime_)
  229. swordTrail_->SetEmitting(false);
  230. }