30_LightAnimation.lua 9.3 KB

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