30_LightAnimation.as 9.5 KB

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