30_LightAnimation.lua 8.1 KB

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