44_RibbonTrailDemo.as 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Ribbon trail demo.
  2. // This sample demonstrates how to use both trail types of RibbonTrail component.
  3. #include "Scripts/Utilities/Sample.as"
  4. Node@ boxNode1;
  5. Node@ boxNode2;
  6. RibbonTrail@ swordTrail;
  7. AnimationController@ ninjaAnimCtrl;
  8. float timeStepSum = 0.0f;
  9. float swordTrailStartTime = 0.2f;
  10. float swordTrailEndTime = 0.46f;
  11. void Start()
  12. {
  13. // Execute the common startup for samples
  14. SampleStart();
  15. // Create the scene content
  16. CreateScene();
  17. // Create the UI content
  18. CreateInstructions();
  19. // Setup the viewport for displaying the scene
  20. SetupViewport();
  21. // Set the mouse mode to use in the sample
  22. SampleInitMouseMode(MM_RELATIVE);
  23. // Hook up to the frame update events
  24. SubscribeToEvents();
  25. }
  26. void CreateScene()
  27. {
  28. scene_ = Scene();
  29. // Create octree, use default volume (-1000, -1000, -1000) to (1000, 1000, 1000)
  30. scene_.CreateComponent("Octree");
  31. // Create scene node & StaticModel component for showing a static plane
  32. Node@ planeNode = scene_.CreateChild("Plane");
  33. planeNode.scale = Vector3(100.0f, 1.0f, 100.0f);
  34. StaticModel@ planeObject = planeNode.CreateComponent("StaticModel");
  35. planeObject.model = cache.GetResource("Model", "Models/Plane.mdl");
  36. planeObject.material = cache.GetResource("Material", "Materials/StoneTiled.xml");
  37. // Create a directional light to the world.
  38. Node@ lightNode = scene_.CreateChild("DirectionalLight");
  39. lightNode.direction = Vector3(0.6f, -1.0f, 0.8f); // The direction vector does not need to be normalized
  40. Light@ light = lightNode.CreateComponent("Light");
  41. light.lightType = LIGHT_DIRECTIONAL;
  42. light.castShadows = true;
  43. light.shadowBias = BiasParameters(0.00005f, 0.5f);
  44. // Set cascade splits at 10, 50 and 200 world units, fade shadows out at 80% of maximum shadow distance
  45. light.shadowCascade = CascadeParameters(10.0f, 50.0f, 200.0f, 0.0f, 0.8f);
  46. // Create first box for face camera trail demo with 1 column.
  47. boxNode1 = scene_.CreateChild("Box1");
  48. StaticModel@ box1 = boxNode1.CreateComponent("StaticModel");
  49. box1.model = cache.GetResource("Model", "Models/Box.mdl");
  50. box1.castShadows = true;
  51. RibbonTrail@ boxTrail1 = boxNode1.CreateComponent("RibbonTrail");
  52. boxTrail1.material = cache.GetResource("Material", "Materials/RibbonTrail.xml");
  53. boxTrail1.startColor = Color(1.0f, 0.5f, 0.0f, 1.0f);
  54. boxTrail1.endColor = Color(1.0f, 1.0f, 0.0f, 0.0f);
  55. boxTrail1.width = 0.5f;
  56. boxTrail1.updateInvisible = true;
  57. // Create second box for face camera trail demo with 4 column.
  58. // This will produce less distortion than first trail.
  59. boxNode2 = scene_.CreateChild("Box2");
  60. StaticModel@ box2 = boxNode2.CreateComponent("StaticModel");
  61. box2.model = cache.GetResource("Model", "Models/Box.mdl");
  62. box2.castShadows = true;
  63. RibbonTrail@ boxTrail2 = boxNode2.CreateComponent("RibbonTrail");
  64. boxTrail2.material = cache.GetResource("Material", "Materials/RibbonTrail.xml");
  65. boxTrail2.startColor = Color(1.0f, 0.5f, 0.0f, 1.0f);
  66. boxTrail2.endColor = Color(1.0f, 1.0f, 0.0f, 0.0f);
  67. boxTrail2.width = 0.5f;
  68. boxTrail2.tailColumn = 4;
  69. boxTrail2.updateInvisible = true;
  70. // Load ninja animated model for bone trail demo.
  71. Node@ ninjaNode = scene_.CreateChild("Ninja");
  72. ninjaNode.position = Vector3(5.0f, 0.0f, 0.0f);
  73. ninjaNode.rotation = Quaternion(0.0f, 180.0f, 0.0f);
  74. AnimatedModel@ ninja = ninjaNode.CreateComponent("AnimatedModel");
  75. ninja.model = cache.GetResource("Model", "Models/NinjaSnowWar/Ninja.mdl");
  76. ninja.material = cache.GetResource("Material", "Materials/NinjaSnowWar/Ninja.xml");
  77. ninja.castShadows = true;
  78. // Create animation controller and play attack animation.
  79. ninjaAnimCtrl = ninjaNode.CreateComponent("AnimationController");
  80. ninjaAnimCtrl.PlayExclusive("Models/NinjaSnowWar/Ninja_Attack3.ani", 0, true, 0.0f);
  81. // Add ribbon trail to tip of sword.
  82. Node@ swordTip = ninjaNode.GetChild("Joint29", true);
  83. swordTrail = swordTip.CreateComponent("RibbonTrail");
  84. // Set sword trail type to bone and set other parameters.
  85. swordTrail.trailType = TT_BONE;
  86. swordTrail.material = cache.GetResource("Material", "Materials/SlashTrail.xml");
  87. swordTrail.lifetime = 0.22f;
  88. swordTrail.startColor = Color(1.0f, 1.0f, 1.0f, 0.75f);
  89. swordTrail.endColor = Color(0.2, 0.5f, 1.0f, 0.0f);
  90. swordTrail.tailColumn = 4;
  91. swordTrail.updateInvisible = true;
  92. // Add floating text for info.
  93. Node@ boxTextNode1 = scene_.CreateChild("BoxText1");
  94. boxTextNode1.position = Vector3(-1.0f, 2.0f, 0.0f);
  95. Text3D@ boxText1 = boxTextNode1.CreateComponent("Text3D");
  96. boxText1.text = "Face Camera Trail (4 Column)";
  97. boxText1.SetFont(cache.GetResource("Font", "Fonts/BlueHighway.sdf"), 24);
  98. Node@ boxTextNode2 = scene_.CreateChild("BoxText2");
  99. boxTextNode2.position = Vector3(-6.0f, 2.0f, 0.0f);
  100. Text3D@ boxText2 = boxTextNode2.CreateComponent("Text3D");
  101. boxText2.text = "Face Camera Trail (1 Column)";
  102. boxText2.SetFont(cache.GetResource("Font", "Fonts/BlueHighway.sdf"), 24);
  103. Node@ ninjaTextNode2 = scene_.CreateChild("NinjaText");
  104. ninjaTextNode2.position = Vector3(4.0f, 2.5f, 0.0f);
  105. Text3D@ ninjaText = ninjaTextNode2.CreateComponent("Text3D");
  106. ninjaText.text = "Bone Trail (4 Column)";
  107. ninjaText.SetFont(cache.GetResource("Font", "Fonts/BlueHighway.sdf"), 24);
  108. // Create the camera.
  109. cameraNode = scene_.CreateChild("Camera");
  110. cameraNode.CreateComponent("Camera");
  111. // Set an initial position for the camera scene node above the plane
  112. cameraNode.position = Vector3(0.0f, 2.0f, -14.0f);
  113. }
  114. void CreateInstructions()
  115. {
  116. // Construct new Text object, set string to display and font to use
  117. Text@ instructionText = ui.root.CreateChild("Text");
  118. instructionText.text = "Use WASD keys and mouse to move";
  119. instructionText.SetFont(cache.GetResource("Font", "Fonts/Anonymous Pro.ttf"), 15);
  120. // Position the text relative to the screen center
  121. instructionText.horizontalAlignment = HA_CENTER;
  122. instructionText.verticalAlignment = VA_CENTER;
  123. instructionText.SetPosition(0, ui.root.height / 4);
  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. // Sum of timesteps.
  172. timeStepSum += timeStep;
  173. // Move first box with pattern.
  174. boxNode1.SetTransform(Vector3(-4.0 + 3.0 * Cos(100.0 * timeStepSum), 0.5, -2.0 * Cos(400.0 * timeStepSum)), Quaternion());
  175. // Move second box with pattern.
  176. boxNode2.SetTransform(Vector3(0.0 + 3.0 * Cos(100.0 * timeStepSum), 0.5, -2.0 * Cos(400.0 * timeStepSum)), Quaternion());
  177. // Get elapsed attack animation time.
  178. float swordAnimTime = ninjaAnimCtrl.GetAnimationState("Models/NinjaSnowWar/Ninja_Attack3.ani").time;
  179. // Stop emitting trail when sword is finished slashing.
  180. if (!swordTrail.emitting && swordAnimTime > swordTrailStartTime && swordAnimTime < swordTrailEndTime)
  181. swordTrail.emitting = true;
  182. else if (swordTrail.emitting && swordAnimTime >= swordTrailEndTime)
  183. swordTrail.emitting = false;
  184. }
  185. // Create XML patch instructions for screen joystick layout specific to this sample app
  186. String patchInstructions = "";