44_RibbonTrailDemo.lua 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. -- Ribbon trail demo.
  2. -- This sample demonstrates how to use both trail types of RibbonTrail component.
  3. require "LuaScripts/Utilities/Sample"
  4. local boxNode1 = nil
  5. local boxNode2 = nil
  6. local swordTrail = nil
  7. local ninjaAnimCtrl = nil
  8. local timeStepSum = 0.0
  9. local swordTrailStartTime = 0.2
  10. local swordTrailEndTime = 0.46
  11. function Start()
  12. -- Execute the common startup for samples
  13. SampleStart()
  14. -- Create the scene content
  15. CreateScene()
  16. -- Create the UI content
  17. CreateInstructions()
  18. -- Setup the viewport for displaying the scene
  19. SetupViewport()
  20. -- Set the mouse mode to use in the sample
  21. SampleInitMouseMode(MM_RELATIVE)
  22. -- Hook up to the frame update events
  23. SubscribeToEvents()
  24. end
  25. function CreateScene()
  26. scene_ = Scene()
  27. -- Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  28. scene_:CreateComponent("Octree")
  29. -- Create scene node & StaticModel component for showing a static plane
  30. local planeNode = scene_:CreateChild("Plane")
  31. planeNode.scale = Vector3(100.0, 1.0, 100.0)
  32. local planeObject = planeNode:CreateComponent("StaticModel")
  33. planeObject.model = cache:GetResource("Model", "Models/Plane.mdl")
  34. planeObject.material = cache:GetResource("Material", "Materials/StoneTiled.xml")
  35. -- Create a directional light to the world.
  36. local lightNode = scene_:CreateChild("DirectionalLight")
  37. lightNode.direction = Vector3(0.6, -1.0, 0.8) -- The direction vector does not need to be normalized
  38. local light = lightNode:CreateComponent("Light")
  39. light.lightType = LIGHT_DIRECTIONAL
  40. light.castShadows = true
  41. light.shadowBias = BiasParameters(0.00005, 0.5)
  42. -- Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
  43. light.shadowCascade = CascadeParameters(10.0, 50.0, 200.0, 0.0, 0.8)
  44. -- Create first box for face camera trail demo with 1 column.
  45. boxNode1 = scene_:CreateChild("Box1")
  46. local box1 = boxNode1:CreateComponent("StaticModel")
  47. box1.model = cache:GetResource("Model", "Models/Box.mdl")
  48. box1.castShadows = true
  49. local boxTrail1 = boxNode1:CreateComponent("RibbonTrail")
  50. boxTrail1.material = cache:GetResource("Material", "Materials/RibbonTrail.xml")
  51. boxTrail1.startColor = Color(1.0, 0.5, 0.0, 1.0)
  52. boxTrail1.endColor = Color(1.0, 1.0, 0.0, 0.0)
  53. boxTrail1.width = 0.5
  54. boxTrail1.updateInvisible = true
  55. -- Create second box for face camera trail demo with 4 column.
  56. -- This will produce less distortion than first trail.
  57. boxNode2 = scene_:CreateChild("Box2")
  58. local box2 = boxNode2:CreateComponent("StaticModel")
  59. box2.model = cache:GetResource("Model", "Models/Box.mdl")
  60. box2.castShadows = true
  61. local boxTrail2 = boxNode2:CreateComponent("RibbonTrail")
  62. boxTrail2.material = cache:GetResource("Material", "Materials/RibbonTrail.xml")
  63. boxTrail2.startColor = Color(1.0, 0.5, 0.0, 1.0)
  64. boxTrail2.endColor = Color(1.0, 1.0, 0.0, 0.0)
  65. boxTrail2.width = 0.5
  66. boxTrail2.tailColumn = 4
  67. boxTrail2.updateInvisible = true
  68. -- Load ninja animated model for bone trail demo.
  69. local ninjaNode = scene_:CreateChild("Ninja")
  70. ninjaNode.position = Vector3(5.0, 0.0, 0.0)
  71. ninjaNode.rotation = Quaternion(0.0, 180.0, 0.0)
  72. local ninja = ninjaNode:CreateComponent("AnimatedModel")
  73. ninja.model = cache:GetResource("Model", "Models/NinjaSnowWar/Ninja.mdl")
  74. ninja.material = cache:GetResource("Material", "Materials/NinjaSnowWar/Ninja.xml")
  75. ninja.castShadows = true
  76. -- Create animation controller and play attack animation.
  77. ninjaAnimCtrl = ninjaNode:CreateComponent("AnimationController")
  78. ninjaAnimCtrl:PlayExclusive("Models/NinjaSnowWar/Ninja_Attack3.ani", 0, true, 0.0)
  79. -- Add ribbon trail to tip of sword.
  80. local swordTip = ninjaNode:GetChild("Joint29", true)
  81. swordTrail = swordTip:CreateComponent("RibbonTrail")
  82. -- Set sword trail type to bone and set other parameters.
  83. swordTrail.trailType = TT_BONE
  84. swordTrail.material = cache:GetResource("Material", "Materials/SlashTrail.xml")
  85. swordTrail.lifetime = 0.22
  86. swordTrail.startColor = Color(1.0, 1.0, 1.0, 0.75)
  87. swordTrail.endColor = Color(0.2, 0.5, 1.0, 0.0)
  88. swordTrail.tailColumn = 4
  89. swordTrail.updateInvisible = true
  90. -- Add floating text for info.
  91. local boxTextNode1 = scene_:CreateChild("BoxText1")
  92. boxTextNode1.position = Vector3(-1.0, 2.0, 0.0)
  93. local boxText1 = boxTextNode1:CreateComponent("Text3D")
  94. boxText1.text = "Face Camera Trail (4 Column)"
  95. boxText1:SetFont(cache:GetResource("Font", "Fonts/BlueHighway.sdf"), 24)
  96. local boxTextNode2 = scene_:CreateChild("BoxText2")
  97. boxTextNode2.position = Vector3(-6.0, 2.0, 0.0)
  98. local boxText2 = boxTextNode2:CreateComponent("Text3D")
  99. boxText2.text = "Face Camera Trail (1 Column)"
  100. boxText2:SetFont(cache:GetResource("Font", "Fonts/BlueHighway.sdf"), 24)
  101. local ninjaTextNode2 = scene_:CreateChild("NinjaText")
  102. ninjaTextNode2.position = Vector3(4.0, 2.5, 0.0)
  103. local ninjaText = ninjaTextNode2:CreateComponent("Text3D")
  104. ninjaText.text = "Bone Trail (4 Column)"
  105. ninjaText:SetFont(cache:GetResource("Font", "Fonts/BlueHighway.sdf"), 24)
  106. -- Create the camera.
  107. cameraNode = scene_:CreateChild("Camera")
  108. cameraNode:CreateComponent("Camera")
  109. -- Set an initial position for the camera scene node above the plane
  110. cameraNode.position = Vector3(0.0, 2.0, -14.0)
  111. end
  112. function CreateInstructions()
  113. -- Construct new Text object, set string to display and font to use
  114. local instructionText = ui.root:CreateChild("Text")
  115. instructionText:SetText("Use WASD keys and mouse to move")
  116. instructionText:SetFont(cache:GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15)
  117. -- Position the text relative to the screen center
  118. instructionText.horizontalAlignment = HA_CENTER
  119. instructionText.verticalAlignment = VA_CENTER
  120. instructionText:SetPosition(0, ui.root.height / 4)
  121. end
  122. function SetupViewport()
  123. -- 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
  124. -- at minimum. Additionally we could configure the viewport screen size and the rendering path (eg. forward / deferred) to
  125. -- use, but now we just use full screen and default render path configured in the engine command line options
  126. local viewport = Viewport:new(scene_, cameraNode:GetComponent("Camera"))
  127. renderer:SetViewport(0, viewport)
  128. end
  129. function MoveCamera(timeStep)
  130. -- Do not move if the UI has a focused element (the console)
  131. if ui.focusElement ~= nil then
  132. return
  133. end
  134. -- Movement speed as world units per second
  135. local MOVE_SPEED = 20.0
  136. -- Mouse sensitivity as degrees per pixel
  137. local MOUSE_SENSITIVITY = 0.1
  138. -- Use this frame's mouse motion to adjust camera node yaw and pitch. Clamp the pitch between -90 and 90 degrees
  139. local mouseMove = input.mouseMove
  140. yaw = yaw +MOUSE_SENSITIVITY * mouseMove.x
  141. pitch = pitch + MOUSE_SENSITIVITY * mouseMove.y
  142. pitch = Clamp(pitch, -90.0, 90.0)
  143. -- Construct new orientation for the camera scene node from yaw and pitch. Roll is fixed to zero
  144. cameraNode.rotation = Quaternion(pitch, yaw, 0.0)
  145. -- Read WASD keys and move the camera scene node to the corresponding direction if they are pressed
  146. -- Use the Translate() function (default local space) to move relative to the node's orientation.
  147. if input:GetKeyDown(KEY_W) then
  148. cameraNode:Translate(Vector3(0.0, 0.0, 1.0) * MOVE_SPEED * timeStep)
  149. end
  150. if input:GetKeyDown(KEY_S) then
  151. cameraNode:Translate(Vector3(0.0, 0.0, -1.0) * MOVE_SPEED * timeStep)
  152. end
  153. if input:GetKeyDown(KEY_A) then
  154. cameraNode:Translate(Vector3(-1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  155. end
  156. if input:GetKeyDown(KEY_D) then
  157. cameraNode:Translate(Vector3(1.0, 0.0, 0.0) * MOVE_SPEED * timeStep)
  158. end
  159. end
  160. function SubscribeToEvents()
  161. -- Subscribe HandleUpdate() function for processing update events
  162. SubscribeToEvent("Update", "HandleUpdate")
  163. end
  164. function HandleUpdate(eventType, eventData)
  165. -- Take the frame time step, which is stored as a float
  166. local timeStep = eventData["TimeStep"]:GetFloat()
  167. -- Move the camera, scale movement with time step
  168. MoveCamera(timeStep)
  169. -- Sum of timesteps.
  170. timeStepSum = timeStepSum + timeStep
  171. -- Move first box with pattern.
  172. boxNode1:SetTransform(Vector3(-4.0 + 3.0 * Cos(100.0 * timeStepSum), 0.5, -2.0 * Cos(400.0 * timeStepSum)), Quaternion())
  173. -- Move second box with pattern.
  174. boxNode2:SetTransform(Vector3(0.0 + 3.0 * Cos(100.0 * timeStepSum), 0.5, -2.0 * Cos(400.0 * timeStepSum)), Quaternion())
  175. -- Get elapsed attack animation time.
  176. local swordAnimTime = ninjaAnimCtrl:GetAnimationState("Models/NinjaSnowWar/Ninja_Attack3.ani").time
  177. -- Stop emitting trail when sword is finished slashing.
  178. if not swordTrail.emitting and swordAnimTime > swordTrailStartTime and swordAnimTime < swordTrailEndTime then
  179. swordTrail.emitting = true
  180. elseif swordTrail.emitting and swordAnimTime >= swordTrailEndTime then
  181. swordTrail.emitting = false
  182. end
  183. end