30_LightAnimation.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // Copyright (c) 2008-2015 the Urho3D project.
  3. // Copyright (c) 2015 Xamarin Inc
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. using AtomicEngine;
  24. namespace FeatureExamples
  25. {
  26. public class LightAnimationSample : Sample
  27. {
  28. public LightAnimationSample() : base() { }
  29. public override void Start()
  30. {
  31. base.Start();
  32. SetupInstructions();
  33. CreateScene();
  34. SetupViewport();
  35. }
  36. void SetupInstructions()
  37. {
  38. SimpleCreateInstructions("Use WASD keys and mouse/touch to move");
  39. }
  40. protected override void Update(float timeStep)
  41. {
  42. SimpleMoveCamera3D(timeStep);
  43. base.Update(timeStep);
  44. }
  45. void SetupViewport()
  46. {
  47. var renderer = GetSubsystem<Renderer>();
  48. renderer.SetViewport(0, new Viewport(scene, CameraNode.GetComponent<Camera>()));
  49. }
  50. void CreateScene()
  51. {
  52. var cache = GetSubsystem<ResourceCache>();
  53. scene = new Scene();
  54. // Create the Octree component to the scene. This is required before adding any drawable components, or else nothing will
  55. // show up. The default octree volume will be from (-1000, -1000, -1000) to (1000, 1000, 1000) in world coordinates; it
  56. // is also legal to place objects outside the volume but their visibility can then not be checked in a hierarchically
  57. // optimizing manner
  58. scene.CreateComponent<Octree>();
  59. // Create a child scene node (at world origin) and a StaticModel component into it. Set the StaticModel to show a simple
  60. // plane mesh with a "stone" material. Note that naming the scene nodes is optional. Scale the scene node larger
  61. // (100 x 100 world units)
  62. Node planeNode = scene.CreateChild("Plane");
  63. planeNode.Scale=new Vector3(100.0f, 1.0f, 100.0f);
  64. StaticModel planeObject = planeNode.CreateComponent<StaticModel>();
  65. planeObject.Model = (cache.Get<Model>("Models/Plane.mdl"));
  66. planeObject.SetMaterial(cache.Get<Material>("Materials/StoneTiled.xml"));
  67. // Create a point light to the world so that we can see something.
  68. Node lightNode = scene.CreateChild("PointLight");
  69. Light light = lightNode.CreateComponent<Light>();
  70. light.LightType = LightType.LIGHT_POINT;
  71. light.Range = (10.0f);
  72. // Create light animation
  73. ObjectAnimation lightAnimation=new ObjectAnimation();
  74. // Create light position animation
  75. ValueAnimation positionAnimation=new ValueAnimation();
  76. // Use spline interpolation method
  77. positionAnimation.InterpolationMethod= InterpMethod.IM_SPLINE;
  78. // Set spline tension
  79. positionAnimation.SplineTension=0.7f;
  80. positionAnimation.SetKeyFrame(0.0f, new Vector3(-30.0f, 5.0f, -30.0f));
  81. positionAnimation.SetKeyFrame(1.0f, new Vector3(30.0f, 5.0f, -30.0f));
  82. positionAnimation.SetKeyFrame(2.0f, new Vector3(30.0f, 5.0f, 30.0f));
  83. positionAnimation.SetKeyFrame(3.0f, new Vector3(-30.0f, 5.0f, 30.0f));
  84. positionAnimation.SetKeyFrame(4.0f, new Vector3(-30.0f, 5.0f, -30.0f));
  85. // Set position animation
  86. lightAnimation.AddAttributeAnimation("Position", positionAnimation, WrapMode.WM_LOOP, 1f);
  87. // Create text animation
  88. /*
  89. ValueAnimation textAnimation=new ValueAnimation();
  90. textAnimation.SetKeyFrame(0.0f, "WHITE");
  91. textAnimation.SetKeyFrame(1.0f, "RED");
  92. textAnimation.SetKeyFrame(2.0f, "YELLOW");
  93. textAnimation.SetKeyFrame(3.0f, "GREEN");
  94. textAnimation.SetKeyFrame(4.0f, "WHITE");
  95. var uiElement = UI.Root.GetChild("animatingText", false);
  96. uiElement.SetAttributeAnimation("Text", textAnimation, WrapMode.Loop, 1f);
  97. */
  98. // Create light color animation
  99. ValueAnimation colorAnimation=new ValueAnimation();
  100. colorAnimation.SetKeyFrame(0.0f, Color.White);
  101. colorAnimation.SetKeyFrame(1.0f, Color.Red);
  102. colorAnimation.SetKeyFrame(2.0f, Color.Yellow);
  103. colorAnimation.SetKeyFrame(3.0f, Color.Green);
  104. colorAnimation.SetKeyFrame(4.0f, Color.White);
  105. // Set Light component's color animation
  106. lightAnimation.AddAttributeAnimation("@Light/Color", colorAnimation, WrapMode.WM_LOOP, 1f);
  107. // Apply light animation to light node
  108. lightNode.ObjectAnimation=lightAnimation;
  109. // Create more StaticModel objects to the scene, randomly positioned, rotated and scaled. For rotation, we construct a
  110. // quaternion from Euler angles where the Y angle (rotation about the Y axis) is randomized. The mushroom model contains
  111. // LOD levels, so the StaticModel component will automatically select the LOD level according to the view distance (you'll
  112. // see the model get simpler as it moves further away). Finally, rendering a large number of the same object with the
  113. // same material allows instancing to be used, if the GPU supports it. This reduces the amount of CPU work in rendering the
  114. // scene.
  115. const uint numObjects = 200;
  116. for (uint i = 0; i < numObjects; ++i)
  117. {
  118. Node mushroomNode = scene.CreateChild("Mushroom");
  119. mushroomNode.Position = (new Vector3(NextRandom(90.0f) - 45.0f, 0.0f, NextRandom(90.0f) - 45.0f));
  120. mushroomNode.Rotation=new Quaternion(0.0f, NextRandom(360.0f), 0.0f);
  121. mushroomNode.SetScale(0.5f + NextRandom(2.0f));
  122. StaticModel mushroomObject = mushroomNode.CreateComponent<StaticModel>();
  123. mushroomObject.Model = (cache.Get<Model>("Models/Mushroom.mdl"));
  124. mushroomObject.SetMaterial(cache.Get<Material>("Materials/Mushroom.xml"));
  125. }
  126. // Create a scene node for the camera, which we will move around
  127. // The camera will use default settings (1000 far clip distance, 45 degrees FOV, set aspect ratio automatically)
  128. CameraNode = scene.CreateChild("Camera");
  129. CameraNode.CreateComponent<Camera>();
  130. // Set an initial position for the camera scene node above the plane
  131. CameraNode.Position = (new Vector3(0.0f, 5.0f, 0.0f));
  132. }
  133. }
  134. }